index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <PageWrapper title="Tree基础示例">
  3. <Row :gutter="[16, 16]">
  4. <Col :span="8">
  5. <BasicTree title="基础示例,默认展开第一层" :treeData="treeData" defaultExpandLevel="1">
  6. <template #icon><SmileTwoTone /></template>
  7. <template #title>666</template>
  8. <template #switcherIcon> <CarryOutOutlined /></template>
  9. </BasicTree>
  10. </Col>
  11. <Col :span="8">
  12. <BasicTree
  13. title="可勾选,默认全部展开"
  14. :treeData="treeData"
  15. :checkable="true"
  16. defaultExpandAll
  17. @check="handleCheck"
  18. />
  19. </Col>
  20. <Col :span="8">
  21. <BasicTree
  22. title="指定默认展开/勾选示例"
  23. :treeData="treeData"
  24. :checkable="true"
  25. :expandedKeys="['0-0']"
  26. :checkedKeys="['0-0']"
  27. />
  28. </Col>
  29. <Col :span="8">
  30. <BasicTree
  31. title="懒加载异步树"
  32. ref="asyncTreeRef"
  33. :treeData="tree"
  34. :load-data="onLoadData"
  35. />
  36. </Col>
  37. <Col :span="8">
  38. <Card title="异步数据,默认展开">
  39. <template #extra>
  40. <a-button @click="loadTreeData" :loading="treeLoading">加载数据</a-button>
  41. </template>
  42. <Spin :spinning="treeLoading">
  43. <BasicTree ref="asyncExpandTreeRef" :treeData="tree2" />
  44. </Spin>
  45. </Card>
  46. </Col>
  47. <Col :span="8">
  48. <Card title="BasicTree内置加载">
  49. <template #extra>
  50. <a-button @click="loadTreeData2" :loading="treeLoading">请求数据</a-button>
  51. </template>
  52. <BasicTree ref="loadTreeRef" :treeData="tree2" :loading="treeLoading" />
  53. </Card>
  54. </Col>
  55. </Row>
  56. </PageWrapper>
  57. </template>
  58. <script lang="ts" setup>
  59. import { nextTick, ref, unref } from 'vue';
  60. import { BasicTree, TreeActionType, TreeItem } from '@/components/Tree';
  61. import { treeData } from './data';
  62. import { PageWrapper } from '@/components/Page';
  63. import { Card, Row, Col, Spin } from 'ant-design-vue';
  64. import { cloneDeep, uniq } from 'lodash-es';
  65. import { isArray } from '@/utils/is';
  66. import { type Nullable } from '@vben/types';
  67. import { SmileTwoTone, CarryOutOutlined } from '@ant-design/icons-vue';
  68. const asyncTreeRef = ref<Nullable<TreeActionType>>(null);
  69. const asyncExpandTreeRef = ref<Nullable<TreeActionType>>(null);
  70. const loadTreeRef = ref<Nullable<TreeActionType>>(null);
  71. const tree2 = ref<TreeItem[]>([]);
  72. const treeLoading = ref(false);
  73. function handleCheck(checkedKeys, e) {
  74. console.log('onChecked', checkedKeys, e);
  75. }
  76. function loadTreeData() {
  77. treeLoading.value = true;
  78. // 以下是模拟异步获取数据
  79. setTimeout(() => {
  80. // 设置数据源
  81. tree2.value = cloneDeep(treeData);
  82. treeLoading.value = false;
  83. // 展开全部
  84. nextTick(() => {
  85. console.log(unref(asyncExpandTreeRef));
  86. unref(asyncExpandTreeRef)?.expandAll(true);
  87. });
  88. }, 2000);
  89. }
  90. function loadTreeData2() {
  91. treeLoading.value = true;
  92. // 以下是模拟异步获取数据
  93. setTimeout(() => {
  94. // 设置数据源
  95. tree2.value = cloneDeep(treeData);
  96. treeLoading.value = false;
  97. }, 2000);
  98. }
  99. const tree = ref([
  100. {
  101. title: 'parent ',
  102. key: '0-0',
  103. },
  104. ]);
  105. function onLoadData(treeNode) {
  106. return new Promise((resolve: (value?: unknown) => void) => {
  107. if (isArray(treeNode.children) && treeNode.children.length > 0) {
  108. resolve();
  109. return;
  110. }
  111. setTimeout(() => {
  112. const asyncTreeAction: TreeActionType | null = unref(asyncTreeRef);
  113. if (asyncTreeAction) {
  114. const nodeChildren = [
  115. { title: `Child Node ${treeNode.eventKey}-0`, key: `${treeNode.eventKey}-0` },
  116. { title: `Child Node ${treeNode.eventKey}-1`, key: `${treeNode.eventKey}-1` },
  117. ];
  118. asyncTreeAction.updateNodeByKey(treeNode.eventKey, { children: nodeChildren });
  119. asyncTreeAction.setExpandedKeys(
  120. uniq([treeNode.eventKey, ...asyncTreeAction.getExpandedKeys()]),
  121. );
  122. }
  123. resolve();
  124. return;
  125. }, 300);
  126. });
  127. }
  128. </script>