网格加载时特定字段上 jqgrid 的默认排序 [英] Default sorting of jqgrid on particular field when grid loads

查看:12
本文介绍了网格加载时特定字段上 jqgrid 的默认排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to load a grid with default sorting on it's one of field. I done this by adding sortname and sortorder to my grid,but when grid is loaded sorting sign is shown on a header of that field.But records are sorted when i click on pagination next button not on grid load.

有谁知道为什么它不在网格负载上排序?

Is anyone know why it is not sorting on grid load?

@更新:kees,我使用我的数据类型作为 XML 和我的配置.如下:

@UPDATE: hi kees, I am using my datatype as XML and my config. is as follows:

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();
    }
});

推荐答案

如果你使用远程datatype (datatype: 'xml' or datatype: 'json') 服务器负责在第一次加载时对数据进行排序.如果您使用 loadonce: true 并希望仅在客户端对数据进行排序,则必须在第一次加载后直接重新加载 jqGrid.对应的代码大概如下

If you use remote datatype (datatype: 'xml' or datatype: 'json') the server is responsible for sorting of the data at the first load. If you use loadonce: true and want to sort the data only on the client side you have to reload jqGrid directly after the first loading. The corresponding code could be about the following

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

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

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

UPDATE: Free jqGrid fork has the option forceClientSorting: true, which can be used in combination with loadonce: true option. The option forceClientSorting: true force client side sorting and filtering. It makes the code described in the answer unneeded.

这篇关于网格加载时特定字段上 jqgrid 的默认排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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