table_documentation.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. with ui.table(columns=columns, rows=rows, row_key='name').classes('w-72') as table:
  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))