table_documentation.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. columns = [
  60. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  61. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  62. ]
  63. rows = [
  64. {'name': 'Alice', 'age': 18},
  65. {'name': 'Bob', 'age': 21},
  66. {'name': 'Carol'},
  67. ]
  68. visible_columns = {column['name'] for column in columns}
  69. table = ui.table(columns=columns, rows=rows, row_key='name')
  70. def toggle(column: dict, visible: bool) -> None:
  71. if visible:
  72. visible_columns.add(column['name'])
  73. else:
  74. visible_columns.remove(column['name'])
  75. table._props['columns'] = [column for column in columns if column['name'] in visible_columns]
  76. table.update()
  77. with ui.button(on_click=lambda: menu.open()).props('icon=menu'):
  78. with ui.menu() as menu, ui.column().classes('gap-0 p-2'):
  79. for column in columns:
  80. ui.switch(column['label'], value=True, on_change=lambda e, column=column: toggle(column, e.value))
  81. @text_demo('Table with drop down selection', '''
  82. Here is an example of how to use a drop down selection in a table.
  83. After emitting a `rename` event from the scoped slot, the `rename` function updates the table rows.
  84. ''')
  85. def table_with_drop_down_selection():
  86. from typing import Dict
  87. columns = [
  88. {'name': 'name', 'label': 'Name', 'field': 'name'},
  89. {'name': 'age', 'label': 'Age', 'field': 'age'},
  90. ]
  91. rows = [
  92. {'id': 0, 'name': 'Alice', 'age': 18},
  93. {'id': 1, 'name': 'Bob', 'age': 21},
  94. {'id': 2, 'name': 'Carol'},
  95. ]
  96. name_options = ['Alice', 'Bob', 'Carol']
  97. def rename(msg: Dict) -> None:
  98. for row in rows:
  99. if row['id'] == msg['args']['id']:
  100. row['name'] = msg['args']['name']
  101. ui.notify(f'Table.rows is now: {table.rows}')
  102. table = ui.table(columns=columns, rows=rows, row_key='name').classes('w-full')
  103. table.add_slot('body', r'''
  104. <q-tr :props="props">
  105. <q-td key="name" :props="props">
  106. <q-select
  107. v-model="props.row.name"
  108. :options="''' + str(name_options) + r'''"
  109. @update:model-value="() => $parent.$emit('rename', props.row)"
  110. />
  111. </q-td>
  112. <q-td key="age" :props="props">
  113. {{ props.row.age }}
  114. </q-td>
  115. </q-tr>
  116. ''')
  117. table.on('rename', rename)
  118. @text_demo('Table from pandas dataframe', '''
  119. Here is a demo of how to create a table from a pandas dataframe.
  120. ''')
  121. def table_from_pandas_demo():
  122. import pandas as pd
  123. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  124. ui.table(
  125. columns=[{'name': col, 'label': col, 'field': col} for col in df.columns],
  126. rows=df.to_dict('records'),
  127. )
  128. @text_demo('Adding Rows', '''
  129. It's simple to add new rows with the `add_rows(dict)` method.
  130. ''')
  131. def adding_rows():
  132. import os
  133. import random
  134. def add():
  135. item = os.urandom(10 // 2).hex()
  136. table.add_rows({'item_id': item, 'count': random.randint(0, 100)})
  137. ui.button('add', on_click=add)
  138. columns = [
  139. {'name': 'item_id', 'label': 'Id', 'field': 'item_id'},
  140. {'name': 'count', 'label': 'Count', 'field': 'count'},
  141. ]
  142. table = ui.table(columns=columns, rows=[], row_key='item_id').classes('w-full')