2

Would it at all be possible to show the current applied filter parameter as a descriptive string appended to the main title in the title bar

i.e.

"Customer - [ Field1 = 'ABC' and Field2 = 'CDE' ]"

Can't find an event that could be hooked onto once a filter is applied?

Scott Evernden
  • 39,136
  • 15
  • 78
  • 84
Jan de Jager
  • 870
  • 2
  • 13
  • 35

2 Answers2

3

I find that you do not really need to have an event after the search. At each search request, the data in the grid will be reloaded. So you can use events like beforeRequest, loadBeforeSend, serializeGridData, gridComplete or loadComplete.

From your title example I suppose that you use "Advanced Searching". I want only to demonstrate the main idea of the possible solution, so I chose loadComplete to implement the capture change:

jQuery('#list').jqGrid({
    // ...
    loadComplete: function(data) {
        var postData = jQuery('#list').getGridParam("postData");
        var newCapture = "Title";
        if (postData._search === true && typeof postData.filters !== "undefined") {
            var filters = jQuery.parseJSON(postData.filters);
            newCapture = "Title: [";
            var rules = filters.rules;
            for (var i=0; i<rules.length; i++) {
                var rule = rules[i];
                var op = rule.op;  // the code name of the operation
                if (jQuery.fn.searchFilter && jQuery.fn.searchFilter.defaults &&
                    jQuery.fn.searchFilter.defaults.operators) {
                    // find op description 
                    var operators = jQuery.fn.searchFilter.defaults.operators;
                    for (var j=0; j<operators.length; j++) {
                        if (operators[j].op === rule.op) {
                            op = operators[j].text;
                            //op = $.jgrid.search.odata[j];
                            break;
                        }
                    }
                }
                newCapture += rule.field + " " + op + " '" + rule.data + "'";
                if (i+1 !== rules.length)
                    newCapture += ", ";
            }
            newCapture += "]";
        }
        jQuery('#list').setCaption(newCapture);
    }
});

If you don't use "Advanced Searching" you should use searchField, searchOper and searchString instead of filters to build the grid title based on the search criteria.

Currently in the example I don't use localized names for operation, but it's clear that one can do this.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • @Oleg - great solution. I don't understand you last comment on how i can include the toolbar searching criteria in the title bar. it seems like toolbar search just added the actual field and value to the post data. So if i am searching on a field called Description for "test", i just see an element in the postdata called Description with a value of "test". I dont see any "searchField", "searchOper" and "searchString". also, how would you support multiple toolbar column searches ?? – leora Mar 11 '11 at 11:15
  • @Oleg - any thoughts on my above comment ?? – leora Mar 11 '11 at 18:08
  • @ooo: The current code place the text "Title: [...] in the grid capture. The text "Title" is in English, so it is not localized. The texts inside of `[...]` contains localizes texts like "beginnt mit" in German instead of "begins with" in English. In the last sentence I explain that one can write code that choose "Title" also corresponds to the language. Additionally instead of `rule.field` one can use the column header of the corresponding column. If you include the code in one grid you will see what I mean. – Oleg Mar 11 '11 at 19:11
  • @ooo: If one uses [Single Field Searching](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:singe_searching) instead of [Advanced Searching](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:advanced_searching) the properties `searchField`, `searchOper` and `searchString` will be used inside of `postData` instead of `filters`. The code from the example work only with "Advanced Searching". – Oleg Mar 11 '11 at 19:17
  • @Oleg - i dont think you understood me . i want to show in the title bar filtering for both Advance searching and TOOLBAR filterbar searching. i dont see anything around searchField when i use toolbar filtering. I just see my fields being added to the postdata [field:value] – leora Mar 11 '11 at 19:22
  • 1
    @ooo: I supposed that you asked the question about my answer which you commented, but you ask new questions. To your first question "I dont see any "searchField", "searchOper" and "searchString". also, how would you support multiple toolbar column searches". You can define only **one search operation** ("searchOper") per column in the toolbar searching with respect of `sopt` of [searchoptions](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:search_config#colmodel_options). You can not define more then one searching criteria per column. – Oleg Mar 11 '11 at 19:31
  • @ooo: To your last question: If the `postdata` will not field you make some error. Both toolbar searching and Advance searching use abd share the same `postData`. If something work not like you think you should better include the demo which reproduce your problem. – Oleg Mar 11 '11 at 19:34
  • @Oleg - maybe i am not setting something up right but today to get the advanced rules, i look at the filters section of the querystring but for toolbar i have to do a Request.Params[fieldname] so i need to check the field specifically as the "key" in the querystring. are you saying that there IS a way to have toolbar filtering show up inside the filter section. When i say multiple i am just referring to having a toolbar filtering on multiple columns (Not multiple filters on the same column) – leora Mar 11 '11 at 19:37
  • @Oleg: Can you help me with [this question](http://stackoverflow.com/questions/8501679/jqgrid-auto-filter-highlighting-search-result)? – AabinGunz Dec 14 '11 at 09:08
  • @Oleg: Thank you very much! I do not understand why this is not the chosen answer. It works perfectly and matches with the question! THX – Larry Feb 24 '12 at 18:44
0

Try hooking into the afterShowSearch event, where you would update the grid header. From the docs:

This event fires (if defined) every time after the search dialog is shown

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284