4

I use the Toolbar Searching. Can you do a search for multiple words for a column? The delimiter is a space and the search should be done using the Like operator. As a result, the search should return all rows that have met all the words in the search string, no matter in what order they go in a field of row. For example there is a column "Product Name", I want to find all rows that have product name contains the word "lever" and contains the word "left."

Dmitriy Luchkin
  • 41
  • 1
  • 1
  • 3

1 Answers1

12

An interesting question!

I created the demo which demonstrate how to implement multi-word searching:

enter image description here

The corresponding code is:

$grid.jqGrid('filterToolbar', {
    stringResult: true,
    defaultSearch: "cn",
    beforeSearch: function () {
        modifySearchingFilter.call(this, ' ');
    }
});

where modifySearchingFilter I defined in the way:

var modifySearchingFilter = function (separator) {
        var i, l, rules, rule, parts, j, group, str,
            filters = $.parseJSON(this.p.postData.filters);
        if (filters && typeof filters.rules !== 'undefined' && filters.rules.length > 0) {
            rules = filters.rules;
            for (i = 0; i < rules.length; i++) {
                rule = rules[i];
                if (rule.op === 'cn') {
                    // make modifications only for the 'contains' operation
                    parts = rule.data.split(separator);
                    if (parts.length > 1) {
                        if (typeof filters.groups === 'undefined') {
                            filters.groups = [];
                        }
                        group = {
                            groupOp: 'OR',
                            groups: [],
                            rules: []
                        };
                        filters.groups.push(group);
                        for (j = 0, l = parts.length; j < l; j++) {
                            str = parts[j];
                            if (str) {
                                // skip empty '', which exist in case of two separaters of once
                                group.rules.push({
                                    data: parts[j],
                                    op: rule.op,
                                    field: rule.field
                                });
                            }
                        }
                        rules.splice(i, 1);
                        i--; // to skip i++
                    }
                }
            }
            this.p.postData.filters = JSON.stringify(filters);
        }
    };

UPDATE: Free jqGrid supports Custom filtering searching Operation, which makes very easy the implementation of such scenarios like above.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Why your function make this filter string filters {"groupOp":"AND","rules":[],"groups":[{"groupOp":"AND","groups":[],"rules":[{"data":"задний","op":"cn","field":"ent_name"},{"data":"аморт","op":"cn","field":"ent_name"}]}]} But Complex search generate this filter string filters {"groupOp":"AND","rules":[{"field":"ent_name","op":"cn","data":"задний"},{"field":"ent_name","op":"cn","data":"аморт"}]} – Dmitriy Luchkin Jan 21 '12 at 18:19
  • @DmitriyLuchkin: Sorry, but I don't full understand your question. Do you have some test case where my demo work wrong? – Oleg Jan 21 '12 at 18:33
  • @DmitriyLuchkin: Probably your problem is that you confused `groupOp='OR'` operation in the `groups` part with the main `groupOp='AND'` operation? – Oleg Jan 21 '12 at 18:41
  • I tried your function and was not what I wanted. The structure of your filter string is different from that which generates a complex search. I showed them to be compared. I had to modify your function. I ruled out the string "groups" from filter and search started working correctly. Can I talk to you in Russian? – Dmitriy Luchkin Jan 22 '12 at 12:13
  • @DmitriyLuchkin: First of all please answer on the question: do you have some test case where *my demo* work wrong? Probably I misunderstood your question and answered on another one? You can talk in Russian. You should describe your problem first of all. Which `datatype` you has. If you has `'local'` datatype you don't need o do nothing. If you has `'json'` or `'xml'` datatype, than your problem can be just because you don't implemented *full support of `filters` parameter*. My demo includes `multipleGroup: true` search option. Try to make the search with the dialog and examine the `filters`. – Oleg Jan 22 '12 at 12:48
  • Ваш демонстрационный пример работает правильно. Вопрос у меня возник из-за того, что у меня multipleGroup: false, поэтому фильтр не срабатывал. Еще вопрос, у renderGrid нет события beforeSearch. Можно ли повесить вашу функцию на событие beforeRequest? – Dmitriy Luchkin Jan 23 '12 at 06:56
  • @DmitriyLuchkin: I don't know `renderGrid` function. Do you use some commercial product which are based on jqGrid? You can modify the `postData.filters` inside of `beforeRequest` too if you have no other possibility. Why not? – Oleg Jan 23 '12 at 08:18
  • http://www.trirand.com/blog/?page_id=393/help/multiple-checkbox-in-searchdropdownsearch-for-multiple-value#spPostForm – user1688401 Sep 01 '14 at 09:15
  • @user1688401: I'm not sure what you mean. My answer allows to search for multiple sub-strings and it have some relation to the post in trirand forum which I answered recently, but the question on trirand forum asked about "multiple checkbox in SearchDropDown". The current answer on the stackoverflow have *another interface*. – Oleg Sep 01 '14 at 09:32
  • hi @Oleg i am trying..i found my business after your answear in here http://www.ok-soft-gmbh.com/jqGrid/SimpleLocalGridWithSearchingToolbarMultilpe.htm how can i get that demo? in shipped column there is multiple checkbox and works very well but how can i get that demo ? – user1688401 Sep 01 '14 at 09:56
  • @user1688401: Sorry, but I don't understand the question "how can i get that demo?". You can see the full JavaScript code which I used in [my demo](http://www.ok-soft-gmbh.com/jqGrid/SimpleLocalGridWithSearchingToolbarMultilpe.htm) just by opening context menu in web browser (click right mouse button) and choosing the menu item "View Page Source" or "View source" (depend on the web browser which you use). – Oleg Sep 01 '14 at 10:10
  • thank you @Oleg ...i get all code but iwanted that demo with documentation and other things...now i get all of them ...thank you very much – user1688401 Sep 01 '14 at 11:48