Commit 56c7cc0efc34361b99d1ca30698045e746547a81

Authored by vanppo
1 parent a0f48947

Add new props 'childrenKey' to Tree component

src/components/tree/node.vue
@@ -17,11 +17,12 @@ @@ -17,11 +17,12 @@
17 <span v-else :class="titleClasses" @click="handleSelect">{{ data.title }}</span> 17 <span v-else :class="titleClasses" @click="handleSelect">{{ data.title }}</span>
18 <Tree-node 18 <Tree-node
19 v-if="data.expand" 19 v-if="data.expand"
20 - v-for="(item, i) in data.children" 20 + v-for="(item, i) in children"
21 :key="i" 21 :key="i"
22 :data="item" 22 :data="item"
23 :multiple="multiple" 23 :multiple="multiple"
24 - :show-checkbox="showCheckbox"> 24 + :show-checkbox="showCheckbox"
  25 + :children-key="childrenKey">
25 </Tree-node> 26 </Tree-node>
26 </li> 27 </li>
27 </ul> 28 </ul>
@@ -52,6 +53,10 @@ @@ -52,6 +53,10 @@
52 type: Boolean, 53 type: Boolean,
53 default: false 54 default: false
54 }, 55 },
  56 + childrenKey: {
  57 + type: String,
  58 + default: 'children'
  59 + },
55 showCheckbox: { 60 showCheckbox: {
56 type: Boolean, 61 type: Boolean,
57 default: false 62 default: false
@@ -93,7 +98,7 @@ @@ -93,7 +98,7 @@
93 ]; 98 ];
94 }, 99 },
95 showArrow () { 100 showArrow () {
96 - return (this.data.children && this.data.children.length) || ('loading' in this.data && !this.data.loading); 101 + return (this.data[this.childrenKey] && this.data[this.childrenKey].length) || ('loading' in this.data && !this.data.loading);
97 }, 102 },
98 showLoading () { 103 showLoading () {
99 return 'loading' in this.data && this.data.loading; 104 return 'loading' in this.data && this.data.loading;
@@ -118,6 +123,9 @@ @@ -118,6 +123,9 @@
118 } else { 123 } else {
119 return []; 124 return [];
120 } 125 }
  126 + },
  127 + children () {
  128 + return this.data[this.childrenKey];
121 } 129 }
122 }, 130 },
123 methods: { 131 methods: {
@@ -126,14 +134,14 @@ @@ -126,14 +134,14 @@
126 if (item.disabled) return; 134 if (item.disabled) return;
127 135
128 // async loading 136 // async loading
129 - if (item.children.length === 0) { 137 + if (item[this.childrenKey].length === 0) {
130 const tree = findComponentUpward(this, 'Tree'); 138 const tree = findComponentUpward(this, 'Tree');
131 if (tree && tree.loadData) { 139 if (tree && tree.loadData) {
132 this.$set(this.data, 'loading', true); 140 this.$set(this.data, 'loading', true);
133 tree.loadData(item, children => { 141 tree.loadData(item, children => {
134 this.$set(this.data, 'loading', false); 142 this.$set(this.data, 'loading', false);
135 if (children.length) { 143 if (children.length) {
136 - this.$set(this.data, 'children', children); 144 + this.$set(this.data, this.childrenKey, children);
137 this.$nextTick(() => this.handleExpand()); 145 this.$nextTick(() => this.handleExpand());
138 } 146 }
139 }); 147 });
@@ -141,7 +149,7 @@ @@ -141,7 +149,7 @@
141 } 149 }
142 } 150 }
143 151
144 - if (item.children && item.children.length) { 152 + if (item[this.childrenKey] && item[this.childrenKey].length) {
145 this.$set(this.data, 'expand', !this.data.expand); 153 this.$set(this.data, 'expand', !this.data.expand);
146 this.dispatch('Tree', 'toggle-expand', this.data); 154 this.dispatch('Tree', 'toggle-expand', this.data);
147 } 155 }
src/components/tree/tree.vue
@@ -6,7 +6,8 @@ @@ -6,7 +6,8 @@
6 :data="item" 6 :data="item"
7 visible 7 visible
8 :multiple="multiple" 8 :multiple="multiple"
9 - :show-checkbox="showCheckbox"> 9 + :show-checkbox="showCheckbox"
  10 + :children-key="childrenKey">
10 </Tree-node> 11 </Tree-node>
11 <div :class="[prefixCls + '-empty']" v-if="!stateTree.length">{{ localeEmptyText }}</div> 12 <div :class="[prefixCls + '-empty']" v-if="!stateTree.length">{{ localeEmptyText }}</div>
12 </div> 13 </div>
@@ -40,6 +41,10 @@ @@ -40,6 +41,10 @@
40 emptyText: { 41 emptyText: {
41 type: String 42 type: String
42 }, 43 },
  44 + childrenKey: {
  45 + type: String,
  46 + default: 'children'
  47 + },
43 loadData: { 48 loadData: {
44 type: Function 49 type: Function
45 }, 50 },