Cascader support loadData async

This commit is contained in:
梁灏 2017-05-09 15:46:08 +08:00
parent 13a940ee86
commit f7ffdac569
4 changed files with 71 additions and 15 deletions

View file

@ -1,24 +1,24 @@
<template> <template>
<div style="width: 300px;margin: 100px;"> <Row>
<Cascader :data="data2" v-model="v1" change-on-select></Cascader> <i-col span="4">
</div> <Button @click="handleLoad">load</Button>
</i-col>
<i-col span="6">
<!--<Cascader :data="data2" v-model="v1" change-on-select></Cascader>-->
<Cascader :data="data2" v-model="v1" :loadData="loadData"></Cascader>
</i-col>
</Row>
</template> </template>
<script> <script>
export default { export default {
data () { data () {
return { return {
v1: ['zhejiang'], v1: [],
data2: [{ data2: [{
value: 'zhejiang', value: 'zhejiang',
label: '浙江', label: '浙江',
children: [{ children: [],
value: 'hangzhou', loading: false
label: '杭州',
children: [{
value: 'xihu',
label: '西湖'
}]
}]
}, { }, {
value: 'jiangsu', value: 'jiangsu',
label: '江苏', label: '江苏',
@ -32,6 +32,35 @@
}] }]
}] }]
} }
},
methods: {
handleLoad () {
this.data2[0].loading = !this.data2[0].loading;
},
loadData (item, cb) {
item.loading = true;
setTimeout(() => {
if (item.value === 'zhejiang') {
item.children = [
{
value: 'hangzhou',
label: '杭州',
loading: false,
children: []
}
];
} else if (item.value === 'hangzhou') {
item.children = [
{
value: 'ali',
label: '阿里巴巴'
}
];
}
item.loading = false;
cb();
}, 1000);
}
} }
} }
</script> </script>

View file

@ -88,6 +88,9 @@
default (label) { default (label) {
return label.join(' / '); return label.join(' / ');
} }
},
loadData: {
type: Function
} }
}, },
data () { data () {
@ -224,9 +227,12 @@
} }
this.updateSelected(true); this.updateSelected(true);
}, },
data () { data: {
deep: true,
handler () {
this.$nextTick(() => this.updateSelected()); this.$nextTick(() => this.updateSelected());
} }
} }
}
}; };
</script> </script>

View file

@ -1,5 +1,9 @@
<template> <template>
<li :class="classes">{{ data.label }}<i v-if="data.children && data.children.length" class="ivu-icon ivu-icon-ios-arrow-right"></i></li> <li :class="classes">
{{ data.label }}
<i v-if="showArrow" class="ivu-icon ivu-icon-ios-arrow-right"></i>
<i v-if="showLoading" class="ivu-icon ivu-icon-load-c ivu-load-loop"></i>
</li>
</template> </template>
<script> <script>
export default { export default {
@ -18,6 +22,12 @@
[`${this.prefixCls}-menu-item-disabled`]: this.data.disabled [`${this.prefixCls}-menu-item-disabled`]: this.data.disabled
} }
]; ];
},
showArrow () {
return (this.data.children && this.data.children.length) || ('loading' in this.data && !this.data.loading);
},
showLoading () {
return 'loading' in this.data && this.data.loading;
} }
} }
}; };

View file

@ -15,6 +15,7 @@
<script> <script>
import Casitem from './casitem.vue'; import Casitem from './casitem.vue';
import Emitter from '../../mixins/emitter'; import Emitter from '../../mixins/emitter';
import { findComponentUpward } from '../../utils/assist';
export default { export default {
name: 'Caspanel', name: 'Caspanel',
@ -56,6 +57,16 @@
handleTriggerItem (item, fromInit = false) { handleTriggerItem (item, fromInit = false) {
if (item.disabled) return; if (item.disabled) return;
if (item.loading !== undefined && !item.children.length) {
const cascader = findComponentUpward(this, 'Cascader');
if (cascader && cascader.loadData) {
cascader.loadData(item, () => {
this.handleTriggerItem(item);
});
return;
}
}
// return value back recursion // // return value back recursion //
const backItem = this.getBaseItem(item); const backItem = this.getBaseItem(item);
this.tmpItem = backItem; this.tmpItem = backItem;