如何过滤jqGrid数据而不是网格本身 [英] How to filter jqGrid data but not the grid itself

查看:103
本文介绍了如何过滤jqGrid数据而不是网格本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所见, free-jqgrid 使用

As far as I see, free-jqgrid uses jlinq to filter and search data. I am making my own filters, that are using grids postData in it's well-known format:

{ "groupOp": "AND",
      "groups" : [
        { "groupOp": "OR",
            "rules": [
                { "field": "name", "op": "eq", "data": "England" },
                { "field": "id", "op": "le", "data": "5"}
             ]
        }
      ],
      "rules": [
        { "field": "name", "op": "eq", "data": "Romania" },
        { "field": "id", "op": "le", "data": "1"}
      ]
}

此数据在内部转换为jlinq查询. 是否有任何公共功能来获取grid的当前查询? 我还看到有一种私人的可能性来获得类似sql的格式.

This data converted internally to jlinq query. Are there any public functions to get current query for grid? I also see there is a private possibility to get it in sql-like format.

我想尽可能多地使用默认的jqGrid功能,但是什么也没找到.任何提示表示赞赏.

I want to use as much as possible of default jqGrid functionality, but didn't find anything. Any hints are appreciated.

更新2 :

以下是当前实现的小提琴.我需要采用它,所以那里有些丑陋的部分,但是您可以根据值进行过滤,以了解想法.首先,使用网格,导致代码很糟糕(这就是为什么我真的要更改它的原因:))

Here comes fiddle with current implementation. I needed to adopt it, so some ugly parts are there, but you can filter on values, to get the idea. First, play with the grid, cause code is awful (that's why I want to change it, really :))

所以,主要思想是:

  1. 我们将过滤器值修改为众所周知的rulesgroups.

我们遍历所有列,删除此列名的rulesgroups,然后过滤数据.这里是我的问题部分:我需要过滤数据,而不是网格,仅过滤数据以获得唯一值.现在,我根据postData和纯网格javascript的子集网格数据生成一个正则表达式.您可以在getDistinctColumnValues我想使用一些网格过滤功能中看到它,以使其与我们在设置postData进行过滤/搜索时所有opts工作方式相同.

We go through all the columns, remove rules and groups for this column name and we filter the data. Here comes my question part: I need to filter the data, but not the grid, just a data to get unique values. Right now I generate a regex from postData and subset grid data by plain javascript. You can see this in getDistinctColumnValues and I want to use some grid filtering possibilities to make it work the same way as all the opts working when we make filtering/searching with postData set.

此外,我已经看到一些内部函数来将过滤器作为sql获取-在这里,这对于后端原型设计也将是很好的.

Also, I've seen some internal function to get filters as a sql-where - this also would be nice for backend prototyping.

推荐答案

对不起,但是我不确定您需要实现什么.您可以获取lastSelectedData参数以具有基于当前过滤器的过滤和排序数据数组.完整数据可通过lastSelectedData参数获得. postData参数的filters属性为您提供当前的过滤器.

Sorry, but I'm not sure what you need to implement. You can get lastSelectedData parameter to have the array of filtered and sorted data, based on the current filter. The full data are available via lastSelectedData parameter. The filters property of postData parameter gets you the current filter.

您问是否有任何公共功能可以获取当前的网格查询?".您可以使用getGridParam方法获取当前查询.我想,您想访问jlinq附近的一些内部结构,但是它实际上没有任何实用价值.您可以通过$.jgrid.from访问该类,但这并没有真正的帮助.实际的人只需要在postData.filters参数中设置新的过滤器,将search参数设置为true并调用.trigger("reloadGrid")即可用新数据重新加载网格.

You asked "are there any public functions to get current query for grid?". You can use getGridParam method to get current query. I guess, that you want to get access to some internal structures close to jlinq, but it gives really no practical value. You can access the class via $.jgrid.from, but it's not really helpful. Practical one needs only to set new filter in postData.filters parameter, set search parameter to true and call .trigger("reloadGrid") to reload grid with new data.

如果您需要自己过滤或排序一些数据,则数组方法filtersort会更有效.如果以上信息不能帮助您解决问题,请在问题后附加其他信息,该信息会在示例中更详细地描述问题,并给我提供一些评论.

If you need to filter or to sort some data yourself, then array methods filter and sort will be more effective. If the above information don't help you to solve your problem, please append your question with additional information, which describes the problem more detailed on an example and post me small comment.

已更新:我仍然不确定我是否完全了解您要实现的目标.在我看来,如果您使用一些免费的jqGrid选项,则可以删除JSFiddle演示中大多数功能.

Updated: I'm still not sure that I exactly understand what you want to implement. It seems to me that the most functionality of your JSFiddle demo can be removed if you use some free jqGrid options.

查看 https://jsfiddle.net/OlegKi/wqxyo579/25/ ,它使用了

colModel: [
    { name: 'name' },
    { name: 'surname' },
    { name: 'age' },
],
cmTemplate: {
    width: 100,
    autoResizable: true,
    stype: 'select',
    searchoptions: {
        generateValue: true,
        noFilterText: "(All)"
    }
},

它在每列上设置一些默认属性.从版本4.14.0开始,免费的jqGrid支持searchoptionsgenerateValue: true属性用于具有stype: 'select'的列(请参见).如果使用stype: 'select', searchoptions: { generateValue: true },则filterToolbar使用索引自动构建<select>元素. jqGrid在内部使用oSv = $("#list")[0].generateValueFromColumnIndex(cmName, sep, delim);(请参阅

It sets some default property on every column. Starting with version 4.14.0 free jqGrid supports generateValue: true property of searchoptions for columns having stype: 'select' (see here). Internally it works like another option of colModel: createColumnIndex: true, which generates the map from unique values in all columns having createColumnIndex: true. One can use getUniqueValueFromColumnIndex method to get the index for any column (like var indexName = $("#list").jqGrid("getUniqueValueFromColumnIndex", "name");). If one uses stype: 'select', searchoptions: { generateValue: true }, then filterToolbar build automatically <select> elements using the index. jqGrid uses internally oSv = $("#list")[0].generateValueFromColumnIndex(cmName, sep, delim); (see here). As the result, one can save a lot of customization using the feature.

唯一的事情,在使用createColumnIndex: truegenerateValue: true的情况下不应忘记:索引将在加载或重新加载数据后建立.因此,在填充数据后,应该呼叫filterToolbar .如果您根据loadonce: true选项从服务器加载数据,则应在loadCompleted回调内部更好地调用filterToolbar(如演示中一样).如果直接加载本地数据,则不需要.填写数据后,只需调用filterToolbar .

The only thing, which one should don't forget in case of using createColumnIndex: true or generateValue: true: the index will be build after the data is loaded or reloaded. So one should call filterToolbar after filling the data. If you load the data from the server with respect of loadonce: true option, then you should call filterToolbar better inside of loadCompleted callback (like in the demo). In case of direct loading of local data it's not needed. Just call filterToolbar after filling the data.

另一种替代方法是使用<datalist>而不是<select>.它允许使用过滤器工具栏中的<input>,但具有接近select或select2的功能.请参阅 https://jsfiddle.net/OlegKi/wqxyo579/24/ createColumnIndex: true, searchoptions: { sopt: [ "cn", "eq", "bw", "ew", "bn", "nc", "en" ], clearSearch: true, generateDatalist: true }.数据列表在不同的Web浏览器中以略有不同的方式实现,它们具有一些缺点,但是它是 native 实现的功能,因此可以非常快速地工作.可以将其与网格中的1000个唯一值和10000行一起使用(在这种情况下,我强烈建议使用本地数据分页,并使用10-25页大小).包含1000个元素的数据列表仍将具有良好的性能,例如,与select2相比要好得多.

Another alternative would be to use <datalist> instead of <select>. It allows to use <input> in the filter toolbar, but have functionality close to select or select2. See https://jsfiddle.net/OlegKi/wqxyo579/24/, where I used createColumnIndex: true, searchoptions: { sopt: [ "cn", "eq", "bw", "ew", "bn", "nc", "en" ], clearSearch: true, generateDatalist: true }. Datalists are implemented in different web browsers in a little different way and they have some disadvantages, but it's native implemented feature and so it works very quickly. One can use it with for example 1000 unique values and 10000 rows in the grid (I'd strictly recommend to use local data paging in the case and to use page size 10-25). Datalists with 1000 elements will still have good performance, much better as select2 for example.

最后的评论.我看到您构建了colModelIndexesByNames来按名称查找列.内置参数iColByName已存在于免费jqGrid中,并在内部使用.如果p是对jqGrid(var p = $("#list").jqGrid("getGridParam"))的参数的引用,则p.iColByName是映射,它按列名获取列索引,而p.colModel[p.iColByName.name]将表示colModel中的项目,它对应于"name"

Final remark. I see that you built colModelIndexesByNames to find column by name. The built-in parameter iColByName already exist in free jqGrid and be used internally. If p is reference to parameters of jqGrid (var p = $("#list").jqGrid("getGridParam")), then p.iColByName is the map, which gets column index by column name and p.colModel[p.iColByName.name] will represent the item in colModel, which corresponds "name" column.

这篇关于如何过滤jqGrid数据而不是网格本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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