table_documentation.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. from nicegui import ui
  2. from . import doc
  3. @doc.demo(ui.table)
  4. def main_demo() -> None:
  5. columns = [
  6. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  7. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  8. ]
  9. rows = [
  10. {'name': 'Alice', 'age': 18},
  11. {'name': 'Bob', 'age': 21},
  12. {'name': 'Carol'},
  13. ]
  14. ui.table(columns=columns, rows=rows, row_key='name')
  15. @doc.demo('Table with expandable rows', '''
  16. Scoped slots can be used to insert buttons that toggle the expand state of a table row.
  17. See the [Quasar documentation](https://quasar.dev/vue-components/table#expanding-rows) for more information.
  18. ''')
  19. def table_with_expandable_rows():
  20. columns = [
  21. {'name': 'name', 'label': 'Name', 'field': 'name'},
  22. {'name': 'age', 'label': 'Age', 'field': 'age'},
  23. ]
  24. rows = [
  25. {'name': 'Alice', 'age': 18},
  26. {'name': 'Bob', 'age': 21},
  27. {'name': 'Carol'},
  28. ]
  29. table = ui.table(columns=columns, rows=rows, row_key='name').classes('w-72')
  30. table.add_slot('header', r'''
  31. <q-tr :props="props">
  32. <q-th auto-width />
  33. <q-th v-for="col in props.cols" :key="col.name" :props="props">
  34. {{ col.label }}
  35. </q-th>
  36. </q-tr>
  37. ''')
  38. table.add_slot('body', r'''
  39. <q-tr :props="props">
  40. <q-td auto-width>
  41. <q-btn size="sm" color="accent" round dense
  42. @click="props.expand = !props.expand"
  43. :icon="props.expand ? 'remove' : 'add'" />
  44. </q-td>
  45. <q-td v-for="col in props.cols" :key="col.name" :props="props">
  46. {{ col.value }}
  47. </q-td>
  48. </q-tr>
  49. <q-tr v-show="props.expand" :props="props">
  50. <q-td colspan="100%">
  51. <div class="text-left">This is {{ props.row.name }}.</div>
  52. </q-td>
  53. </q-tr>
  54. ''')
  55. @doc.demo('Show and hide columns', '''
  56. Here is an example of how to show and hide columns in a table.
  57. ''')
  58. def show_and_hide_columns():
  59. from typing import Dict
  60. columns = [
  61. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  62. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  63. ]
  64. rows = [
  65. {'name': 'Alice', 'age': 18},
  66. {'name': 'Bob', 'age': 21},
  67. {'name': 'Carol'},
  68. ]
  69. table = ui.table(columns=columns, rows=rows, row_key='name')
  70. def toggle(column: Dict, visible: bool) -> None:
  71. column['classes'] = '' if visible else 'hidden'
  72. column['headerClasses'] = '' if visible else 'hidden'
  73. table.update()
  74. with ui.button(icon='menu'):
  75. with ui.menu(), ui.column().classes('gap-0 p-2'):
  76. for column in columns:
  77. ui.switch(column['label'], value=True, on_change=lambda e,
  78. column=column: toggle(column, e.value))
  79. @doc.demo('Table with drop down selection', '''
  80. Here is an example of how to use a drop down selection in a table.
  81. After emitting a `rename` event from the scoped slot, the `rename` function updates the table rows.
  82. ''')
  83. def table_with_drop_down_selection():
  84. from nicegui import events
  85. columns = [
  86. {'name': 'name', 'label': 'Name', 'field': 'name'},
  87. {'name': 'age', 'label': 'Age', 'field': 'age'},
  88. ]
  89. rows = [
  90. {'id': 0, 'name': 'Alice', 'age': 18},
  91. {'id': 1, 'name': 'Bob', 'age': 21},
  92. {'id': 2, 'name': 'Carol'},
  93. ]
  94. name_options = ['Alice', 'Bob', 'Carol']
  95. def rename(e: events.GenericEventArguments) -> None:
  96. for row in rows:
  97. if row['id'] == e.args['id']:
  98. row['name'] = e.args['name']
  99. ui.notify(f'Table.rows is now: {table.rows}')
  100. table = ui.table(columns=columns, rows=rows, row_key='name').classes('w-full')
  101. table.add_slot('body', r'''
  102. <q-tr :props="props">
  103. <q-td key="name" :props="props">
  104. <q-select
  105. v-model="props.row.name"
  106. :options="''' + str(name_options) + r'''"
  107. @update:model-value="() => $parent.$emit('rename', props.row)"
  108. />
  109. </q-td>
  110. <q-td key="age" :props="props">
  111. {{ props.row.age }}
  112. </q-td>
  113. </q-tr>
  114. ''')
  115. table.on('rename', rename)
  116. @doc.demo('Table from Pandas DataFrame', '''
  117. You can create a table from a Pandas DataFrame using the `from_pandas` method.
  118. This method takes a Pandas DataFrame as input and returns a table.
  119. ''')
  120. def table_from_pandas_demo():
  121. import pandas as pd
  122. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  123. ui.table.from_pandas(df).classes('max-h-40')
  124. @doc.demo('Adding rows', '''
  125. It's simple to add new rows with the `add_rows(dict)` method.
  126. ''')
  127. def adding_rows():
  128. import os
  129. import random
  130. def add():
  131. item = os.urandom(10 // 2).hex()
  132. table.add_rows({'id': item, 'count': random.randint(0, 100)})
  133. ui.button('add', on_click=add)
  134. columns = [
  135. {'name': 'id', 'label': 'ID', 'field': 'id'},
  136. {'name': 'count', 'label': 'Count', 'field': 'count'},
  137. ]
  138. table = ui.table(columns=columns, rows=[], row_key='id').classes('w-full')
  139. @doc.demo('Custom sorting and formatting', '''
  140. You can define dynamic column attributes using a `:` prefix.
  141. This way you can define custom sorting and formatting functions.
  142. The following example allows sorting the `name` column by length.
  143. The `age` column is formatted to show the age in years.
  144. ''')
  145. def custom_formatting():
  146. columns = [
  147. {
  148. 'name': 'name',
  149. 'label': 'Name',
  150. 'field': 'name',
  151. 'sortable': True,
  152. ':sort': '(a, b, rowA, rowB) => b.length - a.length',
  153. },
  154. {
  155. 'name': 'age',
  156. 'label': 'Age',
  157. 'field': 'age',
  158. ':format': 'value => value + " years"',
  159. },
  160. ]
  161. rows = [
  162. {'name': 'Alice', 'age': 18},
  163. {'name': 'Bob', 'age': 21},
  164. {'name': 'Carl', 'age': 42},
  165. ]
  166. ui.table(columns=columns, rows=rows, row_key='name')
  167. @doc.demo('Toggle fullscreen', '''
  168. You can toggle the fullscreen mode of a table using the `toggle_fullscreen()` method.
  169. ''')
  170. def toggle_fullscreen():
  171. table = ui.table(
  172. columns=[{'name': 'name', 'label': 'Name', 'field': 'name'}],
  173. rows=[{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
  174. ).classes('w-full')
  175. with table.add_slot('top-left'):
  176. def toggle() -> None:
  177. table.toggle_fullscreen()
  178. button.props('icon=fullscreen_exit' if table.is_fullscreen else 'icon=fullscreen')
  179. button = ui.button('Toggle fullscreen', icon='fullscreen', on_click=toggle).props('flat')
  180. @doc.demo('Pagination', '''
  181. You can provide either a single integer or a dictionary to define pagination.
  182. The dictionary can contain the following keys:
  183. - `rowsPerPage`: The number of rows per page.
  184. - `sortBy`: The column name to sort by.
  185. - `descending`: Whether to sort in descending order.
  186. - `page`: The current page (1-based).
  187. ''')
  188. def pagination() -> None:
  189. columns = [
  190. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  191. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  192. ]
  193. rows = [
  194. {'name': 'Elsa', 'age': 18},
  195. {'name': 'Oaken', 'age': 46},
  196. {'name': 'Hans', 'age': 20},
  197. {'name': 'Sven'},
  198. {'name': 'Olaf', 'age': 4},
  199. {'name': 'Anna', 'age': 17},
  200. ]
  201. ui.table(columns=columns, rows=rows, pagination=3)
  202. ui.table(columns=columns, rows=rows, pagination={'rowsPerPage': 4, 'sortBy': 'age', 'page': 2})
  203. @doc.demo('Computed fields', '''
  204. You can use functions to compute the value of a column.
  205. The function receives the row as an argument.
  206. See the [Quasar documentation](https://quasar.dev/vue-components/table#defining-the-columns) for more information.
  207. ''')
  208. def computed_fields():
  209. columns = [
  210. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  211. {'name': 'length', 'label': 'Length', ':field': 'row => row.name.length'},
  212. ]
  213. rows = [
  214. {'name': 'Alice'},
  215. {'name': 'Bob'},
  216. {'name': 'Christopher'},
  217. ]
  218. ui.table(columns=columns, rows=rows, row_key='name')
  219. @doc.demo('Conditional formatting', '''
  220. You can use scoped slots to conditionally format the content of a cell.
  221. See the [Quasar documentation](https://quasar.dev/vue-components/table#example--body-cell-slot)
  222. for more information about body-cell slots.
  223. In this demo we use a `q-badge` to display the age in red if the person is under 21 years old.
  224. We use the `body-cell-age` slot to insert the `q-badge` into the `age` column.
  225. The ":color" attribute of the `q-badge` is set to "red" if the age is under 21, otherwise it is set to "green".
  226. The colon in front of the "color" attribute indicates that the value is a JavaScript expression.
  227. ''')
  228. def conditional_formatting():
  229. columns = [
  230. {'name': 'name', 'label': 'Name', 'field': 'name'},
  231. {'name': 'age', 'label': 'Age', 'field': 'age'},
  232. ]
  233. rows = [
  234. {'name': 'Alice', 'age': 18},
  235. {'name': 'Bob', 'age': 21},
  236. {'name': 'Carol', 'age': 42},
  237. ]
  238. table = ui.table(columns=columns, rows=rows, row_key='name')
  239. table.add_slot('body-cell-age', '''
  240. <q-td key="age" :props="props">
  241. <q-badge :color="props.value < 21 ? 'red' : 'green'">
  242. {{ props.value }}
  243. </q-badge>
  244. </q-td>
  245. ''')
  246. @doc.demo('Table cells with links', '''
  247. Here is a demo of how to insert links into table cells.
  248. We use the `body-cell-link` slot to insert an `<a>` tag into the `link` column.
  249. ''')
  250. def table_cells_with_links():
  251. columns = [
  252. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  253. {'name': 'link', 'label': 'Link', 'field': 'link', 'align': 'left'},
  254. ]
  255. rows = [
  256. {'name': 'Google', 'link': 'https://google.com'},
  257. {'name': 'Facebook', 'link': 'https://facebook.com'},
  258. {'name': 'Twitter', 'link': 'https://twitter.com'},
  259. ]
  260. table = ui.table(columns=columns, rows=rows, row_key='name')
  261. table.add_slot('body-cell-link', '''
  262. <q-td :props="props">
  263. <a :href="props.value">{{ props.value }}</a>
  264. </q-td>
  265. ''')
  266. @doc.demo('Table with masonry-like grid', '''
  267. You can use the `grid` prop to display the table as a masonry-like grid.
  268. See the [Quasar documentation](https://quasar.dev/vue-components/table#grid-style) for more information.
  269. ''')
  270. def table_with_masonry_like_grid():
  271. columns = [
  272. {'name': 'name', 'label': 'Name', 'field': 'name'},
  273. {'name': 'age', 'label': 'Age', 'field': 'age'},
  274. ]
  275. rows = [
  276. {'name': 'Alice', 'age': 18},
  277. {'name': 'Bob', 'age': 21},
  278. {'name': 'Carol', 'age': 42},
  279. ]
  280. table = ui.table(columns=columns, rows=rows, row_key='name').props('grid')
  281. table.add_slot('item', r'''
  282. <q-card flat bordered :props="props" class="m-1">
  283. <q-card-section class="text-center">
  284. <strong>{{ props.row.name }}</strong>
  285. </q-card-section>
  286. <q-separator />
  287. <q-card-section class="text-center">
  288. <div>{{ props.row.age }} years</div>
  289. </q-card-section>
  290. </q-card>
  291. ''')
  292. doc.reference(ui.table)