jqGrid本地数据排序丢失信息 [英] jqGrid local data sorting loses information

查看:369
本文介绍了jqGrid本地数据排序丢失信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我即将准备把头发拔掉.我正在使用JSON数据加载jqGrid,但将"loadonce"设置为true,以使其保持本地状态.当我仅显示列的默认内容时,排序工作正常,但是我需要的是,某些列使用另一列中的信息来修改显示的内容.例如,我没有在设备"和模型"列中显示,而是希望在一个列中同时显示,例如设备-模型",为此我使用了自定义格式程序.

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.

我尝试触发"reloadGrid",但无济于事.我也尝试添加unformat函数,但是我不确定在那里可以做什么.我基本上只是返回了$(cellobject).html()-显然没有用.

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.

添加了示例JSON数据

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"]
    }
]}

推荐答案

问题是自定义格式化程序的第三个参数在开始时具有数组类型,而在此之后不再具有数组类型.因此,您必须将格式化程序修改为类似的

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.

另一个提示.您使用id列的key:true属性.如果您不需要两次包含相同的id值.您可以将JSON数据简化为以下内容

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"]
]}

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

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

这篇关于jqGrid本地数据排序丢失信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆