c463ab87
梁灏
add Cascader
|
1
2
3
4
5
6
7
8
9
|
<template>
<ul v-if="data && data.length" :class="[prefixCls + '-menu']">
<Casitem
v-for="item in data"
:prefix-cls="prefixCls"
:data.sync="item"
:tmp-item="tmpItem"
@click.stop="handleClickItem(item)"
@mouseenter.stop="handleHoverItem(item)"></Casitem>
|
bd4e3b9b
梁灏
update Cascader
|
10
|
</ul><Caspanel v-if="sublist && sublist.length" :prefix-cls="prefixCls" :data.sync="sublist" :disabled="disabled" :trigger="trigger" :change-on-select="changeOnSelect"></Caspanel>
|
c463ab87
梁灏
add Cascader
|
11
12
13
|
</template>
<script>
import Casitem from './casitem.vue';
|
c463ab87
梁灏
add Cascader
|
14
15
16
17
18
19
20
21
|
export default {
name: 'Caspanel',
components: { Casitem },
props: {
data: {
type: Array,
default () {
|
b0893113
jingsam
add eslint
|
22
|
return [];
|
c463ab87
梁灏
add Cascader
|
23
24
25
26
27
|
}
},
sublist: {
type: Array,
default () {
|
b0893113
jingsam
add eslint
|
28
|
return [];
|
c463ab87
梁灏
add Cascader
|
29
30
31
32
|
}
},
disabled: Boolean,
changeOnSelect: Boolean,
|
bd4e3b9b
梁灏
update Cascader
|
33
|
trigger: String,
|
c463ab87
梁灏
add Cascader
|
34
35
36
37
38
39
|
prefixCls: String
},
data () {
return {
tmpItem: {},
result: []
|
b0893113
jingsam
add eslint
|
40
|
};
|
c463ab87
梁灏
add Cascader
|
41
42
43
|
},
methods: {
handleClickItem (item) {
|
bd4e3b9b
梁灏
update Cascader
|
44
|
if (this.trigger !== 'click' && item.children) return;
|
c463ab87
梁灏
add Cascader
|
45
46
47
|
this.handleTriggerItem(item);
},
handleHoverItem (item) {
|
bd4e3b9b
梁灏
update Cascader
|
48
|
if (this.trigger !== 'hover' || !item.children) return;
|
c463ab87
梁灏
add Cascader
|
49
50
|
this.handleTriggerItem(item);
},
|
bd4e3b9b
梁灏
update Cascader
|
51
|
handleTriggerItem (item, fromInit = false) {
|
c463ab87
梁灏
add Cascader
|
52
53
|
if (item.disabled) return;
|
bd4e3b9b
梁灏
update Cascader
|
54
55
56
57
58
|
// return value back recursion
const backItem = this.getBaseItem(item);
this.tmpItem = backItem;
this.emitUpdate([backItem]);
|
c463ab87
梁灏
add Cascader
|
59
60
|
if (item.children && item.children.length){
this.sublist = item.children;
|
bd4e3b9b
梁灏
update Cascader
|
61
|
this.$dispatch('on-result-change', false, this.changeOnSelect, fromInit);
|
c463ab87
梁灏
add Cascader
|
62
63
|
} else {
this.sublist = [];
|
bd4e3b9b
梁灏
update Cascader
|
64
|
this.$dispatch('on-result-change', true, this.changeOnSelect, fromInit);
|
c463ab87
梁灏
add Cascader
|
65
|
}
|
c463ab87
梁灏
add Cascader
|
66
67
68
|
},
updateResult (item) {
this.result = [this.tmpItem].concat(item);
|
bd4e3b9b
梁灏
update Cascader
|
69
|
this.emitUpdate(this.result);
|
c463ab87
梁灏
add Cascader
|
70
71
72
73
74
75
76
77
|
},
getBaseItem (item) {
let backItem = Object.assign({}, item);
if (backItem.children) {
delete backItem.children;
}
return backItem;
|
bd4e3b9b
梁灏
update Cascader
|
78
79
80
81
82
83
84
|
},
emitUpdate (result) {
if (this.$parent.$options.name === 'Caspanel') {
this.$parent.updateResult(result);
} else {
this.$parent.$parent.updateResult(result);
}
|
c463ab87
梁灏
add Cascader
|
85
86
87
88
89
90
91
|
}
},
watch: {
data () {
this.sublist = [];
}
},
|
bd4e3b9b
梁灏
update Cascader
|
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
events: {
'on-find-selected' (val) {
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(() => {
this.$broadcast('on-find-selected', value);
});
return false;
}
}
}
|
165bb7c9
梁灏
update Cascader
|
107
108
109
110
|
},
'on-clear' () {
this.sublist = [];
this.tmpItem = {};
|
bd4e3b9b
梁灏
update Cascader
|
111
|
}
|
c463ab87
梁灏
add Cascader
|
112
|
}
|
b0893113
jingsam
add eslint
|
113
114
|
};
</script>
|