【问题标题】:Default sorting of jqgrid on particular field when grid loads网格加载时特定字段上 jqgrid 的默认排序
【发布时间】:2011-11-14 08:09:16
【问题描述】:

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

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

@更新: 嗨,kees,我使用我的数据类型作为 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();
    }
});

【问题讨论】:

  • 您使用哪个数据后端?那么你的数据是如何加载的呢? JSON、本地数组等?您能否在此处发布您的 jqGrid 配置,以便我们为您提供帮助。
  • 嗨,我已经添加并更新了我的配置。
  • 如果您使用 FireBug(控制台)检查 URL 中是否有 sidx 和 sord 参数?
  • 是的,网址中存在 idx=company_id&sord=asc,当我点击分页下一步按钮时,排序完成。

标签: jqgrid


【解决方案1】:

如果您使用远程datatypedatatype: '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);
    }
}

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

【讨论】:

  • @Bhagwat:不客气!如果问题解决了可以"accept"回答。
猜你喜欢
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
  • 2014-11-07
相关资源
最近更新 更多