Search code examples
sortingjqgrid

How to set initial sort order as descending in jqGrid


I build the jqGrid table.

I want to sort the table desc as default when click the header columns.

firstsortorder: 'desc',
sortorder: 'desc',

When load the table, it's sorted by desc. But When I click the header column, it's sorted by asc at first time.

How can I change sort order into 'Desc'?


Solution

  • The option firstsortorder is a property which should be set in colModel items and not in grid options.

    To work this you will need to set it separatley to each item in colModel or use template to set it for all columns.

    $("#grid").jqGrid({
        // this will sort initially the grid
        sortorder : "desc",
        ...
        colModel : [ 
            // this will sort the clicked field to desc order
            { name : "some",..., firstsortorder : "desc",...}    
            ...
        ],
        ...
     });
    

    UPDATE If you like to have this option in all your grids without to set it in all of the colModel items you can use cmTemplate property which overwrite the default values in every item in colModel.

    The below code do the same without a need to write the property on every item. See cmTemplate property in the docs here

    $("#grid").jqGrid({
        // this will sort initially the grid
        sortorder : "desc",
        cmTemplate : { firstsortorder : "desc" },
        ...
        colModel : [ 
            // this will sort the clicked field to desc order
            { name : "some1",...}
            { name : "some2",...}
            ...
            { name : "somen",...}
        ],
        ...
     });