checking-tree.vue 2.2 KB

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