table_documentation.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. Here is a demo of how to create a table from a pandas dataframe.
  117. ''')
  118. def table_from_pandas_demo():
  119. import pandas as pd
  120. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  121. ui.table(
  122. columns=[{'name': col, 'label': col, 'field': col} for col in df.columns],
  123. rows=df.to_dict('records'),
  124. )
  125. @text_demo('Adding rows', '''
  126. It's simple to add new rows with the `add_rows(dict)` method.
  127. ''')
  128. def adding_rows():
  129. import os
  130. import random
  131. def add():
  132. item = os.urandom(10 // 2).hex()
  133. table.add_rows({'id': item, 'count': random.randint(0, 100)})
  134. ui.button('add', on_click=add)
  135. columns = [
  136. {'name': 'id', 'label': 'ID', 'field': 'id'},
  137. {'name': 'count', 'label': 'Count', 'field': 'count'},
  138. ]
  139. table = ui.table(columns=columns, rows=[], row_key='id').classes('w-full')
  140. @text_demo('Custom sorting and formatting', '''
  141. You can define dynamic column attributes using a `:` prefix.
  142. This way you can define custom sorting and formatting functions.
  143. The following example allows sorting the `name` column by length.
  144. The `age` column is formatted to show the age in years.
  145. ''')
  146. def custom_formatting():
  147. columns = [
  148. {
  149. 'name': 'name',
  150. 'label': 'Name',
  151. 'field': 'name',
  152. 'sortable': True,
  153. ':sort': '(a, b, rowA, rowB) => b.length - a.length',
  154. },
  155. {
  156. 'name': 'age',
  157. 'label': 'Age',
  158. 'field': 'age',
  159. ':format': 'value => value + " years"',
  160. },
  161. ]
  162. rows = [
  163. {'name': 'Alice', 'age': 18},
  164. {'name': 'Bob', 'age': 21},
  165. {'name': 'Carl', 'age': 42},
  166. ]
  167. ui.table(columns=columns, rows=rows, row_key='name')
  168. @text_demo('Toggle fullscreen', '''
  169. You can toggle the fullscreen mode of a table using the `toggle_fullscreen()` method.
  170. ''')
  171. def toggle_fullscreen():
  172. table = ui.table(
  173. columns=[{'name': 'name', 'label': 'Name', 'field': 'name'}],
  174. rows=[{'name': 'Alice'}, {'name': 'Bob'}, {'name': 'Carol'}],
  175. ).classes('w-full')
  176. with table.add_slot('top-left'):
  177. def toggle() -> None:
  178. table.toggle_fullscreen()
  179. button.props('icon=fullscreen_exit' if table.is_fullscreen else 'icon=fullscreen')
  180. button = ui.button('Toggle fullscreen', icon='fullscreen', on_click=toggle).props('flat')
  181. @text_demo('Pagination', '''
  182. You can provide either a single integer or a dictionary to define pagination.
  183. The dictionary can contain the following keys:
  184. - `rowsPerPage`: The number of rows per page.
  185. - `sortBy`: The column name to sort by.
  186. - `descending`: Whether to sort in descending order.
  187. - `page`: The current page (1-based).
  188. ''')
  189. def pagination() -> None:
  190. columns = [
  191. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  192. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  193. ]
  194. rows = [
  195. {'name': 'Elsa', 'age': 18},
  196. {'name': 'Oaken', 'age': 46},
  197. {'name': 'Hans', 'age': 20},
  198. {'name': 'Sven'},
  199. {'name': 'Olaf', 'age': 4},
  200. {'name': 'Anna', 'age': 17},
  201. ]
  202. ui.table(columns=columns, rows=rows, pagination=3)
  203. ui.table(columns=columns, rows=rows, pagination={'rowsPerPage': 4, 'sortBy': 'age', 'page': 2})
  204. @text_demo('Computed fields', '''
  205. You can use functions to compute the value of a column.
  206. The function receives the row as an argument.
  207. See the [Quasar documentation](https://quasar.dev/vue-components/table#defining-the-columns) for more information.
  208. ''')
  209. def computed_fields():
  210. columns = [
  211. {'name': 'name', 'label': 'Name', 'field': 'name', 'align': 'left'},
  212. {'name': 'length', 'label': 'Length', ':field': 'row => row.name.length'},
  213. ]
  214. rows = [
  215. {'name': 'Alice'},
  216. {'name': 'Bob'},
  217. {'name': 'Christopher'},
  218. ]
  219. ui.table(columns=columns, rows=rows, row_key='name')