ant-design-vue/components/tree-select/demo/async.vue
Cherry7 9be58078d2
Refactor(demo): change options to composition api (#6499)
* feat(demo): A-B

* feat(demo): update B-checkbox

* feat(demo): update CheckBox -DatePicker

* feat(demo): update DatePicker - Form

* feat(demo): update Form - List

* feat(demo): update  List-pagination

* feat(demo): update  List - skeleton

* feat(demo): update  skeleton - switch

* feat(demo): update  skeleton - switch

* feat(demo): update   switch - upload

* feat(demo): update  watermark

* fix(demo): del hashId
2023-04-28 14:08:21 +08:00

69 lines
1.5 KiB
Vue

<docs>
---
order: 5
title:
zh-CN: 异步加载
en-US: Asynchronous loading
---
## zh-CN
异步加载树节点
## en-US
Asynchronous loading tree node.
</docs>
<template>
<a-tree-select
v-model:value="value"
tree-data-simple-mode
style="width: 100%"
:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
:tree-data="treeData"
placeholder="Please select"
:load-data="onLoadData"
/>
</template>
<script lang="ts" setup>
import type { TreeSelectProps } from 'ant-design-vue';
import { ref, watch } from 'vue';
const value = ref<string>();
const treeData = ref<TreeSelectProps['treeData']>([
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
]);
watch(value, () => {
console.log(value.value);
});
const genTreeNode = (parentId: number, isLeaf = false): TreeSelectProps['treeData'][number] => {
const random = Math.random().toString(36).substring(2, 6);
return {
id: random,
pId: parentId,
value: random,
title: isLeaf ? 'Tree Node' : 'Expand to load',
isLeaf,
};
};
const onLoadData = (treeNode: TreeSelectProps['treeData'][number]) => {
return new Promise(resolve => {
const { id } = treeNode.dataRef;
setTimeout(() => {
treeData.value = treeData.value.concat([
genTreeNode(id, false),
genTreeNode(id, true),
genTreeNode(id, true),
]);
console.log(treeData.value);
resolve(true);
}, 300);
});
};
</script>