jqGrid - 如何根据*initial*列值设置自定义editoptions?

bfl*_*ow1 6 c# asp.net jquery-plugins jqgrid

我使用EF4和ASP.NET Web窗体的开源jqGrid插件.我需要根据DB中的列值在可内联编辑的网格行中设置输入元素.例如,第一行可以包含DDL,第二行可以包含复选框等.

我正在尝试使用custom_element和实现这一点custom_values,如下所示:

$("#grid1").jqGrid({
    url: 'Default.aspx/getGridData',
    datatype: 'json',
    ...
    colModel: [
    ...
    //contains the input type ('select', etc.)
    { name: 'InputType', hidden:true }, 
    ...
    //may contain a string of select options ('<option>Option1</option>'...)
    { 
      name: 'Input', 
      editable:true, 
      edittype:'custom', 
      editoptions:{
         custom_element: /* want cell value from InputType column here */ , 
         custom_value:   /* want cell value from Input column here */ 
      } 
     }, 
    ...
    ]
});
Run Code Online (Sandbox Code Playgroud)

该jqGrid的文档说,我可以调用自定义函数来设置custom_elementcustom_values,但我不知道怎样才能捕捉的列值,并将其传递到我的自定义功能.

为了设置custom_values,我确实注意到Oleg使用该list:参数的很好的解决方案,但这似乎涉及额外的Ajax调用.我想避免这种情况,因为我已经从网格的初始Ajax调用中获得了所需的所有数据.

总之,我需要在内联编辑模式下执行以下操作:

  1. 从DB值动态分配输入类型
  2. 从DB字符串动态分配输入值(对于DDL或复选框)

我也愿意跳过使用custom_elementcustom_values,但是我仍然面临着动态设置edittypeeditoptions:{value:}参数的相同问题.

关于如何做到这一点的任何想法?我应该采取不同的方法吗?

更新:感谢您帮助我的努力.根据请求,这是我的JSON响应的缩写示例:

{"d":[
{"Input":null,"InputType":"select"},
{"Input":"From downtown, proceed west on Interstate 70.", "InputType":"text"}
]}
Run Code Online (Sandbox Code Playgroud)

有了这些数据,我想在一行中显示一个空选择,在下一行显示一个填充的文本字段.两者都可以内联编辑.

解决方案:我已经回到这个问题,以便找到一个涉及使用custom_element和的解决方案custom_values.这是我的解决方案(基于下面接受的答案)改变edittypeeditoptions:

loadComplete: function () {
    var rowIds = $("#grid1").jqGrid('getDataIDs');

    $.each(rowIds, function (i, row) {
       var rowData = $("#grid1").getRowData(row);

       if (rowData.InputType == 'select') {
          $("#grid1").jqGrid('restoreRow', row);
                var cm = $("#grid1").jqGrid('getColProp', 'Input');
                cm.edittype = 'select';
                cm.editoptions = { value: "1:A; 2:B; 3:C" };
                $("#grid1").jqGrid('editRow', row);
                cm.edittype = 'text';
                cm.editoptions = null;
       }
   });
}
Run Code Online (Sandbox Code Playgroud)

Nota Bene:对我来说一件重要的事情就是记得在打电话之后editoptions回过头来.另外,正如Oleg在评论中提到的那样,避免使用自定义元素允许我实现datepicker输入而不会有额外的麻烦.这对我的应用来说很重要,所以我最终接受了Oleg的回答,但我仍然赞同Walter的回答.如果这是不好的形式,我真诚地道歉.我只想奖励最适合我的解决方案.nulleditrow

Ole*_*leg 3

如果您使用倾斜编辑,您可以editRow直接在代码中的某处调用方法。在该方法内部 ,与编辑相关的editRow所有选项都将被检查和使用。colModel因此,您可以动态更改任何选项,例如editableedittypeeditoptions答案展示了如何改变editable财产。以同样的方式,您可以更改任何其他属性。

如果需要,您可以在loadComplete事件句柄内设置有关编辑类型和选项的信息。它具有data代表从服务器发送的原始数据的参数。因此,您可以使用其他信息来扩展数据editable,并设置edittypeeditoptions基于该信息的任何列。