我很難嘗試在編輯窗體中設置選擇框的值。例如,我有我的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