table_documentation.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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('Handle pagination changes', '''
  204. You can handle pagination changes using the `on_pagination_change` parameter.
  205. ''')
  206. def handle_pagination_changes() -> None:
  207. ui.table(
  208. columns=[{'id': 'Name', 'label': 'Name', 'field': 'Name', 'align': 'left'}],
  209. rows=[{'Name': f'Person {i}'} for i in range(100)],
  210. pagination=3,
  211. on_pagination_change=lambda e: ui.notify(e.value),
  212. )
  213. @doc.demo('Computed fields', '''
  214. You can use functions to compute the value of a column.
  215. The function receives the row as an argument.
  216. See the [Quasar documentation](https://quasar.dev/vue-components/table#defining-the-columns) for more information.
  217. ''')
  218. def computed_fields():
  219. columns = [
  220. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  221. {'name': 'length', 'label': 'Length', ':field': 'row => row.name.length'},
  222. ]
  223. rows = [
  224. {'name': 'Alice'},
  225. {'name': 'Bob'},
  226. {'name': 'Christopher'},
  227. ]
  228. ui.table(columns=columns, rows=rows, row_key='name')
  229. @doc.demo('Conditional formatting', '''
  230. You can use scoped slots to conditionally format the content of a cell.
  231. See the [Quasar documentation](https://quasar.dev/vue-components/table#example--body-cell-slot)
  232. for more information about body-cell slots.
  233. In this demo we use a `q-badge` to display the age in red if the person is under 21 years old.
  234. We use the `body-cell-age` slot to insert the `q-badge` into the `age` column.
  235. The ":color" attribute of the `q-badge` is set to "red" if the age is under 21, otherwise it is set to "green".
  236. The colon in front of the "color" attribute indicates that the value is a JavaScript expression.
  237. ''')
  238. def conditional_formatting():
  239. columns = [
  240. {'name': 'name', 'label': 'Name', 'field': 'name'},
  241. {'name': 'age', 'label': 'Age', 'field': 'age'},
  242. ]
  243. rows = [
  244. {'name': 'Alice', 'age': 18},
  245. {'name': 'Bob', 'age': 21},
  246. {'name': 'Carol', 'age': 42},
  247. ]
  248. table = ui.table(columns=columns, rows=rows, row_key='name')
  249. table.add_slot('body-cell-age', '''
  250. <q-td key="age" :props="props">
  251. <q-badge :color="props.value < 21 ? 'red' : 'green'">
  252. {{ props.value }}
  253. </q-badge>
  254. </q-td>
  255. ''')
  256. @doc.demo('Table cells with links', '''
  257. Here is a demo of how to insert links into table cells.
  258. We use the `body-cell-link` slot to insert an `<a>` tag into the `link` column.
  259. ''')
  260. def table_cells_with_links():
  261. columns = [
  262. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  263. {'name': 'link', 'label': 'Link', 'field': 'link', 'align': 'left'},
  264. ]
  265. rows = [
  266. {'name': 'Google', 'link': 'https://google.com'},
  267. {'name': 'Facebook', 'link': 'https://facebook.com'},
  268. {'name': 'Twitter', 'link': 'https://twitter.com'},
  269. ]
  270. table = ui.table(columns=columns, rows=rows, row_key='name')
  271. table.add_slot('body-cell-link', '''
  272. <q-td :props="props">
  273. <a :href="props.value">{{ props.value }}</a>
  274. </q-td>
  275. ''')
  276. @doc.demo('Table with masonry-like grid', '''
  277. You can use the `grid` prop to display the table as a masonry-like grid.
  278. See the [Quasar documentation](https://quasar.dev/vue-components/table#grid-style) for more information.
  279. ''')
  280. def table_with_masonry_like_grid():
  281. columns = [
  282. {'name': 'name', 'label': 'Name', 'field': 'name'},
  283. {'name': 'age', 'label': 'Age', 'field': 'age'},
  284. ]
  285. rows = [
  286. {'name': 'Alice', 'age': 18},
  287. {'name': 'Bob', 'age': 21},
  288. {'name': 'Carol', 'age': 42},
  289. ]
  290. table = ui.table(columns=columns, rows=rows, row_key='name').props('grid')
  291. table.add_slot('item', r'''
  292. <q-card flat bordered :props="props" class="m-1">
  293. <q-card-section class="text-center">
  294. <strong>{{ props.row.name }}</strong>
  295. </q-card-section>
  296. <q-separator />
  297. <q-card-section class="text-center">
  298. <div>{{ props.row.age }} years</div>
  299. </q-card-section>
  300. </q-card>
  301. ''')
  302. doc.reference(ui.table)