Skip to content

Commit

Permalink
introduce selection property for ui.table (fixes #4296)
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Feb 3, 2025
1 parent c3ce92a commit 56a2083
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
22 changes: 21 additions & 1 deletion nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self,

def handle_selection(e: GenericEventArguments) -> None:
if e.args['added']:
if selection == 'single':
if self.selection == 'single':
self.selected.clear()
self.selected.extend(e.args['rows'])
else:
Expand Down Expand Up @@ -322,6 +322,26 @@ def selected(self, value: List[Dict]) -> None:
self._props['selected'][:] = value
self.update()

@property
def selection(self) -> Literal['none', 'single', 'multiple']:
"""Selection type.
*Added in version 2.11.0*
"""
return self._props['selection']

@selection.setter
def selection(self, value: Literal['none', 'single', 'multiple']) -> None:
self._props['selection'] = value
self.update()

def set_selection(self, value: Literal['none', 'single', 'multiple']) -> None:
"""Set the selection type.
*Added in version 2.11.0*
"""
self.selection = value

@property
def pagination(self) -> dict:
"""Pagination object."""
Expand Down
18 changes: 16 additions & 2 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ def test_slots(screen: Screen):
screen.should_contain('21')


def test_single_selection(screen: Screen):
ui.table(columns=columns(), rows=rows(), selection='single')
def test_selection(screen: Screen):
table = ui.table(columns=columns(), rows=rows(), selection='single')
ui.radio(['none', 'single', 'multiple'], on_change=lambda e: table.set_selection(e.value))

screen.open('/')
screen.find('Alice').find_element(By.XPATH, 'preceding-sibling::td').click()
Expand All @@ -121,6 +122,19 @@ def test_single_selection(screen: Screen):
screen.wait(0.5)
screen.should_contain('1 record selected.')

screen.click('multiple')
screen.wait(0.5)

screen.find('Lionel').find_element(By.XPATH, 'preceding-sibling::td').click()
screen.wait(0.5)
screen.should_contain('2 records selected.')
assert table.selection == 'multiple'

screen.click('none')
screen.wait(0.5)
screen.should_not_contain('1 record selected.')
assert table.selection == 'none'


def test_dynamic_column_attributes(screen: Screen):
ui.table(columns=[{'name': 'age', 'label': 'Age', 'field': 'age', ':format': 'value => value + " years"'}],
Expand Down
19 changes: 19 additions & 0 deletions website/documentation/content/table_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ def default_column_parameters():
})


@doc.demo('Selection', '''
You can set the selection type of a table using the `selection` parameter.
The `on_select` event handler is called when the selection changes
and the `selected` property contains the selected rows.
*Added in version 2.11.0:*
The `selection` property and the `set_selection` method can be used to change the selection type.
''')
def selection():
table = ui.table(
columns=[{'name': 'name', 'label': 'Name', 'field': 'name'}],
rows=[{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Charlie'}],
row_key='name',
on_select=lambda e: ui.notify(f'selected: {e.selection}'),
)
ui.radio(['none', 'single', 'multiple'], value='none',
on_change=lambda e: table.set_selection(e.value))


@doc.demo('Table with expandable rows', '''
Scoped slots can be used to insert buttons that toggle the expand state of a table row.
See the [Quasar documentation](https://quasar.dev/vue-components/table#expanding-rows) for more information.
Expand Down

0 comments on commit 56a2083

Please sign in to comment.