Commit b31aab71d2ed03bd1db42418f6fec373334ec736

Authored by 梁灏
1 parent 76a47814

Tree add async loading

examples/routers/tree.vue
1 1 <template>
2 2 <div>
3   - <Tree :data="baseData" show-checkbox multiple></Tree>
  3 + <Tree :data="baseData" :load-data="loadData" show-checkbox multiple></Tree>
4 4 <Button @click="handleAdd">add</Button>
5 5 <Button @click="handleUpdate">update</Button>
6 6 </div>
... ... @@ -16,18 +16,9 @@
16 16 children: [
17 17 {
18 18 title: 'parent 1-0',
19   - expand: true,
20   - disabled: true,
21   - children: [
22   - {
23   - title: 'leaf',
24   - disableCheckbox: true
25   - },
26   - {
27   - title: 'leaf',
28   - checked: false
29   - }
30   - ]
  19 + expand: false,
  20 + children: [],
  21 + loading: false
31 22 },
32 23 {
33 24 title: 'parent 1-1',
... ... @@ -71,6 +62,25 @@
71 62 },
72 63 cc () {
73 64 console.log(99)
  65 + },
  66 + loadData (item, callback) {
  67 + item.loading = true;
  68 + setTimeout(() => {
  69 + item.children = [
  70 + {
  71 + title: 'children-1',
  72 + loading: false,
  73 + children: []
  74 + },
  75 + {
  76 + title: 'children-2',
  77 + loading: false,
  78 + children: []
  79 + }
  80 + ];
  81 + item.loading = false;
  82 + callback();
  83 + }, 2000);
74 84 }
75 85 }
76 86 }
... ...
src/components/tree/node.vue
... ... @@ -3,7 +3,8 @@
3 3 <ul :class="classes">
4 4 <li>
5 5 <span :class="arrowClasses" @click="handleExpand">
6   - <Icon type="arrow-right-b"></Icon>
  6 + <Icon v-if="showArrow" type="arrow-right-b"></Icon>
  7 + <Icon v-if="showLoading" type="load-c" class="ivu-load-loop"></Icon>
7 8 </span>
8 9 <Checkbox
9 10 v-if="showCheckbox"
... ... @@ -31,6 +32,7 @@
31 32 import Render from '../base/render';
32 33 import CollapseTransition from '../base/collapse-transition';
33 34 import Emitter from '../../mixins/emitter';
  35 + import { findComponentUpward } from '../../utils/assist';
34 36  
35 37 const prefixCls = 'ivu-tree';
36 38  
... ... @@ -77,8 +79,7 @@
77 79 `${prefixCls}-arrow`,
78 80 {
79 81 [`${prefixCls}-arrow-disabled`]: this.data.disabled,
80   - [`${prefixCls}-arrow-open`]: this.data.expand,
81   - [`${prefixCls}-arrow-hidden`]: !(this.data.children && this.data.children.length)
  82 + [`${prefixCls}-arrow-open`]: this.data.expand
82 83 }
83 84 ];
84 85 },
... ... @@ -89,13 +90,36 @@
89 90 [`${prefixCls}-title-selected`]: this.data.selected
90 91 }
91 92 ];
  93 + },
  94 + showArrow () {
  95 + return (this.data.children && this.data.children.length) || ('loading' in this.data && !this.data.loading);
  96 + },
  97 + showLoading () {
  98 + return 'loading' in this.data && this.data.loading;
92 99 }
93 100 },
94 101 methods: {
95 102 handleExpand () {
96   - if (this.data.disabled) return;
97   - this.$set(this.data, 'expand', !this.data.expand);
98   - this.dispatch('Tree', 'toggle-expand', this.data);
  103 + const item = this.data;
  104 + if (item.disabled) return;
  105 +
  106 + // async loading
  107 + if (item.loading !== undefined && !item.children.length) {
  108 + const tree = findComponentUpward(this, 'Tree');
  109 + if (tree && tree.loadData) {
  110 + tree.loadData(item, () => {
  111 + if (item.children.length) {
  112 + this.handleExpand(item);
  113 + }
  114 + });
  115 + return;
  116 + }
  117 + }
  118 +
  119 + if (item.children && item.children.length) {
  120 + this.$set(this.data, 'expand', !this.data.expand);
  121 + this.dispatch('Tree', 'toggle-expand', this.data);
  122 + }
99 123 },
100 124 handleSelect () {
101 125 if (this.data.disabled) return;
... ...
src/components/tree/tree.vue
... ... @@ -39,6 +39,9 @@
39 39 },
40 40 emptyText: {
41 41 type: String
  42 + },
  43 + loadData: {
  44 + type: Function
42 45 }
43 46 },
44 47 data () {
... ...
src/styles/components/tree.less
... ... @@ -49,12 +49,12 @@
49 49 transform: rotate(90deg);
50 50 }
51 51 }
52   - &-hidden{
53   - cursor: auto;
54   - i{
55   - display: none;
56   - }
57   - }
  52 + //&-hidden{
  53 + // cursor: auto;
  54 + // i{
  55 + // display: none;
  56 + // }
  57 + //}
58 58 &-disabled{
59 59 cursor: @cursor-disabled;
60 60 }
... ...