JQuery jqgrid not sorting on client side

前端 未结 2 1421
名媛妹妹
名媛妹妹 2020-12-17 04:13

The data loads fine into the grid, but it won\'t sort.

When I click in the table header, the sort arrows appear, but the data\'s not being sorted.

Thanks.

相关标签:
2条回答
  • 2020-12-17 04:28

    You code have many important errors:

    1. colModel contains index properties which are different from the value of name for the same item. It's the main bug. You don't specified any sortname option of jqGrid so the values of index properties will be never seen by the server. If you use loadonce: true then index properties must be the same as the values of name properties. I recommend you don't include index properties at all. In the case index properties will be initialized with the values of name properties
    2. You use wrong value of id property in jsonReader: id: "0". One use such value sometime in case of repeatitems: true. In the case the row will be represented in JSON input as array. So 0 value will be correct because in can be used as index in the array. In case of usage repeatitems: false the items represented rows of data in the JSON input are objects with named properties. So You should use id: "ID" in your case. Moreover you don't need to include in jsonReader the properties which value are default (root:"rows", page: "page") and so on.
    3. The next problem is the usage of unconditional reloadGrid inside of loadComplete. You should understand that loadComplete will be executed on every reloading of the grid (event on local reloading). So it would be wrong making permanent reloading of grid. Moreover usage of reloadGrid inside of loadComplete is bad from another point of view. If you trigger reloadGrid the event will be executed * immediately*, but the grid is not in processing of previous loading. So it would be more correct to trigger reloading inside of setTimeout with some small time interval like 50 ms.
    4. The last recommendation is usage of K&R (Kernighan and Ritchie's) style of writing of the code. It's not so important which style of formatting of the code you use in another computer language and it's not important which one you personally find the most nice to read. JavaScript has it's own rights. One from there is automatic semicolon insertion (see here for example). If you follow K&R you will never have any problems with automatic semicolon insertion.
    5. I recommend you to use height: "auto" if you need display not so much rows and use column templates which could reduce the size of your code and simplify its management.

    After described above changing the could will be something like below

    var myFloatTemplate = { width: 80, align: "right", sorttype: "float" };
    
    $("#CompTable").jqGrid({
        url: "BomExplosionInJsonObj.asp",
        datatype: "json",
        height: "auto",
        colNames: ["Part", "Description", "Src", "Std Usage", "Usage Inc Scrap", "Rate Scrap", "UOM", "Item", "Unit Cost", "Stock"],
        colModel: [
            {name: "COMP1_PART", width: 120},
            {name: "WSCOMPDESC", width: 300},
            {name: "WSCOMPSRC", width: 40},
            {name: "COMPUSAGE", template: myFloatTemplate},
            {name: "WSGROSSQTY", width: 120, template: myFloatTemplate},
            {name: "COMPRATE_SCRAP", width: 90, template: myFloatTemplate},
            {name: "COMPBASIC_UNIT", width: 60},
            {name: "COMP1_ITEM", width: 60},
            {name: "WSCOMPUNITCOST", template: myFloatTemplate},
            {name: "WSCOMPQTYSTOCK", template: myFloatTemplate}
        ],
        jsonReader: {
            repeatitems: false,
            id: "ID"
        },
        caption: "Bom Detail",
        rowNum: 10000,
        autoencode: true,
        loadonce: true,
        sortable: true,
        sortname: "COMP1_PART",
        //sortorder: "desc",
        loadComplete: function () {
            var $self = $(this);
            if ($self.jqGrid("getGridParam", "datatype") === "json") {
                setTimeout(function () {
                    $self.trigger("reloadGrid"); // Call to fix client-side sorting
                }, 50);
            }
        }
    });
    

    The corresponding demo is here. Local sorting works and it displays the following results

    enter image description here

    UPDATED: Starting with version 4.12.0 free jqGrid fork of jqGrid, which I develop, support new forceClientSorting: true option. It works in combination with loadonce: true option and allows first load all the data from the server response, then sort the data locally and only then to display page of the data. It makes the trick with reloading of grid inside of setTimeout, started in loadComplete, described in the answer, not needed. One need just replace the above loadComplete code to one additional option forceClientSorting: true. The option forceClientSorting: true have additional two benefits:

    1. One don't see any flicker after displaying of the first (unsorted) grid;
    2. The performance of the grid is better, especially if it have many rows, because displaying the grid is much slowly as sorting. Moreover the trick with described in the old answer displays the grid, then delete the content (which is slowly too) and then one displays the sorted grid once more.
    0 讨论(0)
  • 2020-12-17 04:28

    The code you posted shows that the sorting will be done on the server not on the client. Your jqGrid will post sord and sidx parameters to allow you to do so as part of returning the data to the jqGrid.

    Ex: C# MVC Controller code, where paging is also performed

    public ActionResult GetGridData(string sidx, string sord, int page, int rows, bool _search, string filters)
    {
     ...
     var pagedQuery = wholeDataSet.OrderBy(sidx + " " + sord).Skip((page - 1) * rows).Take(rows).ToList();
     ...
    
    0 讨论(0)
提交回复
热议问题