select_documentation.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class SelectDocumentation(UiElementDocumentation, element=ui.select):
  4. def main_demo(self) -> None:
  5. select1 = ui.select([1, 2, 3], value=1)
  6. select2 = ui.select({1: 'One', 2: 'Two', 3: 'Three'}).bind_value(select1, 'value')
  7. def more(self) -> None:
  8. @self.demo('Search-as-you-type', '''
  9. You can activate `with_input` to get a text input with autocompletion.
  10. The options will be filtered as you type.
  11. ''')
  12. def search_as_you_type():
  13. continents = [
  14. 'Asia',
  15. 'Africa',
  16. 'Antarctica',
  17. 'Europe',
  18. 'Oceania',
  19. 'North America',
  20. 'South America',
  21. ]
  22. ui.select(options=continents, with_input=True,
  23. on_change=lambda e: ui.notify(e.value)).classes('w-40')
  24. @self.demo('Multi selection', '''
  25. You can activate `multiple` to allow the selection of more than one item.
  26. ''')
  27. def multi_select():
  28. names = ['Alice', 'Bob', 'Carol']
  29. ui.select(names, multiple=True, value=names[:2], label='comma-separated') \
  30. .classes('w-64')
  31. ui.select(names, multiple=True, value=names[:2], label='with chips') \
  32. .classes('w-64').props('use-chips')
  33. @self.demo('Update options', '''
  34. Options can be changed with the `options` property.
  35. But then you also need to call `update()` afterwards to let the change take effect.
  36. `set_options` is a shortcut that does both and works well for lambdas.
  37. ''')
  38. def update_selection():
  39. select = ui.select([1, 2, 3], value=1)
  40. with ui.row():
  41. ui.button('4, 5, 6', on_click=lambda: select.set_options([4, 5, 6], value=4))
  42. ui.button('1, 2, 3', on_click=lambda: select.set_options([1, 2, 3], value=1))