select_documentation.py 1.6 KB

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