I am creating an app using Google AppMaker and wish to create a boolean search option (text box where I can use AND/OR options to search multiple combinations of strings). This is similar to how I can search currently on Gmail or LinkedIn etc. How can I create it?
0
votes
1 Answers
0
votes
Here is one way to accomplish this:
- Make a new datasource for the model you want to search in.
- Select
Query Scriptas the query type. - Add a string parameter called
searchCriteria - Add a bunch of parameters called
parameter0,parameter1,parameter2, etc. - Enter this code as the Server Script (assuming the field you want to search in is called Name):
var searchCriteria = query.parameters.searchCriteria;
if(searchCriteria === null) return query.run();
var searchArray = searchCriteria.split(/( and | or )/i);
var searchString = '';
for(var i = 0; i < searchArray.length; i++){
if(i % 2 === 0 ){
searchString += 'Name contains :parameter' + i.toString() + ' ';
query.parameters['parameter' + i.toString()] = searchArray[i];
}else{
searchString += searchArray[i];
}
}
query.where = searchString;
return query.run();
- Then on your page bind the text box you want to search with to
@datasource.query.parameters.searchCriteria - Finally modify the
onValueEditof the search box toReload Datasource
This technique is limited by how many numbered parameters you create and doesn't allow using parenthesis.