Can we define a javascript function and call it?

So I have a dropdown element I created where I am displaying a list of items and want behavior to select/unselect the items. Selected items should have different styling and be filtered to the top of the list.

I have a variable of an Array that I would pass the collection item IDs into the array. If the ID is in the array and user clicks, then the row is deselected. If not, it’s clicked. The row should conditionally show Clicked state if the ID is in the array.

I figure one of the WeWeb formulas would work but I haven’t seen this question asked so wondering if it’s possible to use functions and pass weweb variables/bindable data into a function call, like below

What you have done is a valid way. You can also make it more easily reusable.

You can define custom formula if you want to reuse a logic somewhere, with parameters

You can also define global workflow, and reuse them elsewhere if you want more a sequence of actions

1 Like

@aurelie super helpful. thanks. It would be great if we could add a comment in the formula (like you can add comments in JS). It would be helpful to be able to write documentation or even better if we could add a popup like in the weweb formulas

1 Like

I’m assuming it’s not possible to pass a variable in a global workflow as a parameter? When using the “Change variable value” action i was checking to see if it would be possible to pass a variable in as a parameter and then change its value. It appears not to me?

See also this. Is there not a way of passing in a parameter by using parameters[UID] like you can do with variables[UID]?

I think the best way to achieve what you want will be to pass the variable id, and change the variable with javascript.
variables[uid] = value. We normally have a custom setter which will update the variable.
If not let me now, i will search a way to it more properly on the interface

1 Like