0

I have a jqgrid with the following information:

$("#Table").jqGrid({
            url: 'u.json,
            loadonce:true,
            colNames: msd.rise.columnDisplayNames,
            colModel: msd.rise.colModelDef,
            gridview: true,
            toppager: false,
            sortname: 'sd',
            sortorder: 'desc',
            sortable:true,
            loadComplete: function(){
                $("#Table").setGridParam({datatype:'json', page:1}).trigger('reloadGrid');
            }
});

The client side sort doesn't work. I put loadonce:true and $("#Table").setGridParam({datatype:'json', page:1}).trigger('reloadGrid'); based of this answer. But it still doesn't work. Any idea?

amanda chaw
  • 137
  • 2
  • 14

1 Answers1

0

The code of loadComplete which you use currently is wrong. You should never use unconditional .trigger('reloadGrid') inside of loadComplete. Moreover the setting of datatype to 'json' before reloading will makes reloading of data from the server, but you need local reloading to apply sorting. The correct code will be like in the example from the anwer:

loadComplete: function () {
    var $self = $(this);
    if ($self.jqGrid("getGridParam", "datatype") === "json") {
        setTimeout(function () {
            $self.trigger("reloadGrid"); // Call to fix client-side sorting
        }, 50);
    }
}

The code makes reloading only once after the initial loading from the server will be finished.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much for the answer. I put that inside the code. With settimeout the gird doesn't load correctly (loading is always on top). When I removed setTimeout, it works (loading doesn't appear) but when I click sort, the grid doesn't load again. – amanda chaw Dec 09 '14 at 06:28
  • @amandachaw: Probably you have some additional problems in `colModel`. **Could you append your questing with `colModel` which you use and with an example of input data (1-2 rows of data will be enough). Which `datatype` you use?** The line with syntax error `url: 'u.json,` allows me to suppose that you use `datatype: "json"` which you not included in the posted code. If you do use default `datatype: "xml"` then you should change `loadComplete` to test for it: `if ($self.jqGrid("getGridParam", "datatype") === "xml") {`. – Oleg Dec 09 '14 at 06:38
  • I had rowNum: 0, I removed that and your code now working:) Thank you very much for the help. yes, the datatype is json. – amanda chaw Dec 09 '14 at 06:42