index.vue 4.0 KB

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