Category: Question and Answers
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

Passing CheckBoxGroup.selectedValues to parameter in query

Issue

I have a CheckboxGroup in my app, and I need to use the selected value in a query. However, the value is in an array format and I don't know how to extract it. I need to be able to use the selected option to change the table name in the query. Can anyone help me with this?

Resolution

The user wants to use the value of the selected box in a query. Since the selected value is in an array, the user is using the CheckboxGroup Widget to get the selected values. However, they need to loop through the selected values and perform a query for each one. This can be done by storing the selected value in the appsmith store and then accessing it in the query.

To do this, the user can create a JSObject with a function that loops through the selected values, stores the value in the appsmith store, and runs the query with the stored value. This function can be called when the user clicks a button or when a specific event is triggered.

An example of the function would be:

export default {
performSelectedQueries: async () => {
const selectedValues = CheckBoxGroup.selectedValues // looks like ["RED", "BLUE"]
selectedValues.forEach( (value) => {
await storeValue("currentValue", value) // saving this within the appsmith store to be used within the query
myQuery.run()
// if you need data from the query, capture it here. Maybe something like:
// await storeValue(value, myQuery.data) // use it elsewhere with appsmith.store.RED or appsmith.store.BLUE
})
}
}

And the query might look like:

SELECT * FROM {{appsmith.store.currentValue}} ORDER BY id LIMIT 10;

This way, the user can use the value of the selected box in a query by looping through the selected values and storing them in the appsmith store.