展示:
子组件封装
< template> < el-popoverref = " popover" placement = " bottom-start" trigger = " click" @show = " onShowPopover" @hide = " onHidePopover" > < el-treeref = " tree" class = " select-tree" highlight-current :style = " `min-width: ${treeWidth}`" :data = " data" :props = " props" :expand-on-click-node = " false" :filter-node-method = " filterNode" :default-expand-all = " true" @node-click = " onClickNode" > </ el-tree> < el-inputslot = " reference" ref = " input" v-model = " labelModel" clearable :style = " `width: ${width}%`" :class = " { ' rotate' : showStatus }" suffix-icon = " el-icon-arrow-down" :placeholder = " placeholder" > </ el-input> </ el-popover>
</ template> < script>
import { getTreeList } from '@/api/commonApi'
export default { name: 'Pagination' , props: { value: { type: String, default : '1' } , width: String, placeholder: { type: String, required: false , default : '请选择' } , props: { type: Object, required: false , default : ( ) => ( { value: 'key' , label: 'title' , children: 'children' } ) } } , model: { prop: 'value' , event: 'selected' } , computed: { dataType ( ) { const jsonStr = JSON . stringify ( this . options) return jsonStr. indexOf ( this . props. children) !== - 1 } , data ( ) { return this . dataType ? this . options : this . switchTree ( ) } } , watch: { labelModel ( val ) { if ( ! val) { this . valueModel = '' } this . $refs. tree. filter ( val) } , value ( val ) { this . value = val} , valueModel ( val ) { if ( ! val) { return } this . timer = setTimeout ( ( ) => { this . labelModel = this . queryTree ( this . options, val) } ) } } , data ( ) { return { showStatus: false , treeWidth: 'auto' , labelModel: '' , valueModel: '0' , options: [ ] , timer: null } } , created ( ) { this . value&& this . getLabel ( this . value) this . gettreetable ( ) this . $nextTick ( ( ) => { this . treeWidth = ` ${ ( this . width || this . $refs. input. $refs. input. clientWidth) - 24 } px ` } ) } , methods: { getLabel ( val ) { this . labelModel = val} , gettreetable ( ) { getTreeList ( { menuName: 'ehis_dept' , pageSearchType: 'all' , searchConditions: '[]' , sortType: 'asc' , sortcol: [ 'id' ] , tableType: 'col' } ) . then ( ( res ) => { this . options = res. allDatathis . labelModel = this . queryTree ( this . options, this . valueModel) } ) . catch ( ( err ) => { console. log ( err) } ) } , onClickNode ( node ) { this . labelModel = node[ this . props. label] this . valueModel = node[ this . props. value] this . $emit ( 'getdocotorlist' , this . valueModel) this . onCloseTree ( ) } , switchTree ( ) { return this . cleanChildren ( this . buildTree ( this . options, '0' ) ) } , onCloseTree ( ) { this . $refs. popover. showPopper = false } , onShowPopover ( ) { this . showStatus = true this . $refs. tree. filter ( false ) } , onHidePopover ( ) { this . showStatus = false this . $emit ( 'selected' , this . valueModel) } , filterNode ( query, data ) { if ( ! query) { return true } return data[ this . props. label] . indexOf ( query) !== - 1 } , queryTree ( tree, id ) { let stark = [ ] stark = stark. concat ( tree) while ( stark. length) { const temp = stark. shift ( ) if ( temp[ this . props. children] ) { stark = stark. concat ( temp[ this . props. children] ) } if ( temp[ this . props. value] === id) { return temp[ this . props. label] } } return '' } , buildTree ( data, id = '0' ) { const fa = ( parentId ) => { const temp = [ ] for ( let i = 0 ; i < data. length; i++ ) { const n = data[ i] if ( n[ this . props. parent] === parentId) { n. children = fa ( n. rowGuid) temp. push ( n) } } return temp} return fa ( id) } , cleanChildren ( data ) { const fa = ( list ) => { list. map ( ( e ) => { if ( e. children. length) { fa ( e. children) } else { delete e. children} return e} ) return list} return fa ( data) } } , beforeUpdate ( ) { clearTimeout ( this . timer) this . timer = null }
}
</ script> < style>
.el-input.el-input--suffix { cursor : pointer; overflow : hidden;
}
.el-input.el-input--suffix.rotate .el-input__suffix { transform : rotate ( 180deg) ;
}
.select-tree { max-height : 350px; overflow-y : scroll;
}
.select-tree::-webkit-scrollbar { z-index : 11; width : 6px;
}
.select-tree::-webkit-scrollbar-track,
.select-tree::-webkit-scrollbar-corner { background : #fff;
}
.select-tree::-webkit-scrollbar-thumb { border-radius : 5px; width : 6px; background : #b4bccc;
}
.select-tree::-webkit-scrollbar-track-piece { background : #fff; width : 6px;
}
.el-tree-node.is-current > .el-tree-node__content { background-color : #86bfff !important ; color : black;
}
</ style>
父组件使用
< select-tree :value = " ruleForm.deptId" v-model = " ruleForm.deptId" style = " width : 93%; " /> import SelectTree from '@/components/common/tree/selecttreecopy'components: { SelectTree },