index.vue
5.66 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
<style>
.eagle-tree-search {
padding-bottom: 10px;
}
.eagle-tree-node {
flex: 1;
display: flex;
align-items: center;
justify-content: space-between;
padding: 0px 5px;
}
.eagle-tree-node-active {
color: #1890ff;
}
.eagle-tree-tip {
padding-bottom: 10px;
border-bottom: 1px solid #e8e8e8;
}
.eagle-tree {
padding-top: 10px;
}
.green {
color: #52c41a;
}
.red {
color: #f5222d;
}
</style>
<template>
<div>
<el-input v-model="showText" readonly :disabled="disabled" :size="size" :placeholder="placeholder" @focus="dialogVisible = true"></el-input>
<el-dialog
:title="title"
:visible.sync="dialogVisible"
:close-on-click-modal="false"
:append-to-body="true"
width="30%"
>
<div v-if="filterable" class="eagle-tree-search">
<el-input placeholder="输入关键字进行过滤" v-model="filterText" size="small"></el-input>
</div>
<div class="eagle-tree-tip">
<span v-if="selected">已选中:<span class="red">{{ selected[treeProps.label] }}</span></span>
<span v-else>请选择:</span>
</div>
<el-tree ref="tree" class="eagle-tree" :data="treeData" :props="treeProps" :node-key="nodeKey" @node-click="handleNodeClick" :filter-node-method="filterNode" :expand-on-click-node="false">
<span class="eagle-tree-node" :class="{ 'eagle-tree-node-active': selected && selected[nodeKey] === data[nodeKey] }" slot-scope="{ node, data }">
<span>{{ node.label }}</span>
<span v-if="selected && selected[nodeKey] === data[nodeKey]">
<i class="green el-icon-success"></i>
</span>
</span>
</el-tree>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="handleConfirm">确 定</el-button>
<el-button @click="dialogVisible = false">取 消</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
name: 'TreeSelect',
props: {
// 组件值
value: [Object, String, Number],
// 弹出框标题
title: {
type: String,
default: '选择'
},
// 输入框提示
placeholder: {
type: String,
default: '请选择'
},
// 输入框禁用状态
disabled: {
type: Boolean,
default: false
},
// 是否可搜索
filterable: {
type: Boolean,
default: true
},
// 输入框大小
size: String,
// 格式化值类型 text object
format: {
type: String,
default: 'text'
},
// 树组件值格式
treeProps: {
type: Object,
default: () => {
return { children: 'children', label: 'label' };
}
},
// 树唯一标识
nodeKey: {
type: String,
default: 'id'
},
// 是否每次打开弹出框刷新树数据
uptodate: {
type: Boolean,
default: false
},
// 数据源
dataSource: {
type: [Promise, Function, Array],
required: true,
},
// 是否动态数据源
dynamicSource: {
type: Boolean,
default: false
},
},
data() {
return {
// 弹出框显示状态
dialogVisible: false,
// 搜索文本
filterText: undefined,
// 树数据
treeData: [],
// 树数据列表
treeDataList: [],
// 选中值
selected: undefined,
};
},
watch: {
// 输入搜索文本进行过滤
filterText(val) {
this.$refs.tree.filter(val);
},
// 显示弹出框刷新树数据
dialogVisible(val) {
if (val && this.uptodate) {
this.queryTreeData();
}
},
// 值为空时清空选中与搜索
value(val) {
if (!val) {
this.selected = undefined;
this.filterText = undefined;
}
},
dataSource(val) {
if (this.dynamicSource) {
this.queryTreeData();
}
}
},
computed: {
// 输入框显示值
showText() {
return this.format === 'text' ? this.textValue : !this.value ? undefined : this.value[this.treeProps.label];
},
// text模式输入框显示渲染
textValue() {
const find = this.treeDataList.find(data => data[this.nodeKey] === this.value);
const obj = !find ? {} : find;
return obj[this.treeProps.label];
}
},
mounted() {
this.queryTreeData();
},
methods: {
// 过滤树数据
filterNode(value, data) {
if (!value) return true;
return data.label.indexOf(value) !== -1;
},
// 查询树数据
async queryTreeData() {
if (this.dataSource instanceof Array) {
this.treeData = this.dataSource;
} else {
this.treeData = await this.dataSource();
}
this.treeDataList = this.generateTreeList([...this.treeData]);
},
// 递归树数据
generateTreeList(value) {
const list = [];
const generateChild = (child, result) => {
return child.forEach(data => {
result.push({ ...data, [this.treeProps.children]: undefined });
if (data[this.treeProps.children]) {
generateChild(data[this.treeProps.children], result);
}
});
};
generateChild(value, list);
return list;
},
// 确定选择
handleConfirm() {
this.dialogVisible = false;
if (this.format === 'text') {
this.$emit('input', this.selected && this.nodeKey ? this.selected[this.nodeKey] : undefined);
} else {
this.$emit('input', this.selected);
}
},
// 点击树节点选择
handleNodeClick(data, node) {
if (this.selected && this.nodeKey && this.selected[this.nodeKey] === data[this.nodeKey]) {
this.selected = undefined;
} else {
this.selected = data;
}
}
}
};
</script>