前言:由於做項目需要一個樹形選擇器,項目用的也是element-ui框架,然而它自帶的選擇器組件沒有樹形選項,又不想引入其他的框架組件,於是自己利用el-select和el-tree改造了一個,感覺還挺好用的,就封裝成了一個組件,如下圖:
element-ui的el-select組件的選項只能是列表形式:
改造后的樹形選擇器:
簡介:此樹形選擇器組件是基於elment-ui框架的el-select和el-tree組件的基礎上改造的,其解決了原el-select組件的選項列表不能是樹形的問題,集合了前兩個組件的屬性和方法封裝成了一個組件,引入即可使用。其實現了樹形列表、默認展開、默認選中、清空選值等功能,基本上可以滿足大部分選擇器的使用需求。
主要代碼
組合el-select和el-tree組件:
<template>
<el-select :value="valueTitle" :clearable="clearable" @clear="clearHandle">
<el-option :value="valueTitle" :label="valueTitle">
<el-tree id="tree-option"
ref="selectTree"
:accordion="accordion"
:data="options"
:props="props"
:node-key="props.value"
:default-expanded-keys="defaultExpandedKey"
@node-click="handleNodeClick">
</el-tree>
</el-option>
</el-select>
</template>
封裝組件:
<script>
export default {
name: "el-tree-select",
props:{
/* 配置項 */
props:{
type: Object,
default:()=>{
return {
value:'id', // ID字段名
label: 'title', // 顯示名稱
children: 'children' // 子級字段名
}
}
},
/* 選項列表數據(樹形結構的對象數組) */
options:{
type: Array,
default: ()=>{ return [] }
},
/* 初始值 */
value:{
type: Number,
default: ()=>{ return null }
},
/* 可清空選項 */
clearable:{
type:Boolean,
default:()=>{ return true }
},
/* 自動收起 */
accordion:{
type:Boolean,
default:()=>{ return false }
},
},
data() {
return {
valueId:this.value, // 初始值
valueTitle:'',
defaultExpandedKey:[]
}
},
mounted(){
this.initHandle()
},
methods: {
// 初始化值
initHandle(){
if(this.valueId){
this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label] // 初始化顯示
this.$refs.selectTree.setCurrentKey(this.valueId) // 設置默認選中
this.defaultExpandedKey = [this.valueId] // 設置默認展開
}
this.$nextTick(()=>{
let scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
let scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
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 = []
},
// 清除選中
clearHandle(){
this.valueTitle = ''
this.valueId = null
this.defaultExpandedKey = []
this.clearSelected()
this.$emit('getValue',null)
},
/* 清空選中樣式 */
clearSelected(){
let allNode = document.querySelectorAll('#tree-option .el-tree-node')
allNode.forEach((element)=>element.classList.remove('is-current'))
}
},
watch: {
value(){
this.valueId = this.value
this.initHandle()
}
},
};
</script>
css樣式:
<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;
}
</style>
查看demo
注意:此樹形選擇器要求的值(options)必須是樹形對象數組,如你的值是扁平數據,需轉換成樹形數據。可參考js實現無限層級樹形數據結構(創新算法)
------------------------------------------ 我是有底線的 -------------------------------------------
2019.05.01更新 --- 解決初始化時滾動條滾到底部的bug
2019.05.04更新 --- 解決默認值再次改變時頁面沒渲染的問題
————————————————
版權聲明:本文為CSDN博主「蔚萊先森」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/Mr_JavaScript/article/details/88604270