Show up respective component when change item in dropdown

I have a dropdown and list respective component ,

  • dropdown have item1 /item 2
  • Label 1/ label 2
    I wan try when change item1 , label 1 is show up and label 2 will be hidden
    what can i do when dropdown change item
  def __init__(self, **properties):
    # Set Form properties and Data Bindings.
    self.init_components(**properties)
    dd = DropDown(items=['1','2'])
    dd.set_event_handler('change', self.drop_down_change)
    self.add_component(dd)
    list_child_dropdown = self.list_child_dropdown(['1','2'])
    self.add_component(list_child_dropdown)

  def drop_down_change(self, **event_args):
    print('something')
    # Any code you write here will run when the form opens.

  def list_child_dropdown(self, items):
    ld = LinearPanel()
    for i in items:
      x = Label(text=f'label {i}')
      ld.add_component(x)
    return ld

Clone link:
https://anvil.works/build#clone:C6GMR3BF6ODAVTCA=IVZESWDR34JLKWFWMT5LF6O5

Your starting point is here:

You can find out more about events and event_args in the Documentation. Anvil’s documentation is really good, especially its search feature.

Also search for “visible”, a component property that lets you hide and unhide components.

2 Likes