Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to do local search on formatted column value in jqgrid?
    primarykey
    data
    text
    <p>I'm using loadonce to get all data up front and then doing sorting and filtering locally.</p> <p>One of my column values is an array of objects. In the colModel option I use a formatter function that looks like this:</p> <pre><code>function my_formatter(cellValue) { return $.map(cellValue, function(element) {return element.human_readable_name;}).join(', '); } </code></pre> <p>I also use a custom sorting function that simply returns the length of the array.</p> <p>The problem I have is that the toolbar filtering and multi-field dialog filtering aren't working. They seem to be searching on [objects].toString() rather than the formatted value. So I get hits when I search for "[object Object]", but not when I search for the actual values.</p> <p>Is there a way to get the local filtering to use the formatted value? </p> <p><strong>Edit based on Oleg's response:</strong></p> <p>I adapted Oleg's code to add per-column filter formatting. It seems to work well. I removed the _toStr replacement because it didn't seem necessary -- I think it's used to modify the search term (which makes sense in Oleg's accent-stripping case, but not in mine).</p> <pre><code>// Causes local filtering to use custom formatters for specific columns. // formatters is a dictionary of the form: // { "column_name_1_needing_formatting": "column1FormattingFunctionName", // "column_name_2_needing_formatting": "column2FormattingFunctionName" } // Note that subsequent calls will *replace* all formatters set by previous calls. function setFilterFormatters(formatters) { function columnUsesCustomFormatter(column_name) { for (var col in formatters) { if (col == column_name) return true; } return false; } var accessor_regex = /jQuery\.jgrid\.getAccessor\(this\,'(.+)'\)/; var oldFrom = $.jgrid.from; $.jgrid.from = function(source, initialQuery) { var result = oldFrom(source, initialQuery); result._getStr = function(s) { var column_formatter = 'String'; var column_match = s.match(accessor_regex, '$1'); if (column_match &amp;&amp; columnUsesCustomFormatter(column_match[1])) { column_formatter = formatters[column_match[1]]; } var phrase=[]; if(this._trim) { phrase.push("jQuery.trim("); } phrase.push(column_formatter+"("+s+")"); if(this._trim) { phrase.push(")"); } if(!this._usecase) { phrase.push(".toLowerCase()"); } return phrase.join(""); } return result; }; } </code></pre> <p>And it gets called like this:</p> <pre><code>setFilterFormatters({'column_with_array_of_objects':'my_formatter'}); </code></pre> <p>Testing suggests that this works for 'contains', 'does not contain', 'equals', 'does not equal' (and probably 'begins with' and the other simple string comparisons -- but I'm not using them). </p> <p>Thanks, Oleg.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload