Selaa lähdekoodia

add set_options method and ui.select demo

Rodja Trappe 1 vuosi sitten
vanhempi
säilyke
7b5336532c

+ 7 - 0
nicegui/elements/choice_element.py

@@ -33,3 +33,10 @@ class ChoiceElement(ValueElement):
         self._update_values_and_labels()
         self._update_options()
         super().update()
+
+    def set_options(self, options: Union[List, Dict], *, value=None) -> None:
+        """Set the options of this choice element."""
+        self.options = options
+        if value is not None:
+            self.value = value
+        self.update()

+ 11 - 0
website/more_documentation/select_documentation.py

@@ -35,3 +35,14 @@ def more() -> None:
             .classes('w-64')
         ui.select(names, multiple=True, value=names[:2], label='with chips') \
             .classes('w-64').props('use-chips')
+
+    @text_demo('update_selection', '''
+        Options can be changed with the `options` property.
+        But then you also need to call `update()` afterwards to let the change take effect.
+        `set_options` is a shortcut that does both and works well for lambdas.
+    ''')
+    def update_selection():
+        select = ui.select([1, 2, 3], value=1)
+        with ui.row():
+            ui.button('4, 5, 6', on_click=lambda: select.set_options([4, 5, 6], value=4))
+            ui.button('1, 2, 3', on_click=lambda: select.set_options([1, 2, 3], value=1))