Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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+'"}';

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Variable Input Database Query

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

Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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?

Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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

Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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...

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Variable Input Database Query

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

Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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?

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Variable Input Database Query

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

Nate Snyder
Posts: 0
Joined: Fri May 03, 2013 5:57 pm

Variable Input Database Query

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

Image

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Variable Input Database Query

No, you need to add parameter here:
Image

Return to “Issues”