Add new props 'childrenKey' to Tree component

This commit is contained in:
vanppo 2018-03-26 20:09:18 +08:00
parent a0f489478b
commit 56c7cc0efc
2 changed files with 20 additions and 7 deletions

View file

@ -17,11 +17,12 @@
<span v-else :class="titleClasses" @click="handleSelect">{{ data.title }}</span>
<Tree-node
v-if="data.expand"
v-for="(item, i) in data.children"
v-for="(item, i) in children"
:key="i"
:data="item"
:multiple="multiple"
:show-checkbox="showCheckbox">
:show-checkbox="showCheckbox"
:children-key="childrenKey">
</Tree-node>
</li>
</ul>
@ -52,6 +53,10 @@
type: Boolean,
default: false
},
childrenKey: {
type: String,
default: 'children'
},
showCheckbox: {
type: Boolean,
default: false
@ -93,7 +98,7 @@
];
},
showArrow () {
return (this.data.children && this.data.children.length) || ('loading' in this.data && !this.data.loading);
return (this.data[this.childrenKey] && this.data[this.childrenKey].length) || ('loading' in this.data && !this.data.loading);
},
showLoading () {
return 'loading' in this.data && this.data.loading;
@ -118,6 +123,9 @@
} else {
return [];
}
},
children () {
return this.data[this.childrenKey];
}
},
methods: {
@ -126,14 +134,14 @@
if (item.disabled) return;
// async loading
if (item.children.length === 0) {
if (item[this.childrenKey].length === 0) {
const tree = findComponentUpward(this, 'Tree');
if (tree && tree.loadData) {
this.$set(this.data, 'loading', true);
tree.loadData(item, children => {
this.$set(this.data, 'loading', false);
if (children.length) {
this.$set(this.data, 'children', children);
this.$set(this.data, this.childrenKey, children);
this.$nextTick(() => this.handleExpand());
}
});
@ -141,7 +149,7 @@
}
}
if (item.children && item.children.length) {
if (item[this.childrenKey] && item[this.childrenKey].length) {
this.$set(this.data, 'expand', !this.data.expand);
this.dispatch('Tree', 'toggle-expand', this.data);
}

View file

@ -6,7 +6,8 @@
:data="item"
visible
:multiple="multiple"
:show-checkbox="showCheckbox">
:show-checkbox="showCheckbox"
:children-key="childrenKey">
</Tree-node>
<div :class="[prefixCls + '-empty']" v-if="!stateTree.length">{{ localeEmptyText }}</div>
</div>
@ -40,6 +41,10 @@
emptyText: {
type: String
},
childrenKey: {
type: String,
default: 'children'
},
loadData: {
type: Function
},