Page 1 of 2

Variable Input Database Query

Posted: Wed May 29, 2013 8:34 pm
by Nate Snyder

Hello -
I have several select boxes on an app page that allow the user to make selections in order to narrow down what is returned from a database query. I have an if-then javascript load some local storage variables based on the select boxes.

This works fine until a select box is left at the default "All" selection, in which case I want to have no filtering for that particular field. In the JS, I have the local storage set to nothing, or "" in the code. What should I use instead to get this functional?

Thanks!!

Part of the if-then JS for reference:

var selectvoltage = Appery('select_voltage').val();
if (selectvoltage == "0")
{
localStorage.setItem('localselectvoltage', ""); //nothing selected - default all
}
else if (selectvoltage == "1")
{
localStorage.setItem('localselectvoltage', "12"); //12VDC
}
else if (selectvoltage == "2")
{
localStorage.setItem('localselectvoltage', "24"); //24VDC
}

Database Request JS for reference:

var getvolume = localStorage.getItem('localselectvolume');
var getvoltage = localStorage.getItem('localselectvoltage');
return '{"PG2":"'+getvolume+'", "INPUT_VOLTAGE":"'+getvoltage+'"}';


Variable Input Database Query

Posted: Wed May 29, 2013 9:03 pm
by Maryna Brodina

Hello! Try with the following code:

codechange Database Request JS for reference
var getvolume = localStorage.getItem('localselectvolume');
var getvoltage = localStorage.getItem('localselectvoltage');
var conds = [];
if (getvolume != '' && getvolume != null ) {
conds.push('"PG2":"'+getvolume+'"');
}
if (getvoltage != '' && getvoltage != null) {
conds.push('"INPUT_VOLTAGE":"'+getvoltage+'"');
}

return '{' + conds.join(',') + '}';/code


Variable Input Database Query

Posted: Wed May 29, 2013 9:22 pm
by Nate Snyder

Thanks, Marina! This works! I now need to expand it to incorporate a total of 7 select boxes. I assume I can just keep adding more "if" 's to your code above for each list box and keep the "return" line the same?


Variable Input Database Query

Posted: Wed May 29, 2013 9:38 pm
by Nate Snyder

I just tried adding a third and then a fourth "if". Kept the return line the same. Still working. Thanks again!


Variable Input Database Query

Posted: Fri May 31, 2013 2:33 pm
by Nate Snyder

One more question related to this:

Is there a way to sort this query by one of the returned conditions? Like the "PG2" field in the code above...


Variable Input Database Query

Posted: Fri May 31, 2013 2:49 pm
by Maryna Brodina

Hello! You need to add "sort" parameter to request. Here is more information http://docs.appery.io/documentation/b...


Variable Input Database Query

Posted: Fri May 31, 2013 3:04 pm
by Nate Snyder

Thanks! However, I'm still not sure exactly what to do. Do I add a curl statement similar to what's in your link directly after the return line in the JS request code above?


Variable Input Database Query

Posted: Fri May 31, 2013 3:09 pm
by Maryna Brodina

Just add it as request parameter in Service Settings on Request tab.


Variable Input Database Query

Posted: Fri May 31, 2013 3:44 pm
by Nate Snyder

So sorry, but not having any luck. Do I add a 'sort=DB_COLUMN_NAME" line inside the "where" text box?

Image


Variable Input Database Query

Posted: Fri May 31, 2013 4:00 pm
by Maryna Brodina

No, you need to add parameter here:
Image