table_documentation.py 5.9 KB

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