问题
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(searchKeyword, 'i')});
How do I modify this so that I can search by both, name AND email?
jsFiddle
回答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.
回答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 comma
var len = keywordArr.length;
var query = {}; // search object
if(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 then
if(validateEmail(searchKeyword)){ // check whether is it email or not
query.email = searchKeyword;
} else {
query.name = new RegExp(searchKeyword, 'i');
}
}
dgrid.set("query", query); // search query
// function to validate email id
function validateEmail(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
来源:https://stackoverflow.com/questions/22678477/how-to-search-in-2-columns-with-single-search-box