Sort not working with jqGrid

前端 未结 3 2029
梦谈多话
梦谈多话 2020-12-10 19:18

I\'ve been having problems getting jqGrid to sort. I\'d like to preferable do this sorting on the client, but I\'m also willing to make a new call to the database to get th

相关标签:
3条回答
  • 2020-12-10 19:44

    If you load unsorted data from the server and want just sort local data once you should not place jQuery("#grid").trigger("reloadGrid"); inside of loadComplete. The callback loadComplete will be called on every sorting or paging of local data too. Moreover it would be better to call jQuery("#grid").trigger("reloadGrid"); inside of setTimeout. In the case the full first loading of the grid will be finished before reloading.

    I don't tested, but I suppose the correct code of loadComplete could be about the following

    loadComplete: function () {
        var $this = $(this);
        if ($this.jqGrid('getGridParam', 'datatype') === 'json') {
            if ($this.jqGrid('getGridParam', 'sortname') !== '') {
                // we need reload grid only if we use sortname parameter,
                // but the server return unsorted data
                setTimeout(function () {
                    $this.triggerHandler('reloadGrid');
                }, 50);
            }
        }
    }
    

    In the case the reloadGrid will be called only once at the first load of the grid from the server. At the next call the value of datatype option will be already 'local'.

    UPDATED: Free jqGrid is the fork of jqGrid, which I develop starting with the end 2014. It has many new features. One can use the option forceClientSorting: true to force sorting and filtering of data on the client side before the current page of data will be displayed in jqGrid. Thus one can just add forceClientSorting: true option and remove the trick, described in the old answer.

    0 讨论(0)
  • 2020-12-10 19:47

    loadonce only works with the predefined loaders. If you use datatype as function you should set datatype:local manually after the first loading of the grid with the custom function.

    Try something like this:

    datatype : function (){
        $.ajax({
        …
        complete :function (…){
                    …
                    $("#mygrid").setGridParam({datatype:'local'});
            }
        })
    },
    
    0 讨论(0)
  • 2020-12-10 19:51

    Found one solution, though not entirely sure why this works. Perhaps someone can provide a better answer.

    var x = $("#grid").jqGrid({
        jsonReader: { root: "rows", repeatitems: false },
        datatype: "json",
        height: 'auto',
        autowidth: true,
        forceFit: true,
        colNames:['ID','Name'],
        colModel:[
            {name:'id', key:true, index:'id', width:60, sorttype:"int", jsonmap:"id"},
            {name:'name', index:'name', width:90,  jsonmap: "name"}
        ],
        caption: "Results",
    
        //Required for client side sorting
        loadonce: true,
        gridComplete: function(){ 
          $("#grid").setGridParam({datatype: 'local'}); 
        }
    
    0 讨论(0)
提交回复
热议问题