Is it possible to pass a variable to a DataSource query when binding data to a widget?

Hello,

I’m just starting out with appsmith and am a total newbie so this could be a dumb question :slight_smile:

Anyway, I was wondering if it were possible when using a query to bind data to a widget, to pass that query a variable in order to modify the data coming back from the query to the widget.

I’ve had a bit of a look and can see that when executing a query with run() from a widget action, it is possible to pass a parameter that you can reference in the query - but am not sure if this is possible when binding data.

My use case is a query returns a list of items from a MySQL database - but the items belong to categories and I would like to use the same query to bind the data to the same list widget in my app, but based on a user choosing the category first - so basically the parameter passed to the query would end up being used in a WHERE statement in the query.

Any info / insights would be much appreciated.

Cheers,

Hello, and welcome to our community!
You can run a query with parameters. Here is also a sample app that demonstrates how to do this. Please fork it to check the code.

Hi Amelia, thanks for the reply.

I’ve had a go at trying to make this work but don’t seem to be able to get there.

My original code that works is:

{{ServiceRecord_Retrive.data}}

If I replace it with this, it doesn’t work - but I’m guessing this isnt the correct way to do it?

{{ServiceRecord_Retrive.run({ catValue: 1})}}

I get the error:

Found a reference to ServiceRecord_Retrive.run() during evaluation. Sync fields cannot execute framework actions. Please remove any direct/indirect references to ServiceRecord_Retrive.run() and try again.

Here is a screen shot :slight_smile:

image

I will keep tinkering away, but if you can see something obvious I need to change to get this to work, feel free to let me know! :slight_smile:

Cheers,

Hello @gpnz , this looks like the correct way to do it. Does it not work for you?

Whoops! Sorry, Was too quick - sorry @Olawale :slight_smile:

Hi Amelia,

Yes, this {{ServiceRecord_Retrive.data}} does work, but I would like to pass the referenced query a parameter - and that is the bit I cannot get to work.

Cheers,

It does not work because you are running an async method in a sync field.
You would need to create a JS function inside a JS Object that runs on page load and run your query with the desired params from there.
However, if you need to populate several list or table widgets on this page with the results of this query, each with a different parameter, you would need to create such a JS function for each of the widgets.
Here is our documentation on how to return the data of an async function inside a widget.

Ahhh - cool, thanks Amelia!