基于Element的組件改造的樹形選擇器(樹形下拉框)
前言:由于做項目需要一個樹形選擇器,項目用的也是element-ui框架,然而它自帶的選擇器組件沒有樹形選項,又不想引入其他的框架組件,于是自己利用el-select和el-tree改造了一個,感覺還挺好用的,就封裝成了一個組件,如下圖:
element-ui的el-select組件的選項只能是列表形式:
改造后的樹形選擇器:
簡介:此樹形選擇器組件是基于elment-ui框架的el-select和el-tree組件的基礎上改造的,其解決了原el-select組件的選項列表不能是樹形的問題,集合了前兩個組件的屬性和方法封裝成了一個組件,引入即可使用。其實現(xiàn)了樹形列表、默認展開、默認選中、清空選值等功能,基本上可以滿足大部分選擇器的使用需求。
主要代碼
組合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' // 子級字段名 } } }, /* 選項列表數(shù)據(jù)(樹形結構的對象數(shù)組) */ 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>
注意:此樹形選擇器要求的值(options)必須是樹形對象數(shù)組,如你的值是扁平數(shù)據(jù),需轉換成樹形數(shù)據(jù)??蓞⒖?a target="_blank" href="http://chabaoo.cn/article/181502.htm">js實現(xiàn)無限層級樹形數(shù)據(jù)結構(創(chuàng)新算法)
到此這篇關于基于Element的組件改造的樹形選擇器(樹形下拉框)的文章就介紹到這了,更多相關Element 樹形選擇器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- 詳解element-ui日期時間選擇器的日期格式化問題
- 詳解關于element級聯(lián)選擇器數(shù)據(jù)回顯問題
- element-ui 時間選擇器限制范圍的實現(xiàn)(隨動)
- Element UI框架中巧用樹選擇器的實現(xiàn)
- vue2.0 element-ui中el-select選擇器無法顯示選中的內(nèi)容(解決方法)
- Vue Element 分組+多選+可搜索Select選擇器實現(xiàn)示例
- jQuery中元素選擇器(element)簡單用法示例
- jQuery選擇器源碼解讀(七):elementMatcher函數(shù)
- jQuery中element選擇器用法實例
- jQuery 屬性選擇器element[herf*=''value'']使用示例
相關文章
在Vue里如何把網(wǎng)頁的數(shù)據(jù)導出到Excel的方法
這篇文章主要介紹了在Vue里如何把網(wǎng)頁的數(shù)據(jù)導出到Excel,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09解決nuxt頁面中mounted、created、watch執(zhí)行兩遍的問題
這篇文章主要介紹了解決nuxt頁面中mounted、created、watch執(zhí)行兩遍的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11在vue使用clipboard.js進行一鍵復制文本的實現(xiàn)示例
這篇文章主要介紹了在vue使用clipboard.js進行一鍵復制文本的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01