简体   繁体   English

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

[英]jqGrid - How to set custom editoptions based on *initial* column values?

I am using the open source jqGrid plugin with EF4 and ASP.NET Web Forms. 我使用EF4和ASP.NET Web窗体的开源jqGrid插件。 I need to set an input element in an inline-editable grid row based on a column value from the DB. 我需要根据DB中的列值在可内联编辑的网格行中设置输入元素。 For example, the first row could contain a DDL, the second row could contain a checkbox, etc. 例如,第一行可以包含DDL,第二行可以包含复选框等。

I'm trying to achieve this using the custom_element and custom_values , like so: 我正在尝试使用custom_elementcustom_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 */ 
      } 
     }, 
    ...
    ]
});

The jqGrid docs say that I can call custom functions to set custom_element and custom_values , but I don't see how I can capture column values and pass them into my custom functions. jqGrid 文档说我可以调用自定义函数来设置custom_elementcustom_values ,但是我没有看到如何捕获列值并将它们传递给我的自定义函数。

For setting custom_values , I did notice Oleg's nice solution using the list: parameter, but that appeared to involve an extra Ajax call. 为了设置custom_values ,我确实注意到Oleg使用list:参数的好解决方案 ,但这似乎涉及额外的Ajax调用。 I want to avoid this, as I already have the all data I need from the initial Ajax call for the grid. 我想避免这种情况,因为我已经从网格的初始Ajax调用中获得了所需的所有数据。

In summary, I need to do the following while in inline-edit mode: 总之,我需要在内联编辑模式下执行以下操作:

  1. dynamically assign an input type from a DB value 从DB值动态分配输入类型
  2. dynamically assign input values (for DDL or checkboxes) from a DB string 从DB字符串动态分配输入值(对于DDL或复选框)

I am also open to skipping the use of custom_element and custom_values , but then I still face the same problem of dynamically setting the edittype and editoptions:{value:} parameters. 我也愿意跳过使用custom_elementcustom_values ,但是我仍然面临着动态设置edittypeeditoptions:{value:}参数的同样问题。

Any ideas on how to do this? 关于如何做到这一点的任何想法? Is there a different approach that I should be taking? 我应该采取不同的方法吗?

UPDATE : Thanks for your efforts to help me out. 更新 :感谢您帮助我的努力。 Per request, here is an abbreviated example of my JSON response: 根据请求,这是我的JSON响应的缩写示例:

{"d":[
{"Input":null,"InputType":"select"},
{"Input":"From downtown, proceed west on Interstate 70.", "InputType":"text"}
]}

With this data, I would want to show an empty select in one row, and a populated text field in the next row. 有了这些数据,我想在一行中显示一个空选择,在下一行显示一个填充的文本字段。 Both would be editable inline. 两者都可以内联编辑。

SOLUTION : I have returned to this problem in order to find a solution that does not involve using custom_element and custom_values . 解决方案 :我已回到此问题,以便找到涉及使用custom_elementcustom_values的解决方案。 Here is my solution (based on the accepted answer below) to changing edittype and editoptions : 以下是我更改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;
       }
   });
}

Nota Bene : One important thing for me was remembering to set the editoptions back to null , after calling editrow . Nota Bene :对我来说重要的一件事是在调用editrow之后记得将editoptions设置为null Also, as Oleg mentioned in the comments, avoiding the use of custom elements allows me to implement datepicker inputs without extra trouble. 另外,正如Oleg在评论中提到的那样,避免使用自定义元素允许我实现datepicker输入而不会有额外的麻烦。 This was important for my app, so I ended up accepting Oleg's answer, but I still upvoted Walter's answer, as well. 这对我的应用来说很重要,所以我最终接受了Oleg的回答,但我仍然赞同Walter的回答。 If this is bad form, I sincerely apologize. 如果这是不好的形式,我真诚地道歉。 I simply wanted to reward the solution that worked best for me. 我只想奖励最适合我的解决方案。

If you use incline editing you call editRow method somewhere directly in your code. 如果您使用倾斜编辑,则可以直接在代码中的某处调用editRow方法。 Inside of the editRow method all options from the colModel , which are related to editing, will be examined and use. editRow方法内部,将检查并使用colModel中与编辑相关的所有选项。 So you can change dynamically any options like editable , edittype or editoptions . 所以, 你可以动态地改变任何选项 editableedittypeeditoptions The answer shows how one can change editable property. 答案显示了如何改变editable属性。 In the same way you can change any other properties. 以同样的方式,您可以更改任何其他属性。

If you want you can set the information about editing type and option inside of loadComplete event handle. 如果需要,可以在loadComplete事件句柄内设置有关编辑类型和选项的信息。 It has data parameter which represent the original data sent from the server. 它具有表示从服务器发送的原始数据的data参数。 So you can extend the data with and other information and set editable , edittype or editoptions for any columns based on the information. 这样可以延长使用等信息的数据,并设置editableedittypeeditoptions基于信息的任何列。

Try this: 试试这个:

1. Define a handler for the grid's onSelectRow event (onSelectRow_handler) . 1.为网格的onSelectRow事件定义处理程序(onSelectRow_handler)。
2. Inside the onSelectRow handler: 2.在onSelectRow处理程序中:
2.1. 2.1。 Set a globally scoped variable (lastRow) to the function's id parameter. 将全局范围的变量(lastRow)设置为函数的id参数。
2.2. 2.2。 Call jqGrid's editRow() function to put the grid into edit-mode. 调用jqGrid的editRow()函数将网格置于编辑模式。 This will trigger the function that you have defined as your custom_element renderer (myelem). 这将触发您定义为custom_element渲染器(myelem)的函数。
3. Inside myelem: call jqGrid's getRowData method to get the row data of the row you just selected for edit. 3.在myelem中:调用jqGrid的getRowData方法来获取刚刚选择进行编辑的行的行数据。 From there you can get the value in the ElementType column and do your logic that decides which element to render. 从那里,您可以获取ElementType列中的值,并执行决定要呈现哪个元素的逻辑。

You'll have to tweak my code a bit, as I didn't test it 100% end-to-end. 你必须稍微调整我的代码,因为我没有100%端到端地测试它。 I did verify that everything up to step 3 works. 我确实验证了第3步的所有工作。 I didn't do any research into how you would code myvalue(). 我没有研究如何编写myvalue()。

function renderGrid () {

    $("#grid").jqGrid({
        datatype: "local",
        colNames: ['Id', 'ElementType', 'Name' ],
        colModel: [
            { name: 'Id', index: 'Id', key: true, hidden: true },
            { name: 'ElementType', index: 'ElementType', },
            { name: 'FullName', index: 'FullName',
              editable: true, edittype: 'custom', 
              editoptions: { custom_element: myelem, custom_value: myvalue} }],
        viewrecords: true,
        caption: "",
        autowidth: true,
        height: 'auto',
        forceFit: true ,
        onSelectRow: onSelectRow_handler           
    });


}

var lastRow = null;
function onSelectRow_handler(id) {

    if(id && id!==lastRow){            
        lastRow=id;
    }
    // editRow will send grid into edit mode which will trigger
    $("#grid").editRow(id, true);

}

function myelem(value, options) {
    var data = $("#grid").getRowData(lastRow);
    // the elementType column contains a key to
    // indicate what Input Element to render
    var elementType = data.ElementType;

    if (elementType == 'text') {        
        var el = document.createElement("input");
        el.type = "text";
        el.value = value;         
    }
    if (elementType == 'checkbox') {
        // etc
    }

    return el;
}

function myvalue(elem, operation, value) {
    if (operation === 'get') {
        return $(elem).find("input").val();
    } else if (operation === 'set') {
        $('input', elem).val(value);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM