网格加载时在特定字段上对jqgrid进行默认排序

Bha*_*rle 1 jqgrid

我想加载一个默认排序的网格,它是一个字段.我通过向网格添加sortname和sortorder来完成此操作,但是当网格加载时,排序符号显示在该字段的标题上.但是当我单击分页下一个按钮而不是网格加载时,记录将被排序.

有谁知道它为什么不对电网负载进行排序?

@UPDATE:嗨,我正在使用我的数据类型作为XML和我的配置.如下:

jQuery("#userList").jqGrid({
    url: '<%=request.getContextPath()%>/userJqGrid?q=1&action=fetchData&userCookie=<%= encodedCookie%>',
    datatype: 'xml',
    mtype: 'GET',
    colNames:['<%= userProp.getProperty(userColNames[0])%>',
              '<%= userProp.getProperty(userColNames[1])%>',
              '<%= userProp.getProperty(userColNames[2])%>',
              '<%= userProp.getProperty(userColNames[3])%>',
              '<%= userProp.getProperty(userColNames[4])%>',
              '<%= userProp.getProperty(userColNames[5])%>'
    ],
    colModel:[
        {name:'<%= userColNames[0]%>',index:'<%= userColNames[0]%>',
            width:120,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:1, elmprefix:'*'}},
        {name:'<%= userColNames[1]%>',index:'<%= userColNames[1]%>',
            width:130,sortable:true,editable:true},
        {name:'<%= userColNames[2]%>',index:'<%= userColNames[2]%>',
            width:100,sortable:true,editable:true,editrules:{required:true},formoptions:{rowpos:3, elmprefix:'*'}},
        {name:'<%= userColNames[3]%>',index:'<%= userColNames[3]%>',
            width:180,sortable:true,editable:true,editrules:{email:true,required:true},formoptions:{rowpos:4, elmprefix:'*'}},
        {name:'<%= userColNames[4]%>',index:'<%= userColNames[4]%>',
            width:100,sortable:true,editable:true},
        {name:'<%= userColNames[5]%>',index:'<%= userColNames[5]%>',
            width:100,sortable:true,editable:true},
    ],
    pager:'#pager1',
    rowNum:'<%=appProp.getProperty("per_page_records")%>',
    height:'auto',
    viewrecords:true,
    loadonce:true,
    sortable:true,
    width:'100%',
    gridview: true,
    autowidth:true,
    shrinkToFit:false,
    ignoreCase:true,
    editurl:'<%=request.getContextPath()%>/userJqGrid?q=1&action=addData&userCookie=<%=encodedCookie%>',
    caption: 'User Records',
    sortname:'<%=userColNames[14]%>',
    sortorder:'asc',
    onSelectRow: function (id){
        checkAndSetCookie();
    },
    onSortCol : function(){
        checkAndSetCookie();
    },
    onPaging : function(){
        checkAndSetCookie();
    },
    onSearch : function(){
        checkAndSetCookie();
    },
    loadComplete: function(){
        checkAndSetCookie();
    }
});
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 5

如果使用远程datatype(datatype: 'xml'datatype: 'json')服务器负责在第一次加载时对数据进行排序.如果您使用loadonce: true并且只想在客户端对数据进行排序,则必须在第一次加载后直接重新加载jqGrid.相应的代码可以是以下内容

loadComplete: function (data) {
    var $this = $(this),
        datatype = $this.getGridParam('datatype');

    if (datatype === "xml" || datatype === "json") {
        setTimeout(function () {
            $this.trigger("reloadGrid");
        }, 100);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新: 免费的jqGrid fork有选项forceClientSorting: true,可以与loadonce: true选项结合使用.该选项forceClientSorting: true强制客户端排序和过滤.它使得答案中描述的代码不再需要.