<treetree v-model="search.departmentId" @closeTree="visibled = false" :data="$store.state.publicInterface.depart" :nodeKey="'id'" :props="{ label: 'title', children: 'children' }" placeholder="请选择" @change="handleSelectionChange"></treetree>
<template>
<div>
<el-select :title="optionData.name" ref="select" :value="value" :placeholder="placeholder" @focus="sss" clearable :disabled="disabled" size="mini" :filterable="filterable" :filter-method="filterMethod" style="width: 100%" @clear="clear" @visible-change="visibleChange"> -->
<el-option ref="option" class="tree-select__option" :value="optionData.id" :label="optionData.name"> -->
<el-tree style="" accordion ref="tree" class="tree-select__tree tree-select__tree--radio" :node-key="nodeKey" :data="data" :props="props" :default-expanded-keys="[3, 98]" :highlight-current="true" :filter-node-method="filterNode" @node-click="handleNodeClick"></el-tree>
</el-option>
</el-select>
</div>
</template>
<script>
export default {
name: "TreeSelect",
props: {
placeholder: {
type: String
},
value: {
type: [String, Number],
default: ""
},
data: {
type: Array,
default: function () {
return [];
}
},
nodeKey: {
type: [String, Number],
default: "id"
},
filterable: {
type: Boolean,
default: true
},
disabled: {
type: Boolean,
default: false
},
props: {
type: Object,
default: function () {
return {
label: "title",
children: "children"
};
}
}
},
data() {
return {
optionData: {
id: "",
name: ""
},
filterFlag: false
};
},
watch: {
value: {
handler(val) {
if (!this.isEmpty(this.data)) {
this.init(val);
}
},
immediate: true
},
data: function (val) {
if (!this.isEmpty(val)) {
this.init(this.value);
}
}
},
created() {},
mounted() {
document.body.addEventListener("click", this.handleBodyClick);
this.$nextTick(() => {
this.$refs.select.scrollTop = 0;
});
},
beforeDestroy() {
document.body.removeEventListener("click", this.handleBodyClick);
},
methods: {
sss() {
this.$nextTick(() => {
this.$refs.select.scrollTop = 0;
});
},
isEmpty(val) {
for (let key in val) {
return false;
}
return true;
},
handleNodeClick(data) {
const selectedValue = data[this.nodeKey];
if (selectedValue !== this.value) {
this.optionData.name = data[this.props.label];
this.$emit("input", selectedValue);
this.$emit("change", selectedValue);
this.$emit("closeTree", false);
}
this.$refs.select.visible = false;
},
init(val) {
val = val === "" ? null : val;
this.$nextTick(() => {
this.$refs.tree.setCurrentKey(val);
if (val === null) {
return;
}
const node = this.$refs.tree.getNode(val);
this.optionData.id = val;
this.optionData.name = node.label;
});
this.flag = true;
},
visibleChange(e) {
if (e) {
const tree = this.$refs.tree;
this.filterFlag && tree.filter("");
this.filterFlag = false;
let selectDom = tree.$el.querySelector(".is-current");
setTimeout(() => {
this.$refs.select.scrollToOption({ $el: selectDom });
}, 0);
}
},
clear() {
if (this.value) {
this.$emit("input", "");
this.$emit("change", "");
}
},
filterMethod(val) {
this.filterFlag = true;
this.$refs.tree.filter(val);
},
filterNode(value, data) {
if (!value) return true;
const label = this.props.label || "name";
return data[label].indexOf(value) !== -1;
},
handleBodyClick(event) {
if (this.$refs.select && !this.$refs.select.$el.contains(event.target)) {
this.$refs.select.visible = false;
}
}
}
};
</script>
<style lang="scss">
.tree-select__option {
&.el-select-dropdown__item {
height: 100%;
line-height: 1;
padding: 0;
background-color: yellowgreen !important;
}
}
.tree-select__tree {
color: #333333;
padding: 4px 8px;
font-weight: 400;
&.tree-select__tree--radio {
.el-tree-node.is-current > .el-tree-node__content {
font-weight: 700;
color: #e21c2d;
}
}
}
.el-tree .el-tree-node__expand-icon.expanded {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
//有子节点 且未展开
.el-tree .el-icon-caret-right:before {
background: url("../../assets/common/jiafa.png") no-repeat 0;
content: "";
display: block;
width: 20px;
height: 16px;
font-size: 16px;
background-size: 16px;
margin-right: 4px;
}
//有子节点 且已展开
.el-tree .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
background: url("../../assets/common/jianfa.png") no-repeat 0;
content: "";
display: block;
width: 20px;
height: 16px;
font-size: 16px;
background-size: 16px;
}
//没有子节点
.el-tree .el-tree-node__expand-icon.is-leaf::before {
background: no-repeat 0;
content: "";
display: block;
width: 20px;
height: 16px;
font-size: 16px;
}
</style>