Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement multi-column row keys for ui.table #4105

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def __init__(self,
self._use_columns_from_df = False
self._props['columns'] = self._normalize_columns(columns)
self._props['rows'] = rows
self._props['row-key'] = row_key
# if row_key is a list of columns, use the Javascript arrow function syntax, prepending row. to each key
self.row_key = row_key # should call the property setter
self._props['title'] = title
self._props['hide-pagination'] = pagination is None
self._props['pagination'] = pagination if isinstance(pagination, dict) else {'rowsPerPage': pagination or 0}
Expand All @@ -82,7 +83,11 @@ 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']]
# if row_key is a list of columns, calculate the row_key for each row in the selected list
if isinstance(self._row_key, list):
self.selected = [row for row in self.selected if ''.join([row[col] for col in self._row_key]) not in e.args['keys']]
else:
self.selected = [row for row in self.selected if row[self._row_key] not in e.args['keys']]
self.update()
arguments = TableSelectionEventArguments(sender=self, client=self.client, selection=self.selected)
for handler in self._selection_handlers:
Expand Down Expand Up @@ -301,11 +306,15 @@ def column_defaults(self, value: Optional[Dict]) -> None:
@property
def row_key(self) -> str:
"""Name of the column containing unique data identifying the row."""
return self._props['row-key']
return self._row_key

@row_key.setter
def row_key(self, value: str) -> None:
self._props['row-key'] = value
if isinstance(value, list):
self._row_key = value
self._props[':row-key'] = f"row => {'+'.join([f'row.{col}' for col in value])}"
else:
self._props['row-key'] = self._row_key = value
self.update()

@property
Expand Down