Blame view

src/views/demo/tree/ActionTree.vue 4.25 KB
陈文彬 authored
1
<template>
2
  <PageWrapper title="Tree函数操作示例" contentBackground contentClass="p-4">
陈文彬 authored
3
    <div class="mb-4">
4
5
6
7
      <a-button @click="expandAll(true)" class="mr-2"> 展开全部 </a-button>
      <a-button @click="expandAll(false)" class="mr-2"> 折叠全部 </a-button>
      <a-button @click="checkAll(true)" class="mr-2"> 全选 </a-button>
      <a-button @click="checkAll(false)" class="mr-2"> 全不选 </a-button>
8
9
      <a-button @click="handleLevel(2)" class="mr-2"> 显示到第2级 </a-button>
      <a-button @click="handleLevel(1)" class="mr-2"> 显示到第1级 </a-button>
10
11
    </div>
    <div class="mb-4">
12
13
14
15
      <a-button @click="handleSetCheckData" class="mr-2"> 设置勾选数据 </a-button>
      <a-button @click="handleGetCheckData" class="mr-2"> 获取勾选数据 </a-button>
      <a-button @click="handleSetSelectData" class="mr-2"> 设置选中数据 </a-button>
      <a-button @click="handleGetSelectData" class="mr-2"> 获取选中数据 </a-button>
陈文彬 authored
16
17
18
      <a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
      <a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
陈文彬 authored
19
20
    </div>
    <div class="mb-4">
21
22
23
24
      <a-button @click="appendNodeByKey(null)" class="mr-2"> 添加根节点 </a-button>
      <a-button @click="appendNodeByKey('2-2')" class="mr-2"> 添加在parent3内添加节点 </a-button>
      <a-button @click="deleteNodeByKey('2-2')" class="mr-2"> 删除parent3节点 </a-button>
      <a-button @click="updateNodeByKey('1-1')" class="mr-2"> 更新parent2节点 </a-button>
陈文彬 authored
25
    </div>
26
    <BasicTree :treeData="treeData" title="函数操作" ref="treeRef" :checkable="true" />
27
  </PageWrapper>
陈文彬 authored
28
29
30
31
32
33
</template>
<script lang="ts">
  import { defineComponent, ref, unref } from 'vue';
  import { BasicTree, TreeActionType } from '/@/components/Tree/index';
  import { treeData } from './data';
  import { useMessage } from '/@/hooks/web/useMessage';
34
  import { PageWrapper } from '/@/components/Page';
陈文彬 authored
35
36

  export default defineComponent({
37
    components: { BasicTree, PageWrapper },
陈文彬 authored
38
    setup() {
vben authored
39
      const treeRef = ref<Nullable<TreeActionType>>(null);
陈文彬 authored
40
      const { createMessage } = useMessage();
41
陈文彬 authored
42
43
44
45
46
      function getTree() {
        const tree = unref(treeRef);
        if (!tree) {
          throw new Error('tree is null!');
        }
vben authored
47
        return tree;
陈文彬 authored
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
      }

      function handleLevel(level: number) {
        getTree().filterByLevel(level);
      }

      function handleSetCheckData() {
        getTree().setCheckedKeys(['0-0']);
      }

      function handleGetCheckData() {
        const keys = getTree().getCheckedKeys();
        createMessage.success(JSON.stringify(keys));
      }

      function handleSetSelectData() {
        getTree().setSelectedKeys(['0-0']);
      }

      function handleGetSelectData() {
        const keys = getTree().getSelectedKeys();
        createMessage.success(JSON.stringify(keys));
      }

      function handleSetExpandData() {
        getTree().setExpandedKeys(['0-0']);
      }

      function handleGetExpandData() {
        const keys = getTree().getExpandedKeys();
        createMessage.success(JSON.stringify(keys));
      }
81
82
83
84
85
86
87
88
      function checkAll(checkAll: boolean) {
        getTree().checkAll(checkAll);
      }

      function expandAll(checkAll: boolean) {
        getTree().expandAll(checkAll);
      }
陈文彬 authored
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
      function appendNodeByKey(parentKey: string | null = null) {
        getTree().insertNodeByKey({
          parentKey: parentKey,
          node: {
            title: '新增节点',
            key: '2-2-2',
          },
          // 往后插入
          push: 'push',
          // 往前插入
          // push:'unshift'
        });
      }

      function deleteNodeByKey(key: string) {
        getTree().deleteNodeByKey(key);
      }

      function updateNodeByKey(key: string) {
        getTree().updateNodeByKey(key, {
          title: 'parent2-new',
        });
      }

      return {
        treeData,
        treeRef,
        handleLevel,
        handleSetCheckData,
        handleGetCheckData,
        handleSetSelectData,
        handleGetSelectData,
        handleSetExpandData,
        handleGetExpandData,
        appendNodeByKey,
        deleteNodeByKey,
        updateNodeByKey,
126
127
        checkAll,
        expandAll,
陈文彬 authored
128
129
130
131
      };
    },
  });
</script>