How to perform local search by formatted column value in jqgrid?

I use loadonce to get all the data in front, and then sort and filter locally.

One of the values ​​in my column is an array of objects. In the colModel option, I use a formatting function that looks like this:

function my_formatter(cellValue) { return $.map(cellValue, function(element) {return element.human_readable_name;}).join(', '); } 

I also use a custom sort function that simply returns the length of the array.

The problem is that toolbar filtering and multi-threaded dialog filtering do not work. They seem to be looking for [objects] .toString (), not a formatted value. Therefore, I get hits when I search for “object of object”, but not when searching for actual values.

Is there a way to force local filtering to use a formatted value?

Change based on Oleg’s answer:

I adapted Oleg's code to add filter formatting for each column. It seems to work well. I deleted the _toStr replacement because it did not seem necessary - I think it used to change the search term (which makes sense in the case of the accent for Oleg's accent, but not mine).

 // 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 && 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; }; } 

And it is called like this:

 setFilterFormatters({'column_with_array_of_objects':'my_formatter'}); 

Testing assumes that this works for “contains,” “does not contain,” “equal,” “not equal” (and probably “starts with” and other simple string comparisons — but I don't use them).

Thank you Oleg.

+2
source share
1 answer

In my old answer in the trirand forum, I described how to implement custom local filtering and sorting. The demo shows search and sorting without accents. If you use the same method, you can overwrite some jqGrid functions used for searching (for example, _toStr and _getStr ) and implement it so that in the case of arrays you use your own implementation.

To make my answer more Google friendly, I include a small piece of code

 function myAccentRemovement(s) { // the s parameter is always string s = s.replace(/[àáâãäå]/gi,'a'); s = s.replace(/[èéêë]/gi,'e'); s = s.replace(/[ìíîï]/gi,'i'); s = s.replace(/[òóôõöø]/gi,'o'); s = s.replace(/[ùúûü]/gi,'u'); s = s.replace(/[ýÿ]/gi,'y'); s = s.replace(/æ/gi,'ae'); s = s.replace(/œ/gi,'oe'); s = s.replace(/ç/gi,'c'); s = s.replace(/š/gi,'s'); s = s.replace(/ñ/gi,'n'); s = s.replace(/ž/gi,'z'); return s; } //... var oldFrom = $.jgrid.from; $.jgrid.from = function(source,initalQuery){ var result = oldFrom(source,initalQuery); var old_toStr = result._toStr; result._toStr=function(s) { return myAccentRemovement(old_toStr(s)); }; result._getStr=function(s) { var phrase=[]; if(this._trim){ phrase.push("jQuery.trim("); } phrase.push("myAccentRemovement(String("+s+"))"); if(this._trim){ phrase.push(")"); } if(!this._usecase){ phrase.push(".toLowerCase()"); } return phrase.join(""); } return result; } 

This will solve the problem. I cannot give more accurate suggestions because you did not provide any information about the exact structure of the data you use.

+1
source

Source: https://habr.com/ru/post/911128/


All Articles