【问题标题】:JqGrid: sorting of local dataJqG​​rid:本地数据的排序
【发布时间】:2014-07-23 13:12:10
【问题描述】:

在这个例子中我对本地数据排序有一些问题。排序不起作用,我不知道它的原因。你能解释一下我的代码中的问题吗?

<table id="list" ></table>
<div id="pager"></div>
<script type="text/javascript">
    var myData =  [
                        { id: "1",  cell: ["1", "test"] },
                        { id: "2",  cell: ["2", "test2"] },
                        { id: "3",  cell: ["3", "test3"] },
                        { id: "4",  cell: ["4", "test4"] }
    ];
jQuery("#list").jqGrid({
    data: myData,
    datatype: "local",
    colNames: ['MyId','Client'],
    colModel: [{ name: 'MyId', index: 'MyId', width: 100, align: 'left' ,  sortable: true},
               { name: 'Client', index: 'Client', width: 100, align: 'left', sortable: true }],
    rowNum:10,
    rowList:[10,20,30,100],
    pager: '#pager',
    sortname: 'Id',
    localReader: {repeatitems: true},          
    viewrecords: true,
    sortable: true,
    sortorder: "asc",
    caption: "Tests",
    loadonce: true
});
jQuery("#list").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false });

</script>

附:在此演示中,排序也不适用于本地数据。 http://www.ok-soft-gmbh.com/jqGrid/LocalReader.htm

【问题讨论】:

  • “不起作用”是什么意思?能具体一点吗?
  • “不起作用”意味着我无法使用 jqgrid 排序对网格中的值进行排序。您可以单击演示link 中的“客户端”列的标题,并看到排序不会改变元素的顺序。
  • 而排序就是jqgrid本地排序。

标签: jqgrid


【解决方案1】:

您是对的:jqGrid 在使用datatype: "local" 时存在错误,data 参数采用localReader: {repeatitems: true} 格式。

有很多方法可以修复错误。最简单的一个似乎我可以替换the lines

if(locdata || ts.p.treeGrid===true) {
    rd[locid] = $.jgrid.stripPref(ts.p.idPrefix, idr);
    ts.p.data.push(rd);
    ts.p._index[rd[locid]] = ts.p.data.length-1;
}

以下

if(locdata || ts.p.treeGrid===true) {
    rd[locid] = $.jgrid.stripPref(ts.p.idPrefix, idr);
    ts.p.data.push(rd);
    ts.p._index[rd[locid]] = ts.p.data.length-1;
} else if (ts.p.datatype === "local" && dReader.repeatitems) {
    var idStripted = $.jgrid.stripPref(ts.p.idPrefix, idr),
        iData = ts.p._index[idStripted];
    if (iData !== undefined && ts.p.data != null && ts.p.data[iData] != null) {
        $.extend(true, ts.p.data[iData], rd);
    }
}

The demo 使用the fixed code,它现在可以正常工作。稍后我将发布相应的拉取请求以及对 trirand 的建议修复。

更新:我在the pull request 中发布了更好的修复实现。 更新 2: 我的拉取请求已经是 mergedthe main code of jqGrid on the github。您可以在jquery.jqGrid.srs.js 上进行相同的更改,从here 下载修改后的文件。无论如何,jqGrid 的下一个版本(4.6.0 以上的版本)将包含所描述问题的修复。

【讨论】:

  • 感谢您解决我的问题。您能否在不更改库源代码的情况下建议其他修复此错误的方法?我认为我不应该更改我的本地 jqgrid 库副本的源代码,因为其他人会更改我的代码可能会更改 jqgrid 的源并导致再次出现此错误。
  • @Aracturat:不客气!我会将错误修复发布到 trirand,我认为它将包含在 jqGrid 的下一个版本中。所以建议更改jquery.jqGrid.src.js 的代码是非常安全的。如果你更喜欢使用 jqGrid 的原始代码,那么你应该只使用默认的 repeatitems: false 格式 myData: var myData = [{id:"1", MyId:"1", Client:"test"}, ...]; 你应该删除 localReader 选项。
  • @Aracturat:请参阅我的回答的更新部分。
  • @Aracturat:请参阅我的回答的 UPDATED 2 部分。
猜你喜欢
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多