Blame view

src/views/demo/tree/ActionTree.vue 4.64 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>
16
      <a-button @click="handleGetSelectNode" class="mr-2"> 获取选中节点 </a-button>
陈文彬 authored
17
18
19
      <a-button @click="handleSetExpandData" class="mr-2"> 设置展开数据 </a-button>
      <a-button @click="handleGetExpandData" class="mr-2"> 获取展开数据 </a-button>
陈文彬 authored
20
21
    </div>
    <div class="mb-4">
22
23
24
25
      <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
26
    </div>
27
    <BasicTree :treeData="treeData" title="函数操作" ref="treeRef" :checkable="true" />
28
  </PageWrapper>
陈文彬 authored
29
30
31
32
33
34
</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';
35
  import { PageWrapper } from '/@/components/Page';
36
  import { type Nullable } from '@vben/types';
陈文彬 authored
37
38

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

      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));
      }
74
75
76
77
78
79
      function handleGetSelectNode() {
        const keys = getTree().getSelectedKeys();
        const node = getTree().getSelectedNode(keys[0]);
        createMessage.success(node !== null ? JSON.stringify(node) : null);
      }
陈文彬 authored
80
81
82
83
84
85
86
87
88
      function handleSetExpandData() {
        getTree().setExpandedKeys(['0-0']);
      }

      function handleGetExpandData() {
        const keys = getTree().getExpandedKeys();
        createMessage.success(JSON.stringify(keys));
      }
89
90
91
92
93
94
95
96
      function checkAll(checkAll: boolean) {
        getTree().checkAll(checkAll);
      }

      function expandAll(checkAll: boolean) {
        getTree().expandAll(checkAll);
      }
陈文彬 authored
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
126
127
128
129
130
      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,
131
        handleGetSelectNode,
陈文彬 authored
132
133
134
        appendNodeByKey,
        deleteNodeByKey,
        updateNodeByKey,
135
136
        checkAll,
        expandAll,
陈文彬 authored
137
138
139
140
      };
    },
  });
</script>