JqGrid: sorting of local data

2020-07-31 11:37发布

问题:

I have some problem with local data sorting in this example. Sorting doesn't work and I don't know reason of it. Could you explain problem in my code?

<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>

P.S. Sorting also doesn't work in this demo with local data. http://www.ok-soft-gmbh.com/jqGrid/LocalReader.htm

回答1:

You are right: jqGrid have bug in usage datatype: "local" with data parameter in localReader: {repeatitems: true} format.

There are many ways to fix the bug. One from the most easy one seems me to replace 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;
}

with the following

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 uses the fixed code and it works correctly now. I will post later the corresponding pull request with suggested fix to trirand.

UPDATED: I posted better implementation of the fix in the pull request. UPDATED 2: My pull request is already merged to the main code of jqGrid on the github. You can do the same changes on jquery.jqGrid.srs.js, download the modified file from here. In any way the next version of jqGrid (version higher as 4.6.0) will contains the fix of described problem.



标签: jqgrid