Select to view content in your preferred language

Regexp question for dgrid filter

1687
2
Jump to solution
05-09-2016 09:12 AM
JamesFaron
Occasional Contributor

I am using a filter for a simple dgrid in an application (code below extracted for simplification):

var value = (newValue + "").replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
               queryNameVal[queryProp] = new RegExp(value, "i");
               grid.eschoolsList.set("query", queryNameVal);

    

This works, but it finds all results for the input value, regardless of where it is in the data, in this case, names of schools. Thus if the data has "Randolph" and "Shafer", and the user types in an "r", both items are returned in the filter.  I would like it to only find results beginning with the input value, so that a user input "r" would only return schools beginning with the letter "r", and so on with any user input letter. I have found regexp expressions to search for first letter in a string, but I haven't been able to execute anything successfully in this scenario. Is it possible, and if so, any suggestions would be greatly appreciated.    

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

James,

  Just use a query function:

var newValue = "A";

//so what is going on here is the query function will evaluate each item and the toLowerCase() on the fields value and the input value make the query

//case insensitive and the indexOf  === 0 means that the field has to begin with the input value.

grid.set('query', function (item) { return item.stateName.toLowerCase().indexOf(newValue.toLowerCase()) === 0; });

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Emeritus

James,

  Just use a query function:

var newValue = "A";

//so what is going on here is the query function will evaluate each item and the toLowerCase() on the fields value and the input value make the query

//case insensitive and the indexOf  === 0 means that the field has to begin with the input value.

grid.set('query', function (item) { return item.stateName.toLowerCase().indexOf(newValue.toLowerCase()) === 0; });

JamesFaron
Occasional Contributor

Robert,

Perfect! Thank you once again.

0 Kudos