2012-06-28 63 views
0

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

最後一次修改:''communication_type_select''中的選擇將與'beforeShowForm'的執行異步構建。所以你應該至少移動''beforeShowForm'的代碼部分,它在'buildSelect'中使用''#communication_type_select'''。 – Oleg

+0

你應該總是用@Oleg給我的回答寫一個小評論,告訴你你改變了你的問題的文字。我偶然發現你目前的變化是純粹的。 – Oleg

回答

0

如果我理解你正確,你應該使用的jqGrid的ajaxSelectOptions選項與data財產。您可以在data中定義一些附加選項,如communication_type_id,通過$("#list").jqGrid('getGridParam', 'selrow')的使用返回值,然後使用getCell可獲得communication_type_id列的值。詳情請參閱the answer

+0

感謝您的幫助。我認爲這些行動的時機是讓我搞砸的。如果我把$(「#communication_type_select」,formid).attr(「value」,comidvar);在beforeShowForm中以1000延遲的setTimeout行,它會再次運行,但編輯窗口彈出,然後在超時結束後更改爲正確的選擇值。有沒有更好的方法來做到這一點?我真的不明白我需要遷移到buildSelect。 – user1489283

+0

另外,是否有可能加載編輯表單,填充選擇,並設置正確的選擇選項下一些加載...圖形或東西? – user1489283

+0

@ user1489283:對不起,我不確定我是否正確理解你。你想在加載選擇過程中顯示一些圖形還是想在包含圖形的選擇中顯示選項? – Oleg

相關問題