0

I would like to highlight the search results, when using the filter toolbar.

I tried to use the solution Oleg and Abhishek Simon provided here, but it is not working in JqSuite for PHP.

grid.php code snippet

$highlighting = <<<HIGHLIGHTING

function () {
    var filters, i, l, rules, rule, iCol, $this = $(this);
    if (this.p.search === true) {
        filters = $.parseJSON(this.p.postData.filters);
        if (filters !== null && typeof filters.rules !== 'undefined' &&
                filters.rules.length > 0) {
            rules = filters.rules;
            l = rules.length;
            for (i = 0; i < l; i++) {
                rule = rules[i];
                iCol = getColumnIndexByName($this, rule.field);
                if (iCol >=0) {
                    $('>tbody>tr.jqgrow>td:nth-child(' + (iCol + 1) +
                        ')', this).highlight(rule.data);
                }
            }
        }
    }
}

HIGHLIGHTING;

$grid->setGridEvent('loadComplete',$highlighting);

I get this error:

Notice: Undefined variable: this

Are you able to get what I am doing wrong?

Community
  • 1
  • 1
Jess Stone
  • 677
  • 8
  • 21

1 Answers1

1

I don't use PHP myself. Anyway you should know the ID of the grid which you use on the page. If it is for example id="grid" then you can replace this with $("#grid")[0] inside of the code of the function.

Jess Stone
  • 677
  • 8
  • 21
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I am sorry, but it is still not working. I changed it to: `$this = $("#grid")[0]` – Jess Stone Feb 04 '14 at 11:52
  • @JessStone: The code which you use contains **many** references to `this`. The code `$this = $(this)` can be replaced to `$this = $($("#grid")[0])` or just to `$this = $("#grid")`, The code `$('>tbody>tr.jqgrow>td:nth-child('..., this)` contains `this`, the code `this.p.search`, `this.p.postData.filters` can be replaced to `$("#grid")[0].p.search` and `$("#grid")[0].p.postData.filters`. – Oleg Feb 04 '14 at 14:01
  • thank you dear @Oleg. unfortunately I am now getting: `Uncaught ReferenceError: getColumnIndexByName is not defined ` – Jess Stone Feb 04 '14 at 14:36
  • @JessStone: The code of [the demo](http://www.ok-soft-gmbh.com/jqGrid/AbhishekSimon11_.htm) includes the definition of `getColumnIndexByName` too. You can just include the code **inside** of function which you define as `$highlighting`. The local function `getColumnIndexByName` can be defined in the same way like any local variable. – Oleg Feb 04 '14 at 14:50
  • yeah @Oleg you're right! I was missing that function. NB: i have added the function `getColumnIndexByName` through `$grid->setJSCode($custom);`, since it wasn't working if added through: `$grid->setGridEvent('loadComplete',$highlighting); `. so basically my code got split into two sections :) thanks once again my Guru! – Jess Stone Feb 04 '14 at 15:11
  • how can I avoid highlighting results even in the edit and form-view form? i tried to add `:not(\'.form-view-data\')` (exclude form view class) to the code : `$('>tbody>tr.jqgrow>td:not(\'.form-view-data\'):nth-child(' + (iCol + 1) +')', $('#grid')[0]).highlight(rule.data);` . Though, it is not working! – Jess Stone Feb 05 '14 at 14:51