-
Notifications
You must be signed in to change notification settings - Fork 1
/
cas-multi-select-panel.vue
171 lines (168 loc) · 5.81 KB
/
cas-multi-select-panel.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<template>
<div class="display-inline-block">
<ul class="ivu-cascader-menu">
<CheckboxGroup v-model="checkBoxGroup" @on-change="handleCheckBoxChange">
<li class="ivu-cascader-menu-item" v-for="(item, index) in data" :key="index" :class="{'ivu-cascader-menu-item-active': (!item.parentId && selected == index )|| selected == index}">
<!-- 判断是否第一项开启多选功能,或是否为’全选‘’字段 -->
<span v-if="((item.parentId && !oneToone) || allToone) && item.value !== 'all'">
<Checkbox @click.native.stop="handleCheckBoxClick" :label="index" class="w-full">{{item.label}}
<i class="ivu-icon ivu-icon-ios-arrow-right" v-if="item.children && item.children.length"></i>
</Checkbox>
</span>
<span v-else>
<p @click="handleClick(index,item)">
{{item.label}}
<i class="ivu-icon ivu-icon-ios-arrow-right" v-if="item.children && item.children.length"></i>
</p>
</span>
</li>
</CheckboxGroup>
</ul>
<casMultiPanel @handleGetSelected="selectedData" v-if="children.length && !destroy" :data="children.length && children" :childrencheckedData="childrenSelected" :componentId="componentId" :oneToone="oneToone" :allToone="allToone"></casMultiPanel>
</div>
</template>
<script>
export default {
name: "casMultiPanel",
props: {
data: {
type: Array,
default () {
return [];
}
},
// 子级选中数据
childrencheckedData: {
type: Array,
default () {
return [];
}
},
componentId: {
type: String,
default: ''
},
oneToone: {
type: Boolean,
default: false
},
allToone: {
type: Boolean,
default: false
}
},
data() {
return {
// 当前已选择项
checkBoxGroup: [],
// 子组件数据
children: [],
// 子组件选中数据
childrenSelected: [],
// 一级组件被选中高亮
selected: -1,
destroy: false
};
},
watch: {
// 重新渲染组件
destroy(val) {
if (val) {
this.$nextTick(_ => {
this.destroy = false;
});
}
},
checkBoxGroup(val, oldVal) {
if (val.length > oldVal.length) return
oldVal.map((vv, kk) => {
let type = false
val.map((v, k) => {
if (v == vv) type = true
})
if (!type) {
if (this.data && this.data.length && this.data[vv].children) {
this.data[vv].children.map((dataVal, dataKey) => {
dataVal.checked = false
})
}
}
})
}
},
mounted() {
// 当前组件数据选中状态赋值
let arr = []
this.data.map((val, key) => {
if (val.checked) arr.push(key)
})
this.handleCheckBoxChange(arr)
},
methods: {
// 获取选择项数据
selectedData(val = []) {
let arr = []
this.data.map((val, key) => {
if (val.checked) arr.push(val)
})
// 合并子组件传递的参数,并emit到父组件
this.$emit("handleGetSelected", arr.concat(val));
},
// 防止时间冒泡到父组件handleClose事件
handleCheckBoxClick() {},
// 处理数据排序问题,push带来的顺序跟原始数据可能会不一致问题异常
sortNumber(a, b) {
return a.parentId - b.parentId
},
// checkGroup变更事件,返回已选中的数组
handleCheckBoxChange(arr) {
this.checkBoxGroup = arr
// 清空记录
this.children = [];
// 遍历选择的数据
arr.map((k, v) => {
// 存在children字段则记录
if (this.data[k].children && this.data[k].children.length) {
// 判断添加多选数据
if (this.allToone && this.children.length === 0) {
this.children.push({
value: 'all',
label: '全部',
parentId: 0
})
}
// 记录数据并渲染到子组件
Array.prototype.push.apply(this.children, this.data[k].children);
this.children.sort(this.sortNumber)
}
});
// 重置checked
this.data.map((val, key) => {
val.checked = false
})
this.checkBoxGroup.map((val, key) => {
this.data[val].checked = true
})
this.destroy = true;
// 触发父组件emit
this.selectedData();
},
// 一级组件点击事件
handleClick(index, item) {
this.selected = index;
if (item.value === 'all') {
let data = [];
for (let i = 1; i < this.data.length; i++) {
data.push(i);
}
this.checkBoxGroup = data;
this.handleCheckBoxChange(data);
} else {
this.checkBoxGroup = [index];
this.handleCheckBoxChange([index]);
}
this.destroy = true;
}
}
};
</script>