Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jqGrid sort on first load

I have the following code for my grid (I use an XML file in the same directory as data source).

var handsetGrid = $("#products").jqGrid({
    url: 'catalog.xml',
    datatype: "xml",
    colNames:["SKU", "Name", "Brand", "Description", "Metadescription"],
    colModel:[
        {name:"sku",  key: true, index:"sku", width:100, xmlmap:"sku", align:"right", sortable:true},
        {name:"Name", index:"Name", width:300, sortable:true, xmlmap:">name>en"},
        {name:"Brand", index:"Brand", width:100, sortable:true, xmlmap:"brand"},
        {name:"description", index:"description", width:400, classes:"desc1", xmlmap:"description1>en", formatter:descFormatter},
        {name:"metadescriptionEn", index:"metadescriptionEn", width:400, classes:"desc1", xmlmap:"metadescription>en", formatter:descFormatter}
    ],
    width: 1300,
    height:480,
    shrinkToFit:false,
    rownumbers: true,
    scroll: true,
    rowNum:22,
    ignoreCase: true,
    viewrecords: true,
    sortname: "Name",
    sortorder: "asc",
    sortable: true,
    loadonce: true,
    pager: "#pager",
    caption: "Handsets",                    
    xmlReader: {
        root: "products",
        row: "product",
        repeatitems: false,
        id: "sku"               
    },
    loadComplete: function(data) {
        // test whether we have initial loadind and the "data" has XML type
        if (data.nodeType) {
            myXMLdata = data; // save original XML data                         
        }
    },
    subGrid: true,
    subGridRowExpanded: function(subgrid_id, row_id) {
        var subgrid_table_id;
        subgrid_table_id = subgrid_id + "_t";
        jQuery("#" + subgrid_id).html("<table id='" + subgrid_table_id + "' class='scroll'></table>");
        jQuery("#" + subgrid_table_id).jqGrid( {
            datatype:'xmlstring',
            datastr: myXMLdata,
            colNames: [ 'Id', 'Name', 'Duration', 'Price'],
            colModel: [
                {name:"ppid",index:"ppid",width:80, xmlmap:">id"},
                {name:"ppname",index:"ppname",width:150, xmlmap:">name>en"},
                {name:"ppduration",index:"ppduration",width:85, xmlmap:">priceperduration>duration>en", formatter: durationFormatter},
                {name:"ppprice",index:"ppprice",width:80, xmlmap:">priceperduration>price", formatter: priceFormatter}
            ],
            gridview: true,
            xmlReader: {
                root: "products>product:has('sku:contains('"+row_id+"')')>priceplansavailable",
                row: "priceplan",
                repeatitems: false
            }                           
        });
    }       

}); 

$("#handsets").jqGrid('navGrid','#pager',{edit:false,add:false,del:false,search:false,refresh:false});
$("#handsets").jqGrid('navButtonAdd',"#pager",{caption:"Search Bar", title:"Toggle Search Toolbar", buttonicon :'ui-icon-pin-s',
    onClickButton:function(){
        handsetGrid[0].toggleToolbar();
    }
});

$("#handsets").jqGrid('navButtonAdd',"#pager",{caption:"Clear", title:"Clear Search", buttonicon :'ui-icon-refresh',
    onClickButton:function(){
        handsetGrid[0].clearToolbar();
    }
});

$("#handsets").jqGrid('filterToolbar', {defaultSearch:'cn'});

My problem is when I load the grid I want it to be already sorted for the column : Name. I expected to be working with these three parameters :

  • sortname: "Name",
  • sortorder: "asc",
  • sortable: true,

After clicking on the columns it is working properly, just the first sort is not working (after loading the page).

like image 729
рüффп Avatar asked Dec 21 '22 15:12

рüффп


1 Answers

If you use remote datatype like "xml" or "json", the server is responsible to provide the sorted data.

If you cannot do this, you can trigger the reloadGrid inside of loadComplete, but you should use setTimeout JavaScript method to allow the complete the first loading process.

To have no recursions you should place "reloadGrid" inside of if (data.nodeType) block of loadComplete.

UPDATED: Free jqGrid have the option forceClientSorting: true, which solves the problem. The option allows to force sorting and filtering of data (if optional postData.filters is set) before the first page will be displayed.

like image 69
Oleg Avatar answered Dec 24 '22 05:12

Oleg