排序不使用jqGrid [英] Sort not working with jqGrid

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

问题描述

我一直遇到让jqGrid排序的问题。我希望最好在客户端上进行这种排序,但我也愿意对数据库进行新的调用以获得排序结果。

I've been having problems getting jqGrid to sort. I'd like to preferable do this sorting on the client, but I'm also willing to make a new call to the database to get the sorted results as well.

我可以点击列标题,排序箭头更改方向,但数据根本不会改变。

I can click on the column headers and the sort arrows change directions, however the data does not change at all.

我查看了这个问题,但是调用reloadGrid似乎没什么帮助。

I've looked over this question, however calling reloadGrid didn't seem to help.

我的整个网格如下:

var x = $("#grid").jqGrid({
    jsonReader: { root: "rows", repeatitems: false },
    datatype: "json",
    height: 'auto',
    autowidth: true,
    forceFit: true,
    colNames:['ID','Name'],
    colModel:[
        {name:'id', key:true, index:'id', width:60, sorttype:"int", jsonmap:"id"},
        {name:'name', index:'name', width:90,  jsonmap: "name"}
    ],
    caption: "Results",
    loadonce: true,
    sortable: true,
    loadComplete: function() {
        jQuery("#grid").trigger("reloadGrid"); // Call to fix client-side sorting
    }
});

//This data comes from a web service call, hard coding in to test
var jsonData = [
    {id: 1, name: 'Apple'},
    {id: 2, name: 'Banana'},
    {id: 3, name: 'Pear'},
    {id: 4, name: 'Orange'}
];

x[0].addJSONData( { rows: jsonData } );


推荐答案

如果从服务器加载未排序的数据并且只想要排序本地数据一次你不应该在 jQuery(#grid)。trigger(reloadGrid); > loadComplete 。回调 loadComplete 也将在每次本地数据的排序或分页上调用。此外,最好在 setTimeout jQuery(#grid)。trigger(reloadGrid); >。如果网格的完整第一次加载将在重新加载之前完成。

If you load unsorted data from the server and want just sort local data once you should not place jQuery("#grid").trigger("reloadGrid"); inside of loadComplete. The callback loadComplete will be called on every sorting or paging of local data too. Moreover it would be better to call jQuery("#grid").trigger("reloadGrid"); inside of setTimeout. In the case the full first loading of the grid will be finished before reloading.

我没有测试,但我想正确的代码 loadComplete 可以是以下

I don't tested, but I suppose the correct code of loadComplete could be about the following

loadComplete: function () {
    var $this = $(this);
    if ($this.jqGrid('getGridParam', 'datatype') === 'json') {
        if ($this.jqGrid('getGridParam', 'sortname') !== '') {
            // we need reload grid only if we use sortname parameter,
            // but the server return unsorted data
            setTimeout(function () {
                $this.triggerHandler('reloadGrid');
            }, 50);
        }
    }
}

如果 reloadGrid 将在服务器首次加载网格时被调用一次。在下次调用时, datatype 选项的值将已经'local'

In the case the reloadGrid will be called only once at the first load of the grid from the server. At the next call the value of datatype option will be already 'local'.

更新: 免费jqGrid 是分叉jqGrid,我从2014年底开始开发。它有许多新功能。可以使用选项 forceClientSorting:true 强制在客户端上排序和过滤数据当前页面的数据将显示在jqGrid中。因此,可以添加 forceClientSorting:true 选项并删除旧答案中描述的技巧。

UPDATED: Free jqGrid is the fork of jqGrid, which I develop starting with the end 2014. It has many new features. One can use the option forceClientSorting: true to force sorting and filtering of data on the client side before the current page of data will be displayed in jqGrid. Thus one can just add forceClientSorting: true option and remove the trick, described in the old answer.

这篇关于排序不使用jqGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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