下拉树级带搜索功能

可以直接复制粘贴到自己的项目里,方法处把接口替换一下

html 复制代码
<template>
  <div>
    <el-popover
      placement="bottom"
      width="200"
      trigger="click">
      <el-input
        slot="reference"
        class="mrInput"
        :placeholder="placeholder"
        v-model="nowSelVal"
        clearable
        :disabled="disabled"
        @input="changeValTime"
        @change="changeVal"
        @clear="clearVal"
      />
    <div class="customTreeSelectCont">
      <el-tree
        v-if="searchTit"
        :props="treeProps"
        :load="loadNode"
        empty-text="暂无数据"
        lazy
        :expand-on-click-node="false"
        @node-click="handleNodeClick"
      />
      <ul class="searchUl" v-else>
        <li
          v-for="(item,index) in searchOptions"
          :key="'searchUl__'+index"
          @click="selSearchLi(item)"
        >
          {{item.label}}
        </li>
      </ul>
    </div>
    </el-popover>
  </div>
</template>

<script>
export default {
  name: "customTreeSelect",
  data() {
    return {
      nowSelVal:'',
      nowSelValBF:'',
      placeholder:'',
      disabled:false,
      treeProps: {
        label: 'label',
        value: 'value',
        children: 'children',
        isLeaf: 'leaf'
      },
      searchTit:true,
      searchSel:'',
      searchOptions:[]
    }
  },
  methods: {
    // 搜索框,实时
    changeValTime(val) {
      if(!val){
        this.searchTit = true
      }else{
        this.searchTit = false
        this.getSearchSelFun()
      }
    },
    // 搜索框,失去焦点或用户按下回车
    changeVal(val) {
      console.log('changeVal');
    },
    // 搜索框-清空
    clearVal() {
      this.searchTit = true
    },
    getSearchSelFun(){
      this.searchOptions = [
        {
          value: '选项1',
          label: '黄金糕'
        },{
          value: '选项2',
          label: '黄金糕22'
        },{
          value: '选项3',
          label: '黄金糕33'
        },
      ]
    },
    // tree的懒加载
    loadNode(node, resolve) {
      // 第一次获取数据
      if (node.level === 0) {
        const arr = [
          {
            label: '测试1',
            value: '111'
          },{
            label: '测试2',
            value: '222'
          },
        ]
        return resolve(arr)
      } else {
        // 之后点击获取数据
        if(node.level === 5) return resolve([])
        const datas = [{
          label: 'mmm',
          value: '555'
        }]
        return resolve(datas)
      //   // return resolve([])
      }
    },
    handleNodeClick(data) {
      this.nowSelVal = data[this.treeProps.label]
    },
    selSearchLi(data) {
      console.log(data);
      this.nowSelVal = data[this.treeProps.label]
    },
  }
}
</script>

<style lang="scss" scoped>
  .mrInput{
    width: 433px;
  }
  .searchUl{
    padding:0;
    margin:0;
    list-style: none;
    li{
      padding:5px 10px;
      cursor: pointer;
      &:hover{
        background: #f5f7fa;
       }
    }
  }
  .customTreeSelectCont{
    max-height: 500px;
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 6px;
    }
    //默认不展示
    &::-webkit-scrollbar-thumb {
      border-radius: 10px;
      background: rgba(0,0,0,0);
    }
    //划过展示,具体写法自己改改
    &:hover::-webkit-scrollbar-thumb {
      background: rgba(0,0,0,0.1);
    }
    &::-webkit-scrollbar-track {
      border-radius: 0;
      background: rgba(0,0,0,0);
    }
  }
</style>

成果是这样的,可以手动试一下

相关推荐
明月_清风28 分钟前
性能级目录同步:IntersectionObserver 实战
前端·javascript
明月_清风30 分钟前
告别暴力轮询:深度解锁浏览器“观察者家族”
前端·javascript
摸鱼的春哥33 分钟前
Agent教程17:LangChain的持久化和人工干预
前端·javascript·后端
程序员爱钓鱼2 小时前
Go操作Excel实战详解:github.com/xuri/excelize/v2
前端·后端·go
子兮曰10 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github
恋猫de小郭10 小时前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
GIS之路12 小时前
ArcGIS Pro 中的 Notebooks 入门
前端
IT_陈寒14 小时前
React状态管理终极对决:Redux vs Context API谁更胜一筹?
前端·人工智能·后端
lemon_yyds14 小时前
《vue 2 升级vue3 父组件 子组件 传值: value 和 v-model
vue.js
Kagol15 小时前
TinyVue 支持 Skills 啦!现在你可以让 AI 使用 TinyVue 组件搭建项目
前端·agent·ai编程