2012-06-28 42 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

相关问题