table.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from typing import Any, Callable, Dict, List, Optional
  2. from typing_extensions import Literal
  3. from ..element import Element
  4. from ..events import TableSelectionEventArguments, handle_event
  5. from .mixins.filter_element import FilterElement
  6. class Table(FilterElement):
  7. def __init__(self,
  8. columns: List[Dict],
  9. rows: List[Dict],
  10. row_key: str = 'id',
  11. title: Optional[str] = None,
  12. selection: Optional[Literal['single', 'multiple']] = None,
  13. pagination: Optional[int] = None,
  14. on_select: Optional[Callable[..., Any]] = None,
  15. ) -> None:
  16. """Table
  17. A table based on Quasar's `QTable <https://quasar.dev/vue-components/table>`_ component.
  18. :param columns: list of column objects
  19. :param rows: list of row objects
  20. :param row_key: name of the column containing unique data identifying the row (default: "id")
  21. :param title: title of the table
  22. :param selection: selection type ("single" or "multiple"; default: `None`)
  23. :param pagination: number of rows per page (`None` hides the pagination, 0 means "infinite"; default: `None`)
  24. :param on_select: callback which is invoked when the selection changes
  25. If selection is 'single' or 'multiple', then a `selection` property is accessible containing the selected rows.
  26. """
  27. super().__init__(tag='q-table')
  28. self.rows = rows
  29. self.row_key = row_key
  30. self.selected: List[Dict] = []
  31. self._props['columns'] = columns
  32. self._props['rows'] = rows
  33. self._props['row-key'] = row_key
  34. self._props['title'] = title
  35. self._props['hide-pagination'] = pagination is None
  36. self._props['pagination'] = {'rowsPerPage': pagination or 0}
  37. self._props['selection'] = selection or 'none'
  38. self._props['selected'] = self.selected
  39. def handle_selection(msg: Dict) -> None:
  40. if msg['args']['added']:
  41. if selection == 'single':
  42. self.selected.clear()
  43. self.selected.extend(msg['args']['rows'])
  44. else:
  45. self.selected[:] = [row for row in self.selected if row[row_key] not in msg['args']['keys']]
  46. self.update()
  47. arguments = TableSelectionEventArguments(sender=self, client=self.client, selection=self.selected)
  48. handle_event(on_select, arguments)
  49. self.on('selection', handle_selection)
  50. def add_rows(self, *rows: Dict) -> None:
  51. """Add rows to the table."""
  52. self.rows.extend(rows)
  53. self.update()
  54. def remove_rows(self, *rows: Dict) -> None:
  55. """Remove rows from the table."""
  56. keys = [row[self.row_key] for row in rows]
  57. self.rows[:] = [row for row in self.rows if row[self.row_key] not in keys]
  58. self.update()
  59. class row(Element):
  60. def __init__(self) -> None:
  61. super().__init__('q-tr')
  62. class header(Element):
  63. def __init__(self) -> None:
  64. super().__init__('q-th')
  65. class cell(Element):
  66. def __init__(self, key: str = '') -> None:
  67. super().__init__('q-td')
  68. if key:
  69. self._props['key'] = key