0
votes

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?

1

1 Answers

0
votes

Here is one way to accomplish this:

  1. Make a new datasource for the model you want to search in.
  2. Select Query Script as the query type.
  3. Add a string parameter called searchCriteria
  4. Add a bunch of parameters called parameter0, parameter1, parameter2, etc.
  5. 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();
  1. Then on your page bind the text box you want to search with to @datasource.query.parameters.searchCriteria
  2. Finally modify the onValueEdit of the search box to Reload Datasource

This technique is limited by how many numbered parameters you create and doesn't allow using parenthesis.