Skip to content Skip to sidebar Skip to footer

How To Search In 2 Columns With Single Search Box?

In my project, I would like to find by name and email in the dgrid. As of now, I am able to search by name only. I used the following: dgrid.set('query', {name: new RegExp(searchKe

Solution 1:

The querying engine behind a dojo/store/Memory (called dojo/store/util/SimpleQueryEngine) does not support OR operations, which means it can't query all records where:

name matches searchterm OR email matches searchterm

To solve that issue you will have to use a different query engine than the default one. Currently there are no other query engines available in Dojo but there is a standalone project called the resource query language. You should check this answer and the library itself on Github.

Solution 2:

You try to input two parameter in single textbox and want to search result according to that. It is up to you, how you create logic, I tried something hope this will help you.

I have made some alteration in your code so I am explaining what I done only. Here I uesd comma to separate two search value.

var keywordArr = searchKeyword.split(','); // separate search values by commavar len = keywordArr.length; 
var query = {}; // search objectif(len>1){ // check whether there is two value or single
  query.name = keywordArr[0];
  if(validateEmail(keywordArr[1])){
    query.email = keywordArr[1];
  } 
} else { // if single value thenif(validateEmail(searchKeyword)){ // check whether is it email or not
    query.email = searchKeyword;                   
  } else {
    query.name = newRegExp(searchKeyword, 'i');
  }                                                                 
}
dgrid.set("query", query); // search query// function to validate email idfunctionvalidateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        return re.test(email);
} 

You can try my code on jsFiddle. Thank you

Post a Comment for "How To Search In 2 Columns With Single Search Box?"