【问题标题】:How to bind jqGrid colModel Formatter dynamically如何动态绑定jqGrid colModel Formatter
【发布时间】:2013-04-07 23:49:42
【问题描述】:

我正在尝试动态绑定 jqGrid 列模型的格式化程序。我如下动态构建colModel 数组。

ColModel:[{name:Id,width:50,formatter:customerLinkFormatter}]

我已将格式化程序扩展如下

$.extend($.fn.fmatter, {
customerLinkFormatter: function (cellvalue, options, rowdata) {
    return '<a href="CustomerEdit.aspx?id=' + rowdata[options.colModel.name] + '"> ' + cellvalue + '</a>';
}

});

但没有显示 Id 列的链接。请帮我弄清楚。

这是部分代码

$(document).ready(function () {
        "use strict";
        $.ajax({
            type: "POST",
            url: "../Hdlr.ashx?",
            datatype: "json",
            success: function (msg) {
                jqcolNames = msg.ColNames,
                jqcolModel = msg.ColModel,

                PopulateGrid();
            },
            error: function (msg) {
                alert(' error  ' + msg.responseText);
            }
        });
    });

    function PopulateGrid() {
        $('#list').jqGrid({
            url: "../Hdlr.ashx?",
            colNames: jqcolNames,
            colModel: jqcolModel,
            jsonReader: {
                cell: "",
                id: "0",
                repeatitems: false
            },
            rowNum: 10,
            rowList: [10, 20, 30],
            pager: "#pager",
            rownumbers: true,
            viewrecords: true,
            search: false,
            caption: "Grid Information"
        }).jqGrid("navGrid", "#pager", { edit: false, add: false, del: false, search: false });
    }

【问题讨论】:

    标签: asp.net jqgrid


    【解决方案1】:

    尝试像定义函数一样定义formatter

      function customerLinkFormatter(cellvalue, options, rowdata) {
                   return '<a href="CustomerEdit.aspx?id=' + rowdata.name + '"> ' + cellvalue + '</a>';
                };
    

    【讨论】:

    • 这是我之前做的。数据库中的 formatter 字段直接具有该功能。它也不起作用
    【解决方案2】:

    如果你扩展了$.fn.fmatter,那么你应该使用string "customerLinkFormatter" 而不是直接使用函数customerLinkFormatter:

    colModel:[{name: "Id", width: 50, formatter: "customerLinkFormatter"}]
    

    name:Id的用法如果之前没有用对应的字符串值定义Id变量也是错误的。

    您写过动态绑定 jqGrid 的格式化程序。您只需更改colModel 内的formatter 属性。例如,可以使用setColProp 方法。请参阅heresetColProp 的用法示例。

    【讨论】:

    • 感谢您的回复。当我扩展了格式化程序时,我还需要绑定 colModel 的格式化程序吗?
    • @user1077595:对不起,我不明白你的问题。对于$.extend($.fn.fmatter, {customerLinkFormatter: function(...) {...}});,您定义$.fn.fmatter.customerLinkFormatter 函数,该函数将被调用以格式化用formatter: "customerLinkFormatter" 定义的列上的所有单元格。因此,您的自定义格式化程序的工作方式与 jqGrid 的任何其他 predefined formatter 相同。
    • 我从数据库中获取 colModel 如下 "ColModel":[{"name":"CUSTOMER_ID","width":"60","formatter":"customerLinkFormatter"},{" name":"Description","width":"50","formatter":null}] 但它不起作用
    • @user1077595:什么不工作?你有什么错误?你调试过代码吗?您是否在创建网格之前定义了$.fn.fmatter.customerLinkFormatter?您能否将alert("in customerLinkFormatter"); 作为$.fn.fmatter.customerLinkFormatter 的第一行并验证是否调用了警报?此外,您发布了带有错误的永久代码。你使用"ColModel"(名字错误!!!)还是"colModel"
    • 我像这样成功地为网格动态构建 ColModel:function (msg) { colNames = msg.ColNames, colModel = msg.ColModel, PopulateGrid();什么不工作是列不显示为链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 2011-04-21
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多