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

时间:2011-09-23 23:51:06

标签: c# asp.net jquery-plugins jqgrid

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:2)

试试这个:

  1。为网格的onSelectRow事件定义处理程序(onSelectRow_handler)。
  2。在onSelectRow处理程序中:
  2.1。将全局范围变量(lastRow)设置为函数的id参数   2.2。调用jqGrid的editRow()函数将网格置于编辑模式。这将触发您定义为custom_element渲染器(myelem)的函数   3. 在myelem中:调用jqGrid的getRowData方法获取刚刚选择进行编辑的行的行数据。从那里,您可以获取ElementType列中的值,并执行决定要呈现哪个元素的逻辑。

你必须稍微调整我的代码,因为我没有100%端到端地测试它。我确实验证了第3步的所有工作。我没有研究如何编写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);
    }
}