1
0

ActionTree.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <template>
  2. <PageWrapper title="Tree函数操作示例" contentBackground contentClass="p-4">
  3. <div class="mb-4">
  4. <a-button @click="handleLevel(2)" class="mr-2"> 显示到第2级 </a-button>
  5. <a-button @click="handleLevel(1)" class="mr-2"> 显示到第1级 </a-button>
  6. <a-button @click="handleSetCheckData" class="mr-2"> 设置勾选数据 </a-button>
  7. <a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button>
  8. <a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button>
  9. <a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button>
  10. <a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
  11. <a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
  12. </div>
  13. <div class="mb-4">
  14. <a-button @click="appendNodeByKey(null)" class="mr-2"> 添加根节点 </a-button>
  15. <a-button @click="appendNodeByKey('2-2')" class="mr-2"> 添加在parent3内添加节点 </a-button>
  16. <a-button @click="deleteNodeByKey('2-2')" class="mr-2"> 删除parent3节点 </a-button>
  17. <a-button @click="updateNodeByKey('1-1')" class="mr-2"> 更新parent2节点 </a-button>
  18. </div>
  19. <CollapseContainer title="函数操作" class="mr-4" :canExpan="false" :style="{ width: '33%' }">
  20. <BasicTree :treeData="treeData" ref="treeRef" :checkable="true" />
  21. </CollapseContainer>
  22. </PageWrapper>
  23. </template>
  24. <script lang="ts">
  25. import { defineComponent, ref, unref } from 'vue';
  26. import { BasicTree, TreeActionType } from '/@/components/Tree/index';
  27. import { treeData } from './data';
  28. import { CollapseContainer } from '/@/components/Container/index';
  29. import { useMessage } from '/@/hooks/web/useMessage';
  30. import { PageWrapper } from '/@/components/Page';
  31. export default defineComponent({
  32. components: { BasicTree, CollapseContainer, PageWrapper },
  33. setup() {
  34. const treeRef = ref<Nullable<TreeActionType>>(null);
  35. const { createMessage } = useMessage();
  36. function getTree() {
  37. const tree = unref(treeRef);
  38. if (!tree) {
  39. throw new Error('tree is null!');
  40. }
  41. return tree;
  42. }
  43. function handleLevel(level: number) {
  44. getTree().filterByLevel(level);
  45. }
  46. function handleSetCheckData() {
  47. getTree().setCheckedKeys(['0-0']);
  48. }
  49. function handleGetCheckData() {
  50. const keys = getTree().getCheckedKeys();
  51. createMessage.success(JSON.stringify(keys));
  52. }
  53. function handleSetSelectData() {
  54. getTree().setSelectedKeys(['0-0']);
  55. }
  56. function handleGetSelectData() {
  57. const keys = getTree().getSelectedKeys();
  58. createMessage.success(JSON.stringify(keys));
  59. }
  60. function handleSetExpandData() {
  61. getTree().setExpandedKeys(['0-0']);
  62. }
  63. function handleGetExpandData() {
  64. const keys = getTree().getExpandedKeys();
  65. createMessage.success(JSON.stringify(keys));
  66. }
  67. function appendNodeByKey(parentKey: string | null = null) {
  68. getTree().insertNodeByKey({
  69. parentKey: parentKey,
  70. node: {
  71. title: '新增节点',
  72. key: '2-2-2',
  73. },
  74. // 往后插入
  75. push: 'push',
  76. // 往前插入
  77. // push:'unshift'
  78. });
  79. }
  80. function deleteNodeByKey(key: string) {
  81. getTree().deleteNodeByKey(key);
  82. }
  83. function updateNodeByKey(key: string) {
  84. getTree().updateNodeByKey(key, {
  85. title: 'parent2-new',
  86. });
  87. }
  88. return {
  89. treeData,
  90. treeRef,
  91. handleLevel,
  92. handleSetCheckData,
  93. handleGetCheckData,
  94. handleSetSelectData,
  95. handleGetSelectData,
  96. handleSetExpandData,
  97. handleGetExpandData,
  98. appendNodeByKey,
  99. deleteNodeByKey,
  100. updateNodeByKey,
  101. };
  102. },
  103. });
  104. </script>