问题
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"}
]
}
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.
I want to use as much as possible of default jqGrid
functionality, but didn't find anything. Any hints are appreciated.
UPDATE 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 :))
So, main idea:
We modify filter values to a well-known
rules
andgroups
.We go through all the columns, remove
rules
andgroups
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 frompostData
and subset grid data by plainjavascript
. You can see this ingetDistinctColumnValues
and I want to use some grid filtering possibilities to make it work the same way as all theopts
working when we make filtering/searching withpostData
set.
Also, I've seen some internal function to get filters as a sql
-where - this also would be nice for backend prototyping.
回答1:
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.
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.
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.
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.
Look at https://jsfiddle.net/OlegKi/wqxyo579/25/, which used
colModel: [
{ name: 'name' },
{ name: 'surname' },
{ name: 'age' },
],
cmTemplate: {
width: 100,
autoResizable: true,
stype: 'select',
searchoptions: {
generateValue: true,
noFilterText: "(All)"
}
},
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.
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.
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.
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.
来源:https://stackoverflow.com/questions/57757202/how-to-filter-jqgrid-data-but-not-the-grid-itself