brandTree.vue
5.41 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<template>
<d2-dialog title="选择品牌" :is-visible="dialogVisible" class="brand-dialog" width="50%" @closeDialog="handleClose"
@confirmDialog="handleConfirm">
<el-form :inline="true" size="mini" class="demo-form-inline">
<el-form-item label="品牌名称:">
<el-input v-model="filterText" placeholder="请输入" clearable></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSearch">查询</el-button>
</el-form-item>
</el-form>
<div class="content">
<div class="tree-box">
<div class="title">预选品牌</div>
<el-tree ref="tree" node-key="id" :filter-node-method="filterNode" :data="brandData" :props="defaultProps"
show-checkbox @check-change="handleNodeClick"></el-tree>
</div>
<!-- <div class="check-box">
<div class="title">已选品牌
<el-button type="text" @click="resetChecked">清空</el-button>
</div>
<template v-if="checkList.length == 0">
<div class="no-data">暂无数据</div>
</template>
<template v-else>
<div class="check-list">
<div class="check-item" v-for="item in checkList" :key="item.id">{{ item.label }}<i class="el-icon-close"
@click="closeChecked(item)"></i></div>
</div>
</template>
</div> -->
</div>
</d2-dialog>
</template>
<script>
import { deepClone } from "@/common/util"
import { getCarbrandTree } from "@api/contentManager"
export default {
name: "brandTree",
props: {
dialogVisible: Boolean,
defaultKeys: Array
},
data() {
return {
filterText: '',
checked: false,
checkList: [],
brandData: [],
defaultProps: {
children: 'children',
label: 'label'
},
selectList: []
}
},
watch: {
filterText(val) {
this.$refs.tree.filter(val);
},
defaultKeys(val) {
console.log(val)
this.$nextTick(() => {
// val.forEach(i => {
// this.$refs.tree.setChecked(i, true)
// })
this.$refs.tree.setCheckedKeys(val)
})
}
},
mounted() {
this.getCarbrandTree()
},
methods: {
// 获取品牌树
async getCarbrandTree() {
const res = await getCarbrandTree()
this.brandData = res
},
handleClose() {
this.resetChecked()
this.$emit('close')
},
handleConfirm() {
this.resetChecked()
console.log(this.checkList, 'checkList')
const newList = this.filterCheckList(deepClone(this.checkList))
console.log(newList, 'newList')
this.$emit('submit', {
checkList: this.checkList,
newList: newList
})
},
onSearch() {
this.$refs.tree.filter(this.filterText)
},
handleNodeClick(data) {
console.log(data, this.$refs.tree.getCheckedNodes());
this.checkList = this.$refs.tree.getCheckedNodes()
},
filterCheckList(checkList) {
// 收集所有 children 中的 id
const childrenIds = new Set();
checkList.forEach(item => {
if (item.children && item.children.length > 0) {
item.children.forEach(child => {
childrenIds.add(child.id);
});
}
});
// 过滤掉一级项中 id 存在于 childrenIds 中的项
return checkList.filter(item => !childrenIds.has(item.id));
},
// 清空
resetChecked() {
this.$refs.tree.setCheckedKeys([]);
},
closeChecked(row) {
console.log(row)
this.$refs.tree.setChecked(row.id, false)
console.log(this.$refs.tree.getCheckedNodes())
},
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
},
handleChange(e) {
console.log(e)
if (e) {
const nodes = this.getTreeArray()
this.$refs.tree.setCheckedKeys(nodes)
} else {
this.$refs.tree.setCheckedKeys([]);
}
},
//获取选择项
getTreeArray() {
let tree_arr = []
this.brandData.forEach(k => {
let child = k.children
if (child && child.length > 0) {
child.forEach(j => {
tree_arr.push(j.id)
})
} else {
tree_arr.push(k.id)
}
})
return tree_arr
},
}
}
</script>
<style scoped lang="scss">
.content {
display: flex;
.title {
display: flex;
align-items: center;
justify-content: space-between;
height: 40px;
opacity: 1;
border-bottom: 1px solid #e5e5e5;
background: #f2f4f7;
padding: 0 16px;
}
.tree-box {
border: 1px solid #e5e5e5;
background: #ffffff;
height: 324px;
overflow-y: auto;
width: 100%;
// margin-right: 20px;
.tree-title {
display: flex;
align-items: center;
height: 40px;
opacity: 1;
border-bottom: 1px solid #e5e5e5;
background: #f2f4f7;
padding: 0 16px;
}
}
.check-box {
flex: 1;
height: 324px;
opacity: 1;
border: 1px solid #e5e5e5;
}
.no-data {
width: 100%;
height: calc(100% - 40px);
display: flex;
align-items: center;
justify-content: center;
}
.check-list {
margin-top: 12px;
height: calc(100% - 64px);
overflow-y: auto;
}
.check-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 4px 16px;
}
}
</style>
<style lang="scss">
.brand-dialog {
.el-dialog__body {
padding: 20px !important;
}
}
</style>