Skip to content

Commit

Permalink
use properties to avoid overwriting references
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Oct 6, 2023
1 parent 86369c3 commit d44a3d0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
48 changes: 42 additions & 6 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,14 @@ def __init__(self,
"""
super().__init__()

self.rows = rows
self.row_key = row_key
self.selected: List[Dict] = []

self._props['columns'] = columns
self._props['rows'] = rows
self._props['row-key'] = row_key
self._props['title'] = title
self._props['hide-pagination'] = pagination is None
self._props['pagination'] = pagination if isinstance(pagination, dict) else {'rowsPerPage': pagination or 0}
self._props['selection'] = selection or 'none'
self._props['selected'] = self.selected
self._props['selected'] = []
self._props['fullscreen'] = False

def handle_selection(e: GenericEventArguments) -> None:
Expand All @@ -52,12 +48,52 @@ def handle_selection(e: GenericEventArguments) -> None:
self.selected.clear()
self.selected.extend(e.args['rows'])
else:
self.selected[:] = [row for row in self.selected if row[row_key] not in e.args['keys']]
self.selected = [row for row in self.selected if row[row_key] not in e.args['keys']]
self.update()
arguments = TableSelectionEventArguments(sender=self, client=self.client, selection=self.selected)
handle_event(on_select, arguments)
self.on('selection', handle_selection, ['added', 'rows', 'keys'])

@property
def rows(self) -> List[Dict]:
"""List of rows."""
return self._props['rows']

@rows.setter
def rows(self, value: List[Dict]) -> None:
self._props['rows'][:] = value
self.update()

@property
def columns(self) -> List[Dict]:
"""List of columns."""
return self._props['columns']

@columns.setter
def columns(self, value: List[Dict]) -> None:
self._props['columns'][:] = value
self.update()

@property
def row_key(self) -> str:
"""Name of the column containing unique data identifying the row."""
return self._props['row-key']

@row_key.setter
def row_key(self, value: str) -> None:
self._props['row-key'] = value
self.update()

@property
def selected(self) -> List[Dict]:
"""List of selected rows."""
return self._props['selected']

@selected.setter
def selected(self, value: List[Dict]) -> None:
self._props['selected'][:] = value
self.update()

@property
def is_fullscreen(self) -> bool:
"""Whether the table is in fullscreen mode."""
Expand Down
20 changes: 20 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,23 @@ def test_remove_selection(screen: Screen):
screen.wait(0.5)
screen.should_not_contain('Alice')
screen.should_not_contain('1 record selected.')


def test_replace_rows(screen: Screen):
t = ui.table(columns=columns(), rows=rows())

def replace_rows():
t.rows = [{'id': 3, 'name': 'Carol', 'age': 32}]
ui.button('Replace rows', on_click=replace_rows)

screen.open('/')
screen.should_contain('Alice')
screen.should_contain('Bob')
screen.should_contain('Lionel')

screen.click('Replace rows')
screen.wait(0.5)
screen.should_not_contain('Alice')
screen.should_not_contain('Bob')
screen.should_not_contain('Lionel')
screen.should_contain('Carol')

0 comments on commit d44a3d0

Please sign in to comment.