Justin Levene
Justin Levene

Reputation: 1677

jqGrid how to apply custom filtering / search

I have a jqGrid with a filterToolbar. On one of my columns I want to do a search where for the "filter test", my custom function is called and obviously returns a true for yes it passed of false for no.

Can jqGrid do this, if so, how?

Upvotes: 0

Views: 1271

Answers (1)

Justin Levene
Justin Levene

Reputation: 1677

Using the free fork of jqGrid you can use the following

in your colModel add the value searchoptions as so:

{name:'FIELD',width:120,searchoptions:{ sopt: ["cust"] },label:"Field name"}

then add the following property to jqGrid

customSortOperations: {
    // the properties of customSortOperations defines new operations 
    // used in postData.filters.rules items as op peroperty
    cust:{
           operand: "Custom",// will be displayed in searching Toolbar for example
           text: "Custom",    // will be shown in Searching Dialog or operation menu in searching toolbar

           filter: function (options) {
                        // The method will be called in case of filtering on the custom operation "cust"
                        // 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

                        // Get cell data as lowercase
                        var fieldData = options.item[options.cmName].toLowerCase(),
                        // Get search terms all in lowercase
                        terms = $.map(options.searchValue.split(" "), function(val){ return $.trim(val).toLocaleLowerCase(); }),
                        // A match found
                        match = false;
                        // Loop through terms and see if there is a match in the row cell data
                        $.each(terms, function(i,v)
                        {
                            if(fieldData.indexOf(v) != -1)
                            {
                                match = true;
                                // Quit the each loop
                                return false;
                            }
                        });
                        return match;
                    }
                }
            },

Upvotes: 2

Related Questions