1
0

table_reference.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from nicegui import ui
  2. from ..reference_tools import element_example, text_example
  3. def intro() -> None:
  4. @element_example(ui.table)
  5. def table_example():
  6. columns = [
  7. {'name': 'name', 'label': 'Name', 'field': 'name', 'required': True, 'align': 'left'},
  8. {'name': 'age', 'label': 'Age', 'field': 'age', 'sortable': True},
  9. ]
  10. rows = [
  11. {'name': 'Alice', 'age': 18},
  12. {'name': 'Bob', 'age': 21},
  13. {'name': 'Carol'},
  14. ]
  15. ui.table(columns=columns, rows=rows, row_key='name')
  16. def more() -> None:
  17. @text_example('Table with expandable rows', '''
  18. Scoped slots can be used to insert buttons that toggle the expand state of a table row.
  19. See the [Quasar documentation](https://quasar.dev/vue-components/table#expanding-rows) for more information.
  20. ''')
  21. def table_with_expandable_rows():
  22. columns = [
  23. {'name': 'name', 'label': 'Name', 'field': 'name'},
  24. {'name': 'age', 'label': 'Age', 'field': 'age'},
  25. ]
  26. rows = [
  27. {'name': 'Alice', 'age': 18},
  28. {'name': 'Bob', 'age': 21},
  29. {'name': 'Carol'},
  30. ]
  31. with ui.table(columns=columns, rows=rows, row_key='name').classes('w-72') as table:
  32. table.add_slot('header', r'''
  33. <q-tr :props="props">
  34. <q-th auto-width />
  35. <q-th v-for="col in props.cols" :key="col.name" :props="props">
  36. {{ col.label }}
  37. </q-th>
  38. </q-tr>
  39. ''')
  40. table.add_slot('body', r'''
  41. <q-tr :props="props">
  42. <q-td auto-width>
  43. <q-btn size="sm" color="accent" round dense
  44. @click="props.expand = !props.expand"
  45. :icon="props.expand ? 'remove' : 'add'" />
  46. </q-td>
  47. <q-td v-for="col in props.cols" :key="col.name" :props="props">
  48. {{ col.value }}
  49. </q-td>
  50. </q-tr>
  51. <q-tr v-show="props.expand" :props="props">
  52. <q-td colspan="100%">
  53. <div class="text-left">This is {{ props.row.name }}.</div>
  54. </q-td>
  55. </q-tr>
  56. ''')