tree-data.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <el-table-v2
  3. v-model:expanded-row-keys="expandedRowKeys"
  4. :columns="columns"
  5. :data="treeData"
  6. :width="700"
  7. :expand-column-key="expandColumnKey"
  8. :height="400"
  9. fixed
  10. @row-expand="onRowExpanded"
  11. @expanded-rows-change="onExpandedRowsChange"
  12. />
  13. </template>
  14. <script lang="ts" setup>
  15. import { computed, ref } from 'vue'
  16. import { TableV2FixedDir } from 'element-plus'
  17. import type { ExpandedRowsChangeHandler, RowExpandHandler } from 'element-plus'
  18. const generateColumns = (length = 10, prefix = 'column-', props?: any) =>
  19. Array.from({ length }).map((_, columnIndex) => ({
  20. ...props,
  21. key: `${prefix}${columnIndex}`,
  22. dataKey: `${prefix}${columnIndex}`,
  23. title: `Column ${columnIndex}`,
  24. width: 150,
  25. }))
  26. const generateData = (columns: ReturnType<typeof generateColumns>, length = 200, prefix = 'row-') =>
  27. Array.from({ length }).map((_, rowIndex) => {
  28. return columns.reduce(
  29. (rowData, column, columnIndex) => {
  30. rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
  31. return rowData
  32. },
  33. {
  34. id: `${prefix}${rowIndex}`,
  35. parentId: null,
  36. }
  37. )
  38. })
  39. const columns = generateColumns(10).map((column, columnIndex) => {
  40. let fixed!: TableV2FixedDir
  41. if (columnIndex < 2) fixed = TableV2FixedDir.LEFT
  42. if (columnIndex > 8) fixed = TableV2FixedDir.RIGHT
  43. return { ...column, fixed }
  44. })
  45. const data = generateData(columns, 200)
  46. const expandColumnKey = 'column-0'
  47. // add some sub items
  48. for (let i = 0; i < 50; i++) {
  49. data.push(
  50. {
  51. ...data[0],
  52. id: `${data[0].id}-sub-${i}`,
  53. parentId: data[0].id,
  54. [expandColumnKey]: `Sub ${i}`,
  55. },
  56. {
  57. ...data[2],
  58. id: `${data[2].id}-sub-${i}`,
  59. parentId: data[2].id,
  60. [expandColumnKey]: `Sub ${i}`,
  61. },
  62. {
  63. ...data[2],
  64. id: `${data[2].id}-sub-sub-${i}`,
  65. parentId: `${data[2].id}-sub-${i}`,
  66. [expandColumnKey]: `Sub-Sub ${i}`,
  67. }
  68. )
  69. }
  70. function unflatten(data: ReturnType<typeof generateData>, rootId = null, dataKey = 'id', parentKey = 'parentId') {
  71. const tree: any[] = []
  72. const childrenMap = {}
  73. for (const datum of data) {
  74. const item = { ...datum }
  75. const id = item[dataKey]
  76. const parentId = item[parentKey]
  77. if (Array.isArray(item.children)) {
  78. childrenMap[id] = item.children.concat(childrenMap[id] || [])
  79. } else if (!childrenMap[id]) {
  80. childrenMap[id] = []
  81. }
  82. item.children = childrenMap[id]
  83. if (parentId !== undefined && parentId !== rootId) {
  84. if (!childrenMap[parentId]) childrenMap[parentId] = []
  85. childrenMap[parentId].push(item)
  86. } else {
  87. tree.push(item)
  88. }
  89. }
  90. return tree
  91. }
  92. const treeData = computed(() => unflatten(data))
  93. const expandedRowKeys = ref<string[]>([])
  94. const onRowExpanded = ({ expanded }: Parameters<RowExpandHandler<any>>[0]) => {
  95. console.log('Expanded:', expanded)
  96. }
  97. const onExpandedRowsChange = (expandedKeys: Parameters<ExpandedRowsChangeHandler>[0]) => {
  98. console.log(expandedKeys)
  99. }
  100. </script>