【问题标题】:jqGrid - Setting select value in edit formjqGrid - 在编辑表单中设置选择值
【发布时间】:2012-06-28 18:46:11
【问题描述】:

我很难在编辑表单中设置选择框的值。例如,我的 colModel 是这样设置的。

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 的执行异步构建。因此,您应该至少在buildSelect 中移动使用"#communication_type_select"beforeShowForm 代码部分。
  • 您应该始终在我的回答中用@Oleg 写小评论,以通知您您更改了问题的文本。我偶然发现了你当前的变化。

标签: jqgrid


【解决方案1】:

如果我理解你是正确的,你应该使用带有data 属性的jqGrid 的ajaxSelectOptions 选项。您可以在data 中定义一些额外的选项,例如communication_type_id,您可以使用$("#list").jqGrid('getGridParam', 'selrow')getCell 返回该值,以从communication_type_id 列中获取值。详情请见the answer

【讨论】:

  • 感谢您的帮助。我认为行动的时机让我感到困惑。如果我把 $("#communication_type_select", formid).attr("value",comidvar);在 setTimeout 中的 beforeShowForm 中加入 1000 延迟,它会再次工作,但会弹出编辑窗口,然后在超时结束后更改为正确的选择值。有更好的方法吗?我真的不明白要迁移到 buildSelect 需要什么。
  • 另外,是否可以加载编辑表单,填充选择,并在某些加载...图形或其他内容下设置正确的选择选项?
  • @user1489283:对不起,我不确定我是否理解正确。您想在加载选择时显示一些图形,还是希望在包含图形的选择中显示选项?
  • 抱歉不清楚。我正在使用 editoptions 和 buildSelect 将列表加载到选择下拉列表中,然后我想将下拉列表中的选定选项设置为正在编辑的行。除了我必须在加载后使用 setTimeout 来设置选择之外,这一切都在进行中。我想知道是否有可以在加载时显示的加载消息。
猜你喜欢
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2011-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
相关资源
最近更新 更多