table.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from typing import Any, Callable, Dict, List, Literal, Optional, Union
  2. from ..element import Element
  3. from ..events import GenericEventArguments, TableSelectionEventArguments, handle_event
  4. from .mixins.filter_element import FilterElement
  5. class Table(FilterElement, component='table.js'):
  6. def __init__(self,
  7. columns: List[Dict],
  8. rows: List[Dict],
  9. row_key: str = 'id',
  10. title: Optional[str] = None,
  11. selection: Optional[Literal['single', 'multiple']] = None,
  12. pagination: Optional[Union[int, dict]] = None,
  13. on_select: Optional[Callable[..., Any]] = None,
  14. ) -> None:
  15. """Table
  16. A table based on Quasar's `QTable <https://quasar.dev/vue-components/table>`_ component.
  17. :param columns: list of column objects
  18. :param rows: list of row objects
  19. :param row_key: name of the column containing unique data identifying the row (default: "id")
  20. :param title: title of the table
  21. :param selection: selection type ("single" or "multiple"; default: `None`)
  22. :param pagination: A dictionary correlating to a pagination object or number of rows per page (`None` hides the pagination, 0 means "infinite"; default: `None`).
  23. :param on_select: callback which is invoked when the selection changes
  24. If selection is 'single' or 'multiple', then a `selected` property is accessible containing the selected rows.
  25. """
  26. super().__init__()
  27. self.rows = rows
  28. self.row_key = row_key
  29. self.selected: List[Dict] = []
  30. self._props['columns'] = columns
  31. self._props['rows'] = rows
  32. self._props['row-key'] = row_key
  33. self._props['title'] = title
  34. self._props['hide-pagination'] = pagination is None
  35. self._props['pagination'] = pagination if isinstance(pagination, dict) else {'rowsPerPage': pagination or 0}
  36. self._props['selection'] = selection or 'none'
  37. self._props['selected'] = self.selected
  38. self._props['fullscreen'] = False
  39. def handle_selection(e: GenericEventArguments) -> None:
  40. if e.args['added']:
  41. if selection == 'single':
  42. self.selected.clear()
  43. self.selected.extend(e.args['rows'])
  44. else:
  45. self.selected[:] = [row for row in self.selected if row[row_key] not in e.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, ['added', 'rows', 'keys'])
  50. @property
  51. def is_fullscreen(self) -> bool:
  52. """Whether the table is in fullscreen mode."""
  53. return self._props['fullscreen']
  54. @is_fullscreen.setter
  55. def is_fullscreen(self, value: bool) -> None:
  56. """Set fullscreen mode."""
  57. self._props['fullscreen'] = value
  58. self.update()
  59. def set_fullscreen(self, value: bool) -> None:
  60. """Set fullscreen mode."""
  61. self.is_fullscreen = value
  62. def toggle_fullscreen(self) -> None:
  63. """Toggle fullscreen mode."""
  64. self.is_fullscreen = not self.is_fullscreen
  65. def add_rows(self, *rows: Dict) -> None:
  66. """Add rows to the table."""
  67. self.rows.extend(rows)
  68. self.update()
  69. def remove_rows(self, *rows: Dict) -> None:
  70. """Remove rows from the table."""
  71. keys = [row[self.row_key] for row in rows]
  72. self.rows[:] = [row for row in self.rows if row[self.row_key] not in keys]
  73. self.selected[:] = [row for row in self.selected if row[self.row_key] not in keys]
  74. self.update()
  75. class row(Element):
  76. def __init__(self) -> None:
  77. super().__init__('q-tr')
  78. class header(Element):
  79. def __init__(self) -> None:
  80. super().__init__('q-th')
  81. class cell(Element):
  82. def __init__(self, key: str = '') -> None:
  83. super().__init__('q-td')
  84. if key:
  85. self._props['key'] = key