获取所有未从jqGrid过滤的行 [英] Get all rows not filtered from jqGrid

查看:69
本文介绍了获取所有未从jqGrid过滤的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网格中有本地数据.用户使用过滤器工具栏后,如何获取所有未删除的行或ID?无论分页如何,我都需要获取所有过滤的行.

I have local data in a grid. How can I get all of the rows or IDs that are not removed after a user uses the filter toolbar? I need to get all filtered rows, regardless of pagination.

例如,假设我从网格中的50行开始.用户使用过滤器工具栏,行的集合减少到10行.我怎么能得到那十行呢?

For example, say I begin with 50 rows in the grid. The user uses the filter toolbar and the set of rows decreases to 10 rows. How can I get those ten rows?

推荐答案

没有直接的方法来获取所需的信息. jqGrid内部使用 $ .jgrid.from 过滤本地数据.在$.jgrid.from的主要代码noreferrer> addLocalData .为了在不学习所有代码的情况下获得所需的结果,我建议使用所有已过滤的数据将由$.jgrid.fromselect方法返回的事实(请参阅

There are no direct way to get the information which you need. Internally jqGrid uses $.jgrid.from to filter local data. The main code which uses $.jgrid.from in inside of addLocalData. To get results which you need without studying all the code I suggest to use the fact that all filtered data will be returned by select method of $.jgrid.from (see the line of code). My suggestion is to catch the data before the data will be cut to the page size.

为此,我建议使用子类:覆盖$.jgrid.from的方法select方法.我将在为答案

To do this I suggest to use sub-classing: overwriting of the method select method of $.jgrid.from. I demonstrate the technique in the examples created for the answer and this one.

在您的情况下,代码将是

In your case the code will be

var oldFrom = $.jgrid.from,
    lastSelected;

$.jgrid.from = function (source, initalQuery) {
    var result = oldFrom.call(this, source, initalQuery),
        old_select = result.select;
    result.select = function (f) {
        lastSelected = old_select.call(this, f);
        return lastSelected;
    };
    return result;
};

现在,变量lastSelected将保存元素数组,这些元素是最后一次排序或过滤操作的结果.因为$.jgrid.from是全局的,所以数据未连接到网格.如果页面上有多个网格,那将很不舒服.可以使用每个网格的loadComplate代码中的以下行来修复较小的缺点:

Now the variable lastSelected will save the array of elements which are results of the last sorting or filtering operation. Because $.jgrid.from is global the data are not connected to the grid. If you have more as one grid on the page it will be uncomfortable. One can fix the small disadvantage with the following line in the code of loadComplate of every grid:

loadComplete: function () {
    this.p.lastSelected = lastSelected; // set this.p.lastSelected
}

通过这种方式,我们引入了新的jqGrid参数lastSelected,该参数与data参数具有紧密的结构,但仅保存最后过滤的数据.

In the way we introduce new jqGrid parameter lastSelected which will have close structure as data parameter, but will hold only last filtered data.

以下代码将在alert消息中显示已过滤数据的ID

The following code will display the ids of filtered data in alert message

$("#getIds").click(function () {
    var filteredData = $grid.jqGrid('getGridParam', 'lastSelected'), i, n, ids = [],
        idName = $grid.jqGrid('getGridParam', 'localReader').id;
    if (filteredData) {
        for (i = 0, n = filteredData.length; i < n; i++) {
            ids.push(filteredData[i][idName]);
        }
        alert("tolal number of filtered data: " + n + "\n" +
            "ids of filtered data:\n" + ids.join(', '));
    }
});

我使用了 localReader.id 参数,因为用于本地数据的属性名称通常为id_id_.如果使用loadonce: true选项,则从服务器加载数据时将使用_id_.

I used localReader.id parameter because property name used for local data are typically id or _id_. The _id_ will be used in case of data loaded from the server if one uses loadonce: true option.

演示演示了该方法.例如,如果一个过滤器仅过滤来自FedEx的数据,然后单击显示ID"按钮,则将看到有关所有过滤的信息,而不仅是有关当前页面上显示的数据的信息:

The demo demonstrate the approach. If one filter for example only the data from FedEx and then clicks on "Show Ids" button one will see information about all filtered and not only about the data displayed on the current page:

更新:免费jqGrid 提供了新的lastSelectedData选项.请参见演示. /github.com/free-jqgrid/jqGrid/blob/v4.8.0/README.md#some-demos-which-demonstrates-new-features"rel =" nofollow noreferrer>演示列表.

UPDATED: free jqGrid provides new lastSelectedData option. See the demo in the list of demos.

这篇关于获取所有未从jqGrid过滤的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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