基於Element-UI的組件改造的樹形選擇器(樹形下拉框)

前言:由於做項目需要一個樹形選擇器,項目用的也是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: "selectTree",
  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>

 

父組件引用:

 <select-tree style="width:86%" :props="props"
                           :options="treeData"
                           :value="valueId"
                           :clearable="isClearable"
                           :accordion="isAccordion"
                           @getValue="getValue($event)"></select-tree>
<script>
import SelectTree from '@/components/selectTree'

export default {
  name: 'areaBox',
  components: { SelectTree },
  data () {
    return {
      treeData: [],
      isClearable: true,
      valueId: 20,
      props: {
         value: 'id',
         label: 'label',
         children: 'children'
      },
  },
  created () {
    const that = this
    get(globalAPI.treeData).then(function (res) {
      that.treeData = res.list
    }).catch(function (err) {
      console.log(err)
    })
  },
 methods: {
    // selectTree取值
    getValue (value) {
      this.valueId = value
    },
  }
}

 

發佈了11 篇原創文章 · 獲贊 1 · 訪問量 823
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章