table_documentation.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. columns = [
  5. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  6. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  7. ]
  8. rows = [
  9. {'name': 'Alice', 'age': 18},
  10. {'name': 'Bob', 'age': 21},
  11. {'name': 'Carol'},
  12. ]
  13. ui.table(columns=columns, rows=rows, row_key='name')
  14. def more() -> None:
  15. @text_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. @text_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, column=column: toggle(column, e.value))
  78. @text_demo('Table with drop down selection', '''
  79. Here is an example of how to use a drop down selection in a table.
  80. After emitting a `rename` event from the scoped slot, the `rename` function updates the table rows.
  81. ''')
  82. def table_with_drop_down_selection():
  83. from nicegui import events
  84. columns = [
  85. {'name': 'name', 'label': 'Name', 'field': 'name'},
  86. {'name': 'age', 'label': 'Age', 'field': 'age'},
  87. ]
  88. rows = [
  89. {'id': 0, 'name': 'Alice', 'age': 18},
  90. {'id': 1, 'name': 'Bob', 'age': 21},
  91. {'id': 2, 'name': 'Carol'},
  92. ]
  93. name_options = ['Alice', 'Bob', 'Carol']
  94. def rename(e: events.GenericEventArguments) -> None:
  95. for row in rows:
  96. if row['id'] == e.args['id']:
  97. row['name'] = e.args['name']
  98. ui.notify(f'Table.rows is now: {table.rows}')
  99. table = ui.table(columns=columns, rows=rows, row_key='name').classes('w-full')
  100. table.add_slot('body', r'''
  101. <q-tr :props="props">
  102. <q-td key="name" :props="props">
  103. <q-select
  104. v-model="props.row.name"
  105. :options="''' + str(name_options) + r'''"
  106. @update:model-value="() => $parent.$emit('rename', props.row)"
  107. />
  108. </q-td>
  109. <q-td key="age" :props="props">
  110. {{ props.row.age }}
  111. </q-td>
  112. </q-tr>
  113. ''')
  114. table.on('rename', rename)
  115. @text_demo('Table from Pandas DataFrame', '''
  116. You can create a table from a Pandas DataFrame using the `from_pandas` method.
  117. This method takes a Pandas DataFrame as input and returns a table.
  118. ''')
  119. def table_from_pandas_demo():
  120. import pandas as pd
  121. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  122. ui.table.from_pandas(df).classes('max-h-40')
  123. @text_demo('Adding rows', '''
  124. It's simple to add new rows with the `add_rows(dict)` method.
  125. ''')
  126. def adding_rows():
  127. import os
  128. import random
  129. def add():
  130. item = os.urandom(10 // 2).hex()
  131. table.add_rows({'id': item, 'count': random.randint(0, 100)})
  132. ui.button('add', on_click=add)
  133. columns = [
  134. {'name': 'id', 'label': 'ID', 'field': 'id'},
  135. {'name': 'count', 'label': 'Count', 'field': 'count'},
  136. ]
  137. table = ui.table(columns=columns, rows=[], row_key='id').classes('w-full')
  138. @text_demo('Custom sorting and formatting', '''
  139. You can define dynamic column attributes using a `:` prefix.
  140. This way you can define custom sorting and formatting functions.
  141. The following example allows sorting the `name` column by length.
  142. The `age` column is formatted to show the age in years.
  143. ''')
  144. def custom_formatting():
  145. columns = [
  146. {
  147. 'name': 'name',
  148. 'label': 'Name',
  149. 'field': 'name',
  150. 'sortable': True,
  151. ':sort': '(a, b, rowA, rowB) => b.length - a.length',
  152. },
  153. {
  154. 'name': 'age',
  155. 'label': 'Age',
  156. 'field': 'age',
  157. ':format': 'value => value + " years"',
  158. },
  159. ]
  160. rows = [
  161. {'name': 'Alice', 'age': 18},
  162. {'name': 'Bob', 'age': 21},
  163. {'name': 'Carl', 'age': 42},
  164. ]
  165. ui.table(columns=columns, rows=rows, row_key='name')
  166. @text_demo('Toggle fullscreen', '''
  167. You can toggle the fullscreen mode of a table using the `toggle_fullscreen()` method.
  168. ''')
  169. def toggle_fullscreen():
  170. table = ui.table(
  171. columns=[{'name': 'name', 'label': 'Name', 'field': 'name'}],
  172. rows=[{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
  173. ).classes('w-full')
  174. with table.add_slot('top-left'):
  175. def toggle() -> None:
  176. table.toggle_fullscreen()
  177. button.props('icon=fullscreen_exit' if table.is_fullscreen else 'icon=fullscreen')
  178. button = ui.button('Toggle fullscreen', icon='fullscreen', on_click=toggle).props('flat')
  179. @text_demo('Pagination', '''
  180. You can provide either a single integer or a dictionary to define pagination.
  181. The dictionary can contain the following keys:
  182. - `rowsPerPage`: The number of rows per page.
  183. - `sortBy`: The column name to sort by.
  184. - `descending`: Whether to sort in descending order.
  185. - `page`: The current page (1-based).
  186. ''')
  187. def pagination() -> None:
  188. columns = [
  189. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  190. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  191. ]
  192. rows = [
  193. {'name': 'Elsa', 'age': 18},
  194. {'name': 'Oaken', 'age': 46},
  195. {'name': 'Hans', 'age': 20},
  196. {'name': 'Sven'},
  197. {'name': 'Olaf', 'age': 4},
  198. {'name': 'Anna', 'age': 17},
  199. ]
  200. ui.table(columns=columns, rows=rows, pagination=3)
  201. ui.table(columns=columns, rows=rows, pagination={'rowsPerPage': 4, 'sortBy': 'age', 'page': 2})
  202. @text_demo('Computed fields', '''
  203. You can use functions to compute the value of a column.
  204. The function receives the row as an argument.
  205. See the [Quasar documentation](https://quasar.dev/vue-components/table#defining-the-columns) for more information.
  206. ''')
  207. def computed_fields():
  208. columns = [
  209. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  210. {'name': 'length', 'label': 'Length', ':field': 'row => row.name.length'},
  211. ]
  212. rows = [
  213. {'name': 'Alice'},
  214. {'name': 'Bob'},
  215. {'name': 'Christopher'},
  216. ]
  217. ui.table(columns=columns, rows=rows, row_key='name')
  218. @text_demo('Conditional formatting', '''
  219. You can use scoped slots to conditionally format the content of a cell.
  220. See the [Quasar documentation](https://quasar.dev/vue-components/table#example--body-cell-slot)
  221. for more information about body-cell slots.
  222. In this demo we use a `q-badge` to display the age in red if the person is under 21 years old.
  223. We use the `body-cell-age` slot to insert the `q-badge` into the `age` column.
  224. The ":color" attribute of the `q-badge` is set to "red" if the age is under 21, otherwise it is set to "green".
  225. The colon in front of the "color" attribute indicates that the value is a JavaScript expression.
  226. ''')
  227. def conditional_formatting():
  228. columns = [
  229. {'name': 'name', 'label': 'Name', 'field': 'name'},
  230. {'name': 'age', 'label': 'Age', 'field': 'age'},
  231. ]
  232. rows = [
  233. {'name': 'Alice', 'age': 18},
  234. {'name': 'Bob', 'age': 21},
  235. {'name': 'Carol', 'age': 42},
  236. ]
  237. table = ui.table(columns=columns, rows=rows, row_key='name')
  238. table.add_slot('body-cell-age', '''
  239. <q-td key="age" :props="props">
  240. <q-badge :color="props.value < 21 ? 'red' : 'green'">
  241. {{ props.value }}
  242. </q-badge>
  243. </q-td>
  244. ''')
  245. @text_demo('Table cells with links', '''
  246. Here is a demo of how to insert links into table cells.
  247. We use the `body-cell-link` slot to insert an `<a>` tag into the `link` column.
  248. ''')
  249. def table_cells_with_links():
  250. columns = [
  251. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  252. {'name': 'link', 'label': 'Link', 'field': 'link', 'align': 'left'},
  253. ]
  254. rows = [
  255. {'name': 'Google', 'link': 'https://google.com'},
  256. {'name': 'Facebook', 'link': 'https://facebook.com'},
  257. {'name': 'Twitter', 'link': 'https://twitter.com'},
  258. ]
  259. table = ui.table(columns=columns, rows=rows, row_key='name')
  260. table.add_slot('body-cell-link', '''
  261. <q-td :props="props">
  262. <a :href="props.value">{{ props.value }}</a>
  263. </q-td>
  264. ''')