checking-tree.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <el-tree ref="treeRef" :data="data" show-checkbox default-expand-all node-key="id" highlight-current :props="defaultProps" />
  3. <div class="buttons">
  4. <el-button @click="getCheckedNodes">get by node</el-button>
  5. <el-button @click="getCheckedKeys">get by key</el-button>
  6. <el-button @click="setCheckedNodes">set by node</el-button>
  7. <el-button @click="setCheckedKeys">set by key</el-button>
  8. <el-button @click="resetChecked">reset</el-button>
  9. </div>
  10. </template>
  11. <script lang="ts" setup>
  12. import { ref } from 'vue'
  13. import type { ElTree } from 'element-plus'
  14. import type Node from 'element-plus/es/components/tree/src/model/node'
  15. interface Tree {
  16. id: number
  17. label: string
  18. children?: Tree[]
  19. }
  20. const treeRef = ref<InstanceType<typeof ElTree>>()
  21. const getCheckedNodes = () => {
  22. console.log(treeRef.value!.getCheckedNodes(false, false))
  23. }
  24. const getCheckedKeys = () => {
  25. console.log(treeRef.value!.getCheckedKeys(false))
  26. }
  27. const setCheckedNodes = () => {
  28. treeRef.value!.setCheckedNodes(
  29. [
  30. {
  31. id: 5,
  32. label: 'Level two 2-1',
  33. },
  34. {
  35. id: 9,
  36. label: 'Level three 1-1-1',
  37. },
  38. ] as Node[],
  39. false
  40. )
  41. }
  42. const setCheckedKeys = () => {
  43. treeRef.value!.setCheckedKeys([3], false)
  44. }
  45. const resetChecked = () => {
  46. treeRef.value!.setCheckedKeys([], false)
  47. }
  48. const defaultProps = {
  49. children: 'children',
  50. label: 'label',
  51. }
  52. const data: Tree[] = [
  53. {
  54. id: 1,
  55. label: 'Level one 1',
  56. children: [
  57. {
  58. id: 4,
  59. label: 'Level two 1-1',
  60. children: [
  61. {
  62. id: 9,
  63. label: 'Level three 1-1-1',
  64. },
  65. {
  66. id: 10,
  67. label: 'Level three 1-1-2',
  68. },
  69. ],
  70. },
  71. ],
  72. },
  73. {
  74. id: 2,
  75. label: 'Level one 2',
  76. children: [
  77. {
  78. id: 5,
  79. label: 'Level two 2-1',
  80. },
  81. {
  82. id: 6,
  83. label: 'Level two 2-2',
  84. },
  85. ],
  86. },
  87. {
  88. id: 3,
  89. label: 'Level one 3',
  90. children: [
  91. {
  92. id: 7,
  93. label: 'Level two 3-1',
  94. },
  95. {
  96. id: 8,
  97. label: 'Level two 3-2',
  98. },
  99. ],
  100. },
  101. ]
  102. </script>