Skip to content

Commit

Permalink
add support for default column parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 16, 2024
1 parent 96f83e7 commit b4fb23d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
9 changes: 7 additions & 2 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self,
first_row = rows[0] if rows else {}
columns = [{'name': key, 'label': str(key).upper(), 'field': key, 'sortable': True} for key in first_row]

self._props['columns'] = columns
self._props['columns'] = self._normalize_columns(columns)
self._props['rows'] = rows
self._props['row-key'] = row_key
self._props['title'] = title
Expand Down Expand Up @@ -92,6 +92,11 @@ def on_pagination_change(self, callback: Callable[..., Any]) -> Self:
self._pagination_change_handlers.append(callback)
return self

@staticmethod
def _normalize_columns(columns: List[Dict]) -> List[Dict]:
default_column_parameters = dict(item for column in columns if 'name' not in column for item in column.items())
return [{**default_column_parameters, **column} for column in columns if 'name' in column]

@classmethod
def from_pandas(cls,
df: 'pd.DataFrame',
Expand Down Expand Up @@ -178,7 +183,7 @@ def columns(self) -> List[Dict]:

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

@property
Expand Down
23 changes: 23 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,26 @@ def test_infer_columns(screen: Screen):
screen.should_contain('Bob')
screen.should_contain('18')
screen.should_contain('21')


def test_default_column_parameters(screen: Screen):
ui.table(rows=[
{'name': 'Alice', 'age': 18, 'city': 'London'},
{'name': 'Bob', 'age': 21, 'city': 'Paris'},
], columns=[
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
{'name': 'city', 'label': 'City', 'field': 'city', 'sortable': False},
{'sortable': True},
])

screen.open('/')
screen.should_contain('Name')
screen.should_contain('Age')
screen.should_contain('Alice')
screen.should_contain('Bob')
screen.should_contain('18')
screen.should_contain('21')
screen.should_contain('London')
screen.should_contain('Paris')
assert len(screen.find_all_by_class('sortable')) == 2
16 changes: 16 additions & 0 deletions website/documentation/content/table_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ def omitting_columns():
])


@doc.demo('Default column parameters', '''
You can define default column parameters that apply to all columns.
In this example, all columns are left-aligned by default.
Every column dictionary without a "name" key is treated as default column parameters.
''')
def default_column_parameters():
ui.table(rows=[
{'name': 'Alice', 'age': 18},
{'name': 'Bob', 'age': 21},
], columns=[
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
{'align': 'left'},
])


@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 b4fb23d

Please sign in to comment.