我很难尝试在编辑窗体中设置选择框的值。例如,我有我的colModel像这样设置。jqGrid - 在编辑窗体中设置选择值
colModel:[
{name:'communication_id', key: true, index:'communication_id', width:30},
{name:'communication_type_id', index:'communication_type_id', width:30},
{name:'communication_type_select',index:'communication_type_select',hidden:true,width:150, editable:true,edittype:'select',formatter:'select',editrules: {edithidden:true},
formoptions:{label:'Communication Type'},
editoptions:{dataUrl:"working_data_url",
buildSelect: function(json){
var response = $.parseJSON(json);
var s = '<select>';
$.each(response.results,function(i,com){
s += ('<option value="' + com.communication_type_id + '">'+ com.communication_type_desc + '</option>');
});
return s + "</select>";
},dataInit: function(elem){
alert(temp);
//alert($('#com_table_communication_type_id').val());
//$(elem).val($('#com_table_communication_type_id').val());
}}},
{name:'communication_send_dt', index:'communication_send_dt', width:150, sortable:true, sorttype: 'date',
firstsortorder: 'desc', datefmt:'m/d/Y', editable:true},
editoptions: {recreateForm:true},
rowNum:10,
width:'100%',
rowList:[10,20,30],
pager: '#com_pager',
sortname: 'communication_send_dt',
viewrecords: true,
sortorder: "desc",
loadonce:true,
caption: "Communication",
jsonReader: {
repeatitems : false,
root: "results"
},
height: '100%',
onSelectRow: function(communication_id){
var comtype = $(this).getRowData(communication_id);
var temp = comtype['communication_type_id'];
}
});
grid.jqGrid('navGrid','#com_pager',{edit:true,add:false,del:false});
当我点击编辑按钮时,它会正确加载选择选项,但我在选择哪个选项时遇到问题。我希望来自communication_type_id的值加载到communication_type_select中,并且我尝试了不同的事情来实现这一点。基本上,如果communication_type_id中的id是2,那么当编辑窗体加载时,我希望编辑窗体中的选择框设置为2。对此有何帮助?
更新1:我现在主要使用beforeShowForm工作,但现在我遇到了一个奇怪的事情。当我在beforeShowForm中有一个警报时,一切正常,但是当它注释掉时,它不起作用!感谢您的帮助@ Oleg!
grid.jqGrid('navGrid','#com_pager',{edit:true,add:false,del:false},
{closeOnEscape:true, recreateForm:true,beforeShowForm: function(formid){
//alert("com type id = "+comidvar + " response id = "+comrespvar + " com form type id = "+comfrmtypevar);
$("#communication_type_select", formid).attr("value",comidvar);
$("#form_response_select", formid).attr("value",comrespvar);
$("#form_type_select", formid).attr("value", comfrmtypevar);
}},
最后一次修改:''communication_type_select''中的选择将与'beforeShowForm'的执行异步构建。所以你应该至少移动''beforeShowForm'的代码部分,它在'buildSelect'中使用''#communication_type_select'''。 – Oleg
你应该总是用@Oleg给我的回答写一个小评论,告诉你你改变了你的问题的文字。我偶然发现你目前的变化是纯粹的。 – Oleg