index.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <PageWrapper title="Tree基础示例">
  3. <div class="flex">
  4. <BasicTree
  5. :treeData="treeData"
  6. title="基础示例,默认展开第一层"
  7. defaultExpandLevel="1"
  8. class="w-1/3"
  9. />
  10. <BasicTree
  11. :treeData="treeData"
  12. title="可勾选,默认全部展开"
  13. :checkable="true"
  14. class="w-1/3 mx-4"
  15. defaultExpandAll
  16. @check="handleCheck"
  17. />
  18. <BasicTree
  19. title="指定默认展开/勾选示例"
  20. :treeData="treeData"
  21. :checkable="true"
  22. :expandedKeys="['0-0']"
  23. :checkedKeys="['0-0']"
  24. class="w-1/3"
  25. />
  26. </div>
  27. <div class="flex">
  28. <BasicTree
  29. title="异步树"
  30. ref="asyncTreeRef"
  31. :treeData="tree"
  32. class="w-1/3"
  33. :load-data="onLoadData"
  34. />
  35. </div>
  36. </PageWrapper>
  37. </template>
  38. <script lang="ts">
  39. import { defineComponent, ref, unref } from 'vue';
  40. import { BasicTree, TreeActionType } from '/@/components/Tree/index';
  41. import { treeData } from './data';
  42. import { PageWrapper } from '/@/components/Page';
  43. export default defineComponent({
  44. components: { BasicTree, PageWrapper },
  45. setup() {
  46. const asyncTreeRef = ref<Nullable<TreeActionType>>(null);
  47. function handleCheck(checkedKeys, e) {
  48. console.log('onChecked', checkedKeys, e);
  49. }
  50. const tree = ref([
  51. {
  52. title: 'parent ',
  53. key: '0-0',
  54. },
  55. ]);
  56. function onLoadData(treeNode) {
  57. return new Promise((resolve: (value?: unknown) => void) => {
  58. if (!treeNode.children) {
  59. resolve();
  60. return;
  61. }
  62. setTimeout(() => {
  63. const asyncTreeAction: TreeActionType | null = unref(asyncTreeRef);
  64. if (asyncTreeAction) {
  65. const nodeChildren = [
  66. { title: `Child Node ${treeNode.eventKey}-0`, key: `${treeNode.eventKey}-0` },
  67. { title: `Child Node ${treeNode.eventKey}-1`, key: `${treeNode.eventKey}-1` },
  68. ];
  69. asyncTreeAction.updateNodeByKey(treeNode.eventKey, { children: nodeChildren });
  70. asyncTreeAction.setExpandedKeys([
  71. treeNode.eventKey,
  72. ...asyncTreeAction.getExpandedKeys(),
  73. ]);
  74. }
  75. resolve();
  76. return;
  77. }, 1000);
  78. });
  79. }
  80. return { treeData, handleCheck, tree, onLoadData, asyncTreeRef };
  81. },
  82. });
  83. </script>