el-select + el-tree选择(包含模糊查询功能)
本文内容是我自己写的简单封装,如需功能齐全的可以自己补充或者使用其它现有的组件库:vue-treeselect
<div class="menuSearch"> <el-select class="el-select-menu" ref="selectTree" v-model="filterMenuText" placeholder="请输入菜单名称" filterable :filter-method="filterMenu" @clear="filterMenu" clearable > <el-option style="height: auto;background-color: white;min-width: 270px;font-weight: normal" :label="defaultOption.menuName" :value="defaultOption.path" > <el-tree ref="tree" v-if="showTree" :data="menu" :props="defaultProps" node-key="menuId" @node-click="nodeClick" :filter-node-method="filterNode" /> </el-option> </el-select> </div>
filterMenuText: '', // 查询菜单 showTree: true, defaultProps: { children: 'children', label: 'menuName' }, defaultOption: { path: '', menuName: '' }
nodeClick(item) { const { path, menuName } = item this.defaultOption.path = path this.defaultOption.menuName = menuName this.filterMenuText = menuName this.$refs.selectTree.blur() if (!item.children) { this.$router.push(path) } }, filterMenu(val) { if (!val) {//清空时重置树 this.showTree = false this.$nextTick(() => { this.showTree = true }) } this.$refs.tree.filter(val) }, filterNode(value, data) { if (!value) return true return data.menuName.indexOf(value) !== -1 }
// 修改el-select的icon
/deep/.el-select-menu .el-input .el-input__suffix .el-input__suffix-inner { .el-icon-arrow-up:before{ content: "\e778"; } .el-select__caret{ transform: rotateZ(0deg); } }