index.vue 3.7 KB

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