jqGrid本地数据排序丢失信息

发布于 2024-11-19 19:04:11 字数 2046 浏览 1 评论 0原文

好吧,我正准备撕掉我的头发。我正在加载带有 JSON 数据的 jqGrid,但将“loadonce”设置为 true,以将其保留在本地。当我只显示列的默认内容时,排序工作正常,但我需要的是,某些列使用另一列中的信息来修改显示的内容。例如,我不想将“设备”和“型号”列显示在同一列下,例如“设备 - 型号”,并且为此使用自定义格式化程序。

问题是,在这种情况下,当我进行排序时,我丢失了“模型”信息,并且它变得“未定义”。这是我的代码的一部分:

mdlTable = tableWrap.jqGrid({
    url: loadURL,
    datatype: 'json',
    colNames: ['ID', 'Device', 'Description', 'IP', 'Model'],
    colModel: [
        {name:'id', index:'id', hidden:true, key:true},
        {name:'device', index:'device', width:192,
            formatter:function(value, options, rData){
                var str = "<a href='/administration/mdl/vwDevice.aspx?device_id=";
                str += rData[0] + "' target='_blank'>" + value;
                if ('' != rData[4]) str += " - " + rData[4];
                str += "</a>";
                return str;
            }
        },
        {name:'desc', index:'desc', width:256, sortable:false},
        {name:'ip', index:'ip', width:96},
        {name:'model', index:'model', hidden:true}
    ],
    sortname: 'id',
    viewrecords: true,
    loadonce: true,
    viewsortcols: [true,'vertical',true],
    gridview: true,
    ignoreCase: true
})
.navGrid('#deviceList_footer', {edit:false, add:false, del:false, cloneToTop:true});

正如您所看到的,我隐藏了型号列,并将该信息“移动”到设备列,因为那是它应该显示的位置。加载时一切都很好,但是一旦我进行排序或搜索并按原样刷新视图,数据的“副本”就会由于某种原因丢失。如果我显示模型列,那里的信息仍然很好,只是设备列获得“未定义”值。

我尝试触发“reloadGrid”,但没有帮助。我还尝试添加 unformat 功能,但我不确定在那里能做什么。我基本上只是返回了 $(cellobject).html() - 这显然不起作用。

编辑:添加示例 JSON 数据

{ "rows" : [{
        "id" : "181",
        "cell" : ["181", "Router A", "some description", "55.444.33.222", "Model 1"]
    }, {
        "id" : "291",
        "cell" : ["291", "Router B", "some description", "55.333.22.444", "Model 2"]
    }, {
        "id" : "1346",
        "cell" : ["1346", "Router C", "some description", "55.111.44.333", "Model 3"]
    }, {
        "id" : "1999",
        "cell" : ["1999", "Router D", "some description", "55.222.11.000", "Model 4"]
    }
]}

Okay I'm just about ready to tear my hair out. I'm loading a jqGrid with JSON data but with "loadonce" set to true, to keep it local. When I just show the default content of columns sorting works fine, but what I need is, for some columns to use info from another column to modify what is shown. For example, instead of having a "device" and a "model" column, I want to show both under one column, like this "device - model", and I use a custom formatter for that.

The problem is, in this case, when I do sort, I lose the "model" information and it becomes "undefined". Here's part of my code:

mdlTable = tableWrap.jqGrid({
    url: loadURL,
    datatype: 'json',
    colNames: ['ID', 'Device', 'Description', 'IP', 'Model'],
    colModel: [
        {name:'id', index:'id', hidden:true, key:true},
        {name:'device', index:'device', width:192,
            formatter:function(value, options, rData){
                var str = "<a href='/administration/mdl/vwDevice.aspx?device_id=";
                str += rData[0] + "' target='_blank'>" + value;
                if ('' != rData[4]) str += " - " + rData[4];
                str += "</a>";
                return str;
            }
        },
        {name:'desc', index:'desc', width:256, sortable:false},
        {name:'ip', index:'ip', width:96},
        {name:'model', index:'model', hidden:true}
    ],
    sortname: 'id',
    viewrecords: true,
    loadonce: true,
    viewsortcols: [true,'vertical',true],
    gridview: true,
    ignoreCase: true
})
.navGrid('#deviceList_footer', {edit:false, add:false, del:false, cloneToTop:true});

So as you can see, I hide the model column, and "move" that information over to the device column because that's where it's suppose to be shown. It's all fine on load, but as soon as I do sort, or search and it refreshes the view as it were, the "copy" of the data is lost for some reason. If I show the model column the info there remains just fine, it's just the device column that gets the "undefined" value.

I tried triggering "reloadGrid", doesn't help. I also tried to add unformat function but I'm not sure what I can do there. I basically just returned a $(cellobject).html() - that didn't work obviously.

Edit: Added sample JSON data

{ "rows" : [{
        "id" : "181",
        "cell" : ["181", "Router A", "some description", "55.444.33.222", "Model 1"]
    }, {
        "id" : "291",
        "cell" : ["291", "Router B", "some description", "55.333.22.444", "Model 2"]
    }, {
        "id" : "1346",
        "cell" : ["1346", "Router C", "some description", "55.111.44.333", "Model 3"]
    }, {
        "id" : "1999",
        "cell" : ["1999", "Router D", "some description", "55.222.11.000", "Model 4"]
    }
]}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2024-11-26 19:04:11

问题是,一开始自定义格式化程序的第三个参数是数组类型,后来不再是数组类型。因此,您必须将格式化程序修改为类似的内容

formatter: function(value, options, rData){
    var model = '',
        str = "<a href='/administration/mdl/vwDevice.aspx?device_id=" +
              value + "' target='_blank'>" + value;
    if ($.isArray(rData)) {
        model = rData[4];
    } else {
        model = rData.model;
    }
    if (model) {
        str += " - " + model;
    }
    str += "</a>";
    return str;
}

然后排序才会起作用:请参阅此处< /a>.

还有一点提示。您使用 id 列的 key:true 属性。在这种情况下,您不需要两次包含相同的 id 值。您可以将 JSON 数据简化为以下内容。

{ "rows" : [
    ["181", "Router A", "some description", "55.444.33.222", "Model 1"],
    ["291", "Router B", "some description", "55.333.22.444", "Model 2"],
    ["1346", "Router C", "some description", "55.111.44.333", "Model 3"],
    ["1999", "Router D", "some description", "55.222.11.000", "Model 4"]
]}

为了能够读取新的 JSON 格式,jqGrid 中的唯一更改是附加参数 jsonReader: {cell:''}。请在此处查看结果。

The problem is that at the beginning the third parameter of the custom formatter has array type and later no more. So you have to modify the formatter to something like

formatter: function(value, options, rData){
    var model = '',
        str = "<a href='/administration/mdl/vwDevice.aspx?device_id=" +
              value + "' target='_blank'>" + value;
    if ($.isArray(rData)) {
        model = rData[4];
    } else {
        model = rData.model;
    }
    if (model) {
        str += " - " + model;
    }
    str += "</a>";
    return str;
}

Then the sorting will work: see here.

One more tip. You use key:true property of the id column. In the case you don't need include the same id values twice. You can reduce the JSON data to the following

{ "rows" : [
    ["181", "Router A", "some description", "55.444.33.222", "Model 1"],
    ["291", "Router B", "some description", "55.333.22.444", "Model 2"],
    ["1346", "Router C", "some description", "55.111.44.333", "Model 3"],
    ["1999", "Router D", "some description", "55.222.11.000", "Model 4"]
]}

The only change in the jqGrid, to be able to read the new JSON format, is additional parameter jsonReader: {cell:''}. See the results here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文