0

I have an array from which i need to filter the JQGrid.

var filter = ["a","b","c","d",...255];
var postData = $('jqGridName').jqGrid('getGridParam', 'postData');
jQuery.extend(postData,
{
    filters: {
        groupOp: "AND",
        rules: [
            { field: "Types", op: "ne", data: filter[0] },
            { field: "Types", op: "ne", data: filter[1] },
            { field: "Types", op: "ne", data: filter[2] },
            { field: "Types", op: "ne", data: filter[3] },
            .
            .
            .
            { field: "Types", op: "ne", data: filter[255] },
        ]
    },
});

The number of item in the array is not fixed. But maximum it can contain is 255. So do I need to write till 255 (as above) or is there any simple way to achieve the same?

Regards,

Varun R

Varun R
  • 95
  • 1
  • 8
  • You can use loop for filling the `rules` array. If you need to send the filter to the server you can consider to use `"ni"` operation ("is not in") instead of `"ne"` ("not equal"). It's important that the server "understand" the operation. – Oleg Feb 19 '15 at 07:38
  • I am not sending it to the server. LoadOnce is set to true. I am filtering from the data that is loaded. – Varun R Feb 19 '15 at 09:21
  • if you don't send the data to the server then `loadonce: true` option will be ignored. The rules array which will be apply *locally* should work without any problems with 255 elements. Do you have some performance problem? How many rows of data you need to filter? – Oleg Feb 19 '15 at 09:33
  • With 255 elements it works very well. My only worry is what if the array size increases in future (based on new requirements). So just want to know if there is any better way to achieve this. – Varun R Feb 19 '15 at 09:59
  • There are no other way in the current version of jqGrid. I plan implement the possibility to define **custom** operation in the future. I'll post the implementation in [my jqGrid fork](https://github.com/free-jqgrid/jqGrid), but the feature will be not included in the version which I will soon publish in February. I think that I will implement the feature in the next version (in March/April). – Oleg Feb 19 '15 at 10:10
  • Thanks a lot for your support. – Varun R Feb 19 '15 at 10:22
  • I created [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Custom-filtering-searching-Operation), which describes the feature more detailed. I think about changing the syntax of parameters of `customSortOperations`. – Oleg Feb 20 '15 at 09:37

1 Answers1

5

I have to think permanently about the problem with custom sorting operation which I promised you (in comment) to implement in the future version of jqGrid from my fork. At the end I do implemented the feature now. I present it below.

The first demo uses Searching Toolbar and the second demo uses Advanced Searching. Both uses common jqGrid settings which allows to make custom searching operation on local data.

First of all new custom searching operation need be defined. I uses in the demo IN operation which allows to define one rule with multiple values. I use the custom operation in the column "tax". It allows to filter for multiple values divided by semicolon (;). The corresponding code looks like below

$("#grid").jqGrid({
    colModel: [
        { name: "tax", label: "Tax", width: 100, template: "number",
            //sopt contains CUSTOM operation "nIn"
            searchoptions: { sopt: ["nIn", "eq", "ne", "lt", "le", "gt", "ge", "in", "ni"] } },
        ...
    ],
    customSortOperations: {
        // the properties of customSortOperations defines new operations
        // used in postData.filters.rules items as op peroperty
        nIn: {
            operand: "nIN",        // will be displayed in searching Toolbar for example
            text: "numeric IN",    // will be shown in Searching Dialog or operation menu in searching toolbar
            title: "Type multiple values separated by semicolon (";") to search for one from the specified values",
            buildQueryValue: function (otions) {
                // the optional callback can be called if showQuery:true option of the searching is enabled
                return otions.cmName + " " + otions.operand + " (" + otions.searchValue.split(";").join("; ") + ") ";
            },
            funcName: "myCustomIn" // the callback function implements the compare operation
        }
    },
    myCustomIn: function (options) {
        // The method will be called in case of filtering on the custom operation "nIn"
        // All parameters of the callback are properties of the only options parameter.
        // It has the following properties:
        //     item        - the item of data (exacly like in mydata array)
        //     cmName      - the name of the field by which need be filtered
        //     searchValue - the filtered value typed in the input field of the searching toolbar
        var fieldData = options.item[options.cmName],
            data = $.map(
                options.searchValue.split(";"),
                function (val) {
                    return parseFloat(val);
                }
            );
        return $.inArray(parseFloat(fieldData), data) >= 0;
    }
});

As the result the operation "nIn" will be placed in op property of filters.rules in the same way like the standard "en" operation for example. jqGrid displays the operation as "nIN" in the searching toolbar (see the valeu of operand: "nIN" property). The property tooltip defines the tooltip displayed over the operation.

enter image description here

and one can filter the results like on the animated gif below

enter image description here

During the filtering jqGrid calls myCustomIn callback. So one is absolutely free how to implement the corresponding operation.

In the same way one can use Advanced Searching too:

enter image description here

UPDATED: The wiki article describes the new feature more detailed.

Oleg
  • 220,925
  • 34
  • 403
  • 798