table.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. from typing import Optional
  2. from typing_extensions import Literal
  3. from .mixins.filter_element import FilterElement
  4. from ..element import Element
  5. class QTr(Element):
  6. def __init__(self) -> None:
  7. super().__init__('q-tr')
  8. class QTh(Element):
  9. def __init__(self) -> None:
  10. super().__init__('q-th')
  11. class QTd(Element):
  12. def __init__(self, key: str = '') -> None:
  13. super().__init__('q-td')
  14. if key:
  15. self._props['key'] = key
  16. class QTable(FilterElement):
  17. row = QTr
  18. header = QTh
  19. cell = QTd
  20. def __init__(
  21. self,
  22. columns: list,
  23. rows: list,
  24. key: str,
  25. title: Optional[str] = None,
  26. selection: Optional[Literal['single', 'multiple', 'none']] = 'none',
  27. ) -> None:
  28. """QTable
  29. A component that allows you to display using `QTable <https://quasar.dev/vue-components/table>`_ component.
  30. :param columns: A list of column objects (see `column API <https://quasar.dev/vue-components/table#qtable-api>`_)
  31. :param rows: A list of row objects.
  32. :param key: The name of the column containing unique data identifying the row.
  33. :param title: The title of the table.
  34. :param selection: defines the selection behavior.
  35. 'single': only one cell can be selected at a time.
  36. 'multiple': more than one cell can be selected at a time.
  37. 'none': no cells can be selected.
  38. If selection is passed to 'single' or 'multiple', then a `selected` property is accessible containing
  39. the selected rows.
  40. """
  41. super().__init__(tag='q-table')
  42. self._props['columns'] = columns
  43. self._props['rows'] = rows
  44. self._props['row-key'] = key
  45. self._props['title'] = title
  46. self.selected: list = []
  47. self._props['selected'] = self.selected
  48. self._props['selection'] = selection
  49. self.on('selection', handler=self.on_selection_change)
  50. def on_selection_change(self, data):
  51. self.selected = data['args']
  52. self._props['selected'] = data['args']['rows']
  53. self.update()