TreeTest.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="tree-test">
  3. <el-card shadow="never">
  4. <template #header>
  5. <h3>树形组件测试</h3>
  6. </template>
  7. <div class="test-content">
  8. <p>测试目标:当选中"成都市"时,"四川省"应该保持半选状态,而不是全选状态</p>
  9. <div class="tree-container">
  10. <data-permission-tree :data="treeData" :selected-keys="selectedKeys"
  11. @update:selectedKeys="handleSelectedKeysChange" @check="handleCheck" />
  12. </div>
  13. <div class="info-panel">
  14. <h4>当前选中的节点:</h4>
  15. <p>{{ selectedKeys }}</p>
  16. <h4>当前半选的节点:</h4>
  17. <p>{{ indeterminateKeys }}</p>
  18. </div>
  19. </div>
  20. </el-card>
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import { ref } from 'vue'
  25. import DataPermissionTree from '@/components/DataPermisionTree/index.vue'
  26. import type { TreeData, CheckedKeys } from '@/components/DataPermisionTree/types'
  27. // 树形数据示例 - 模拟地理层级结构
  28. const treeData: TreeData[] = [
  29. {
  30. id: 1,
  31. name: '中国',
  32. children: [
  33. {
  34. id: 2,
  35. name: '四川省',
  36. children: [
  37. { id: 3, name: '成都市' },
  38. { id: 4, name: '绵阳市' },
  39. { id: 5, name: '德阳市' }
  40. ]
  41. },
  42. {
  43. id: 6,
  44. name: '广东省',
  45. children: [
  46. { id: 7, name: '广州市' },
  47. { id: 8, name: '深圳市' },
  48. { id: 9, name: '珠海市' }
  49. ]
  50. }
  51. ]
  52. }
  53. ]
  54. // 选中键值
  55. const selectedKeys = ref<number[]>([])
  56. // 半选键值
  57. const indeterminateKeys = ref<number[]>([])
  58. // 处理选中键值变化
  59. const handleSelectedKeysChange = (keys: number[]) => {
  60. selectedKeys.value = keys
  61. }
  62. // 处理选中事件
  63. const handleCheck = (keys: CheckedKeys) => {
  64. console.log('Checked keys:', keys.checked)
  65. console.log('Indeterminate keys:', keys.indeterminate)
  66. indeterminateKeys.value = keys.indeterminate
  67. }
  68. </script>
  69. <style scoped>
  70. .tree-test {
  71. padding: 20px;
  72. max-width: 800px;
  73. margin: 0 auto;
  74. }
  75. .test-content {
  76. padding: 20px 0;
  77. }
  78. .tree-container {
  79. margin: 20px 0;
  80. padding: 15px;
  81. border: 1px solid #e4e7ed;
  82. border-radius: 4px;
  83. background-color: #f5f7fa;
  84. min-height: 200px;
  85. }
  86. .info-panel {
  87. margin-top: 20px;
  88. padding: 15px;
  89. background-color: #ecf5ff;
  90. border-radius: 4px;
  91. }
  92. .info-panel h4 {
  93. margin-top: 0;
  94. color: #303133;
  95. }
  96. .info-panel p {
  97. font-family: monospace;
  98. background-color: #fff;
  99. padding: 10px;
  100. border-radius: 4px;
  101. border: 1px solid #e4e7ed;
  102. margin: 10px 0 0 0;
  103. }
  104. </style>