功能如下展示:
在公共的地方寫好被引用的樹
<template>
<el-select
ref="selector"
:value="valueTitle"
:clearable="clearable"
@clear="clearHandle"
>
<el-input
v-model="filterText"
class="selectInput"
:placeholder="placeholder"
/>
<el-option
:value="valueTitle"
:label="valueTitle"
class="options"
>
<el-tree
id="tree-option"
ref="selectTree"
:accordion="accordion"
:data="options"
:props="props"
:node-key="props.value"
:default-expanded-keys="defaultExpandedKey"
:filter-node-method="filterNode"
@node-click="handleNodeClick"
/>
</el-option>
</el-select>
</template>
<script>
export default {
name: 'ElTreeSelect',
props: {
/* 配置項 */
props: {
type: Object,
default: () => {
return {
value: 'id', // ID字段名
label: 'title', // 顯示名稱
children: 'children', // 子級字段名
};
},
},
/* 選項列表數據(樹形結構的對象數組) */
options: {
type: Array,
default: () => { return []; },
},
/* 初始值 */
// type: Number,
value: {
type: String,
default: () => { return null; },
},
/* 可清空選項 */
clearable: {
type: Boolean,
default: () => { return true; },
},
/* 自動收起 */
accordion: {
type: Boolean,
default: () => { return false; },
},
placeholder: {
type: String,
default: () => { return '檢索關鍵字'; },
},
},
data() {
return {
filterText: '',
valueId: this.value, // 初始值
valueTitle: '',
defaultExpandedKey: [],
};
},
watch: {
value() {
this.valueId = this.value;
this.initHandle();
},
filterText(val) {
this.$refs.selectTree.filter(val);
},
},
mounted() {
this.initHandle();
},
methods: {
// 初始化值
initHandle() {
if (this.valueId) {
console.log(this.options);
this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label]; // 初始化顯示
this.$refs.selectTree.setCurrentKey(this.valueId); // 設置默認選中
this.defaultExpandedKey = [this.valueId]; // 設置默認展開
}
this.initScroll();
},
// 初始化滾動條
initScroll() {
this.$nextTick(() => {
const scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0];
const scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar');
scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;';
// eslint-disable-next-line no-return-assign
scrollBar.forEach(ele => ele.style.width = 0);
});
},
// 切換選項
handleNodeClick(node) {
this.valueTitle = node[this.props.label];
this.valueId = node[this.props.value];
this.$emit('getValue', this.valueId);
this.defaultExpandedKey = [];
this.$refs.selector.blur();
},
// 清除選中
clearHandle() {
this.valueTitle = '';
this.valueId = null;
this.defaultExpandedKey = [];
this.clearSelected();
this.$emit('getValue', null);
},
/* 清空選中樣式 */
clearSelected() {
const allNode = document.querySelectorAll('#tree-option .el-tree-node');
allNode.forEach((element) => element.classList.remove('is-current'));
},
filterNode(value, data) {
if (!value) return true;
return data.name.indexOf(value) !== -1;
},
},
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.el-scrollbar .el-scrollbar__view .el-select-dropdown__item {
height: auto;
max-height: 274px;
padding: 0;
overflow: hidden;
overflow-y: auto;
}
.el-select-dropdown__item.selected {
font-weight: normal;
}
ul li >>> .el-tree .el-tree-node__content {
height: auto;
padding: 0 20px;
}
.el-tree-node__label {
font-weight: normal;
}
.el-tree >>> .is-current .el-tree-node__label {
color: #409eff;
font-weight: 700;
}
.el-tree >>> .is-current .el-tree-node__children .el-tree-node__label {
color: #606266;
font-weight: normal;
}
.selectInput {
padding: 0 5px;
box-sizing: border-box;
width: 100%;
}
</style>
引用樹
// 引用樹的結構
<el-form-item
label="xxx"
:rules="rules.Null"
prop="area"
>
<SelectTree
:props="props"
:options="admittanceAreaTrees"
:value="enterdirectory.enterArea"
:clearable="isClearable"
:accordion="isAccordion"
style="display: inline;"
@getValue="getValue($event,'3180143')"
/>
</el-form-item>
// 在一級目錄下面
import SelectTree from '../selectTree.vue';
script>
export default {
data() {
return {
enterdirectory:[],
// 樹的值
admittanceAreaTrees: [],
props: {
// 配置項(必選)
parent: 'parentId', // 父級唯一標識
value: 'id',
label: 'name',
children: 'children',
},
// 可清空(可選)
isClearable: true,
// 可收起(可選)
isAccordion: false,
filterText: '',
},
};
},
// 樹引用
components: {
SelectTree,
},
methods: {
getValue (value, code) {
if (code === '3180145') {
this.enterdirectory['gdj'] = value;
} else if (code === '3180143') {
this.enterdirectory['enterArea'] = value;
}
// 取值
// this.valueId = value;
},
// 獲取樹的值(鉤子函數)
// 合同類型下拉框-樹
selectContractTypeList() {
selectContractType({}).then((response) => {
this.contractTypeList = response.data;
});
},
},
};
</script>