tree-data.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = (
  27. columns: ReturnType<typeof generateColumns>,
  28. length = 200,
  29. prefix = 'row-'
  30. ) =>
  31. Array.from({ length }).map((_, rowIndex) => {
  32. return columns.reduce(
  33. (rowData, column, columnIndex) => {
  34. rowData[column.dataKey] = `Row ${rowIndex} - Col ${columnIndex}`
  35. return rowData
  36. },
  37. {
  38. id: `${prefix}${rowIndex}`,
  39. parentId: null,
  40. }
  41. )
  42. })
  43. const columns = generateColumns(10).map((column, columnIndex) => {
  44. let fixed!: TableV2FixedDir
  45. if (columnIndex < 2) fixed = TableV2FixedDir.LEFT
  46. if (columnIndex > 8) fixed = TableV2FixedDir.RIGHT
  47. return { ...column, fixed }
  48. })
  49. const data = generateData(columns, 200)
  50. const expandColumnKey = 'column-0'
  51. // add some sub items
  52. for (let i = 0; i < 50; i++) {
  53. data.push(
  54. {
  55. ...data[0],
  56. id: `${data[0].id}-sub-${i}`,
  57. parentId: data[0].id,
  58. [expandColumnKey]: `Sub ${i}`,
  59. },
  60. {
  61. ...data[2],
  62. id: `${data[2].id}-sub-${i}`,
  63. parentId: data[2].id,
  64. [expandColumnKey]: `Sub ${i}`,
  65. },
  66. {
  67. ...data[2],
  68. id: `${data[2].id}-sub-sub-${i}`,
  69. parentId: `${data[2].id}-sub-${i}`,
  70. [expandColumnKey]: `Sub-Sub ${i}`,
  71. }
  72. )
  73. }
  74. function unflatten(
  75. data: ReturnType<typeof generateData>,
  76. rootId = null,
  77. dataKey = 'id',
  78. parentKey = 'parentId'
  79. ) {
  80. const tree: any[] = []
  81. const childrenMap = {}
  82. for (const datum of data) {
  83. const item = { ...datum }
  84. const id = item[dataKey]
  85. const parentId = item[parentKey]
  86. if (Array.isArray(item.children)) {
  87. childrenMap[id] = item.children.concat(childrenMap[id] || [])
  88. } else if (!childrenMap[id]) {
  89. childrenMap[id] = []
  90. }
  91. item.children = childrenMap[id]
  92. if (parentId !== undefined && parentId !== rootId) {
  93. if (!childrenMap[parentId]) childrenMap[parentId] = []
  94. childrenMap[parentId].push(item)
  95. } else {
  96. tree.push(item)
  97. }
  98. }
  99. return tree
  100. }
  101. const treeData = computed(() => unflatten(data))
  102. const expandedRowKeys = ref<string[]>([])
  103. const onRowExpanded = ({ expanded }: Parameters<RowExpandHandler<any>>[0]) => {
  104. console.log('Expanded:', expanded)
  105. }
  106. const onExpandedRowsChange = (
  107. expandedKeys: Parameters<ExpandedRowsChangeHandler>[0]
  108. ) => {
  109. console.log(expandedKeys)
  110. }
  111. </script>