提交 54212112 作者: 无木

feat(demo): add basicTree with async data expand all

演示basicTree使用异步数据并自动展开
上级 cf840e3e
<template> <template>
<PageWrapper title="Tree基础示例"> <PageWrapper title="Tree基础示例">
<div class="flex"> <Row :gutter="[16, 16]">
<BasicTree <Col :span="8">
:treeData="treeData" <BasicTree title="基础示例,默认展开第一层" :treeData="treeData" defaultExpandLevel="1" />
title="基础示例,默认展开第一层" </Col>
defaultExpandLevel="1" <Col :span="8">
class="w-1/3" <BasicTree
/> title="可勾选,默认全部展开"
:treeData="treeData"
<BasicTree :checkable="true"
:treeData="treeData" defaultExpandAll
title="可勾选,默认全部展开" @check="handleCheck"
:checkable="true" />
class="w-1/3 mx-4" </Col>
defaultExpandAll <Col :span="8">
@check="handleCheck" <BasicTree
/> title="指定默认展开/勾选示例"
:treeData="treeData"
<BasicTree :checkable="true"
title="指定默认展开/勾选示例" :expandedKeys="['0-0']"
:treeData="treeData" :checkedKeys="['0-0']"
:checkable="true" />
:expandedKeys="['0-0']" </Col>
:checkedKeys="['0-0']" <Col :span="8">
class="w-1/3" <BasicTree
/> title="懒加载异步树"
</div> ref="asyncTreeRef"
<div class="flex"> :treeData="tree"
<BasicTree :load-data="onLoadData"
title="异步树" />
ref="asyncTreeRef" </Col>
:treeData="tree" <Col :span="16">
class="w-1/3" <Card title="异步数据,默认展开">
:load-data="onLoadData" <template #extra>
/> <a-button @click="loadTreeData" :loading="treeLoading">加载数据</a-button>
</div> </template>
<Spin :spinning="treeLoading">
<BasicTree ref="asyncExpandTreeRef" :treeData="tree2" />
</Spin>
</Card>
</Col>
</Row>
</PageWrapper> </PageWrapper>
</template> </template>
<script lang="ts"> <script lang="ts">
import { defineComponent, ref, unref } from 'vue'; import { defineComponent, nextTick, ref, unref } from 'vue';
import { BasicTree, TreeActionType } from '/@/components/Tree/index'; import { BasicTree, TreeActionType, TreeItem } from '/@/components/Tree/index';
import { treeData } from './data'; import { treeData } from './data';
import { PageWrapper } from '/@/components/Page'; import { PageWrapper } from '/@/components/Page';
import { Card, Row, Col, Spin } from 'ant-design-vue';
import { cloneDeep } from 'lodash-es';
export default defineComponent({ export default defineComponent({
components: { BasicTree, PageWrapper }, components: { BasicTree, PageWrapper, Card, Row, Col, Spin },
setup() { setup() {
const asyncTreeRef = ref<Nullable<TreeActionType>>(null); const asyncTreeRef = ref<Nullable<TreeActionType>>(null);
const asyncExpandTreeRef = ref<Nullable<TreeActionType>>(null);
const tree2 = ref<TreeItem[]>([]);
const treeLoading = ref(false);
function handleCheck(checkedKeys, e) { function handleCheck(checkedKeys, e) {
console.log('onChecked', checkedKeys, e); console.log('onChecked', checkedKeys, e);
} }
function loadTreeData() {
treeLoading.value = true;
// 以下是模拟异步获取数据
setTimeout(() => {
// 设置数据源
tree2.value = cloneDeep(treeData);
treeLoading.value = false;
// 展开全部
nextTick(() => {
console.log(unref(asyncExpandTreeRef));
unref(asyncExpandTreeRef)?.expandAll(true);
});
}, 2000);
}
const tree = ref([ const tree = ref([
{ {
title: 'parent ', title: 'parent ',
...@@ -82,7 +110,17 @@ ...@@ -82,7 +110,17 @@
}, 1000); }, 1000);
}); });
} }
return { treeData, handleCheck, tree, onLoadData, asyncTreeRef }; return {
treeData,
handleCheck,
tree,
onLoadData,
asyncTreeRef,
asyncExpandTreeRef,
tree2,
loadTreeData,
treeLoading,
};
}, },
}); });
</script> </script>
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论