Switch Method for UI Elements

I can do this programmatically, but it would be super cool if there was a native switch method for every UI element that would work something like:

self.location_dropdown_search.switch(self.answer_component)

And would switch locations between those elements when called.

I’m not sure what you mean by that. The dropdown has a distinct list of elements. I don’t know what self.answer_component is, but what do you want to happen when a particular location is chosen from the dropdown?

I think what he means is like if you have button_1 in a certain location in your layout (e.g. the first position in flow_panel_1) and button_2 in a different location (e.g. the third position in linear_panel_5), calling this method would switch their locations.

1 Like

Yes, exactly correct, @hugetim.

Maybe switch_location would be more clear?

I don’t think I’ve ever done that in any of the Anvil apps I’ve written. What cool UI technique would you use such a thing for?

I’m using it to invert search by.

So for example, you could have a search start off by asking you to select a dog breed, then a new search box appears below for that dog bread with a list of FAQ for that breed.

If you had a “Switch” button in the middle, its on click method would be something like

self.dog_breed_search.switch(self.breed_faq)

self.set_search_mode("faq_mode")

So then, in this mode, you type a FAQ, and it returns a list of dog breeds that have this FAQ in common!

1 Like

When I have a list of options similar to this, whether they are 2 or more options, I put them inside a repeating panel. Then, when it’s time to switch or reorder them, I reorder and reassign to items.

Whether you use a repeating panel or not, it’s usually easier to rebuild from scratch based on a list of items rather than managing UI components one by one.

In theory it may be slower when the components you want to switch are really large, but I have never had that problem.


I’m not sure I understand your use case. From the little I understand, you have one input element at the top of the page, and, based on the value of that input element, you want to change the visibility or the items of other input elements that are below that one. I don’t see how switching would help.

Changing the position of input elements is often a bad user experience. But again, I may not understand your use case.

3 Likes