Create an Event Handler that Sets a Component to Default Value

I am creating a button that will reset a bunch of select/multiselect components to their default values. How can I do this with Event Handlers? When using Clear Value, the value is then set to NULL rather than the default value. When looking at Set Value, I don't see any option for Default Value or something along those lines like you can set within a Select component. Is there a way to reset the value to the default (whether using Retool UI or JS) or is this a feature that would need to be created?

I'm surprised there's not a setDefault or something similar. There may be a trick I'm not aware of.

Here's a stab at a workaround using temp state to store the default component values.

select1_default

Agreed, I am as well. This is the first app I'm building in Retool so I'm not super familiar with all the workings - this looks like it should work, but would you mind sharing a screenshot of what your code looks like for the temp state?

Yep, np.

defaults_temp

Is there any way to populate that with the default value from the component? Or does that need to be a second place where it's manually maintained? Or conversely, is there a way to set the default value of the component based on that temp state?

In the example, I'm maintaining the component defaults in a temporary state variable.

https://docs.retool.com/docs/temporary-state

Then, within each component, set the component's "default" value like
set_default

Perfect, that is very helpful! I will get implementing now and let you know if that works.

Getting an error back that "c.setValue is not a function". I'm pretty new to JS as well, do you know what function may work there instead? Was it working on the reset for you?

The reset did work, yep.

Are the items in your components array quoted? Should look like

const components = [select1, multiselect1]

where each item is the component id of a component you want to reset.

Did try it quoted and unquoted...runs successfully without the quotes,but the actual values don't change. Attached a screenshot, maybe I'm mistyping something?

I also tried {{defaults.value[self.name]}} as well.


Missing a comma in my defaults :man_facepalming: :man_facepalming:
Got it working now....thanks very much for the help and apologies for the error between the chair and keyboard.

1 Like