jqGrid在第一次加载时排序 [英] jqGrid sort on first load

查看:141
本文介绍了jqGrid在第一次加载时排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网格具有以下代码(我在与数据源相同的目录中使用XML文件).

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:名称",
  • sortorder:"asc",
  • sortable:是,

单击各列后,它可以正常工作,只是第一种排序不起作用(加载页面后).

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

推荐答案

如果您使用"xml"或"json"之类的远程数据类型,则服务器负责提供排序后的数据.

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

如果无法执行此操作,则可以触发loadComplete内部的reloadGrid,但是您应该使用setTimeout JavaScript方法来完成第一次加载过程.

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.

要没有递归,应将"reloadGrid"放在loadCompleteif (data.nodeType)块内.

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

更新:免费jqGrid 具有选项forceClientSorting: true,解决了这个问题.该选项允许强制排序和过滤数据(如果设置了可选的postData.filters),则在显示 之前.

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.

这篇关于jqGrid在第一次加载时排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆