TestPermissionTree.vue 3.0 KB

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