Skip to content

Commit

Permalink
Merge pull request #1646 from natankeddem/table_pagination
Browse files Browse the repository at this point in the history
Table Pagination
  • Loading branch information
falkoschindler authored Sep 19, 2023
2 parents 320731b + 0c8844d commit bfd70b0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
8 changes: 4 additions & 4 deletions nicegui/elements/table.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, List, Literal, Optional
from typing import Any, Callable, Dict, List, Literal, Optional, Union

from ..element import Element
from ..events import GenericEventArguments, TableSelectionEventArguments, handle_event
Expand All @@ -13,7 +13,7 @@ def __init__(self,
row_key: str = 'id',
title: Optional[str] = None,
selection: Optional[Literal['single', 'multiple']] = None,
pagination: Optional[int] = None,
pagination: Optional[Union[int, dict]] = None,
on_select: Optional[Callable[..., Any]] = None,
) -> None:
"""Table
Expand All @@ -25,7 +25,7 @@ def __init__(self,
:param row_key: name of the column containing unique data identifying the row (default: "id")
:param title: title of the table
:param selection: selection type ("single" or "multiple"; default: `None`)
:param pagination: number of rows per page (`None` hides the pagination, 0 means "infinite"; default: `None`)
:param pagination: A dictionary correlating to a pagination object or number of rows per page (`None` hides the pagination, 0 means "infinite"; default: `None`).
:param on_select: callback which is invoked when the selection changes
If selection is 'single' or 'multiple', then a `selected` property is accessible containing the selected rows.
Expand All @@ -41,7 +41,7 @@ def __init__(self,
self._props['row-key'] = row_key
self._props['title'] = title
self._props['hide-pagination'] = pagination is None
self._props['pagination'] = {'rowsPerPage': pagination or 0}
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['fullscreen'] = False
Expand Down
12 changes: 11 additions & 1 deletion tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_table(screen: Screen):
screen.should_contain('Lionel')


def test_pagination(screen: Screen):
def test_pagination_int(screen: Screen):
ui.table(columns=columns(), rows=rows(), pagination=2)

screen.open('/')
Expand All @@ -43,6 +43,16 @@ def test_pagination(screen: Screen):
screen.should_contain('1-2 of 3')


def test_pagination_dict(screen: Screen):
ui.table(columns=columns(), rows=rows(), pagination={'rowsPerPage': 2})

screen.open('/')
screen.should_contain('Alice')
screen.should_contain('Bob')
screen.should_not_contain('Lionel')
screen.should_contain('1-2 of 3')


def test_filter(screen: Screen):
table = ui.table(columns=columns(), rows=rows())
ui.input('Search by name').bind_value(table, 'filter')
Expand Down
26 changes: 26 additions & 0 deletions website/more_documentation/table_documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,29 @@ def toggle() -> None:
table.toggle_fullscreen()
button.props('icon=fullscreen_exit' if table.is_fullscreen else 'icon=fullscreen')
button = ui.button('Toggle fullscreen', icon='fullscreen', on_click=toggle).props('flat')

@text_demo('Pagination', '''
You can provide either a single integer or a dictionary to define pagination.
The dictionary can contain the following keys:
- `rowsPerPage`: The number of rows per page.
- `sortBy`: The column name to sort by.
- `descending`: Whether to sort in descending order.
- `page`: The current page (1-based).
''')
def pagination() -> None:
columns = [
{'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
{'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
]
rows = [
{'name': 'Elsa', 'age': 18},
{'name': 'Oaken', 'age': 46},
{'name': 'Hans', 'age': 20},
{'name': 'Sven'},
{'name': 'Olaf', 'age': 4},
{'name': 'Anna', 'age': 17},
]
ui.table(columns=columns, rows=rows, pagination=3)
ui.table(columns=columns, rows=rows, pagination={'rowsPerPage': 4, 'sortBy': 'age', 'page': 2})

0 comments on commit bfd70b0

Please sign in to comment.