iview/src/components/cascader/caspanel.vue

125 lines
4.3 KiB
Vue
Raw Normal View History

2016-11-15 19:17:54 +08:00
<template>
2017-03-06 17:30:39 +08:00
<span>
<ul v-if="data && data.length" :class="[prefixCls + '-menu']">
<Casitem
v-for="item in data"
2017-03-14 11:25:21 +08:00
:key="item"
2017-03-06 17:30:39 +08:00
:prefix-cls="prefixCls"
:data="item"
:tmp-item="tmpItem"
@click.native.stop="handleClickItem(item)"
@mouseenter.native.stop="handleHoverItem(item)"></Casitem>
</ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
</span>
2016-11-15 19:17:54 +08:00
</template>
<script>
import Casitem from './casitem.vue';
2017-03-06 17:30:39 +08:00
import Emitter from '../../mixins/emitter';
2016-11-15 19:17:54 +08:00
export default {
name: 'Caspanel',
2017-03-06 17:30:39 +08:00
mixins: [ Emitter ],
2016-11-15 19:17:54 +08:00
components: { Casitem },
props: {
data: {
type: Array,
default () {
2016-12-25 22:49:42 +08:00
return [];
2016-11-15 19:17:54 +08:00
}
},
disabled: Boolean,
changeOnSelect: Boolean,
2016-11-15 23:23:16 +08:00
trigger: String,
2016-11-15 19:17:54 +08:00
prefixCls: String
},
data () {
return {
tmpItem: {},
2017-03-06 17:30:39 +08:00
result: [],
sublist: []
2016-12-25 22:49:42 +08:00
};
2016-11-15 19:17:54 +08:00
},
2017-03-06 17:30:39 +08:00
watch: {
data () {
this.sublist = [];
}
},
2016-11-15 19:17:54 +08:00
methods: {
handleClickItem (item) {
2016-11-15 23:23:16 +08:00
if (this.trigger !== 'click' && item.children) return;
2016-11-15 19:17:54 +08:00
this.handleTriggerItem(item);
},
handleHoverItem (item) {
2016-11-15 23:23:16 +08:00
if (this.trigger !== 'hover' || !item.children) return;
2016-11-15 19:17:54 +08:00
this.handleTriggerItem(item);
},
2016-11-15 23:23:16 +08:00
handleTriggerItem (item, fromInit = false) {
2016-11-15 19:17:54 +08:00
if (item.disabled) return;
2017-04-22 14:00:10 +08:00
// return value back recursion // 向上递归,设置临时选中值(并非真实选中)
2016-11-15 23:23:16 +08:00
const backItem = this.getBaseItem(item);
this.tmpItem = backItem;
this.emitUpdate([backItem]);
2016-11-15 19:17:54 +08:00
if (item.children && item.children.length){
this.sublist = item.children;
2017-03-06 17:30:39 +08:00
this.dispatch('Cascader', 'on-result-change', {
lastValue: false,
changeOnSelect: this.changeOnSelect,
fromInit: fromInit
});
2016-11-15 19:17:54 +08:00
} else {
this.sublist = [];
2017-03-06 17:30:39 +08:00
this.dispatch('Cascader', 'on-result-change', {
lastValue: true,
changeOnSelect: this.changeOnSelect,
fromInit: fromInit
});
2016-11-15 19:17:54 +08:00
}
},
updateResult (item) {
this.result = [this.tmpItem].concat(item);
2016-11-15 23:23:16 +08:00
this.emitUpdate(this.result);
2016-11-15 19:17:54 +08:00
},
getBaseItem (item) {
let backItem = Object.assign({}, item);
if (backItem.children) {
delete backItem.children;
}
return backItem;
2016-11-15 23:23:16 +08:00
},
emitUpdate (result) {
if (this.$parent.$options.name === 'Caspanel') {
this.$parent.updateResult(result);
} else {
this.$parent.$parent.updateResult(result);
}
2016-11-15 19:17:54 +08:00
}
},
2017-03-06 17:30:39 +08:00
mounted () {
this.$on('on-find-selected', (params) => {
const val = params.value;
2016-11-15 23:23:16 +08:00
let value = [...val];
for (let i = 0; i < value.length; i++) {
for (let j = 0; j < this.data.length; j++) {
if (value[i] === this.data[j].value) {
this.handleTriggerItem(this.data[j], true);
value.splice(0, 1);
this.$nextTick(() => {
2017-03-06 17:30:39 +08:00
this.broadcast('Caspanel', 'on-find-selected', {
value: value
});
2016-11-15 23:23:16 +08:00
});
return false;
}
}
}
2017-03-06 17:30:39 +08:00
});
this.$on('on-clear', () => {
2016-11-16 10:07:03 +08:00
this.sublist = [];
this.tmpItem = {};
2017-03-06 17:30:39 +08:00
});
2016-11-15 19:17:54 +08:00
}
2016-12-25 22:49:42 +08:00
};
</script>