vue实现下拉多选、可搜索、全选功能(示例代码)

 更新时间:2025年01月01日 08:32:35   作者:emoji111111  
本文介绍了如何在Vue中实现一个树形结构的下拉多选组件,支持任意一级选项的选择,全选功能,以及搜索功能,通过在mounted生命周期中获取数据,并使用handleTree函数将接口返回的数据整理成树形结构,实现了这一功能,感兴趣的朋友一起看看吧

最后的效果就是树形的下拉多选,可选择任意一级选项,下拉框中有一个按钮可以实现全选,也支持搜索功能。

在mounted生命周期里面获取全部部门的数据,handleTree是讲接口返回的数据整理成树形结构,可以自行解决

             <div class="LeftText">
                <span style="color: red; margin-right: 4px">*</span>部门:
              </div>
              <el-select
                v-model="executiveDepartName"
                filterable
                :filter-method="selectChange"
                multiple
                @visible-change="visibleChange"
                @remove-tag="seleRemoveTag"
                style="width: 80%"
              >
                <el-option style="display: none" value=""></el-option>
                <el-checkbox
                  style="
                    width: 100%;
                    height: 40px;
                    line-height: 40px;
                    padding-left: 20px;
                    border-bottom: 1px solid #dcdfe6;
                  "
                  class="allselect"
                  :indeterminate="isIndeterminate"
                  v-model="allSelectModule"
                  @change="allselect"
                  >全选</el-checkbox
                >
                <el-cascader-panel
                  ref="cascaderModule"
                  :key="deptList.length"
                  :options="deptList"
                  @change="cascaderChange"
                  style="width: 80%"
                  :props="props"
                  filterable
                  :border="false"
                  :show-all-levels="false"
                  v-model="executiveDepartment"
                >
                </el-cascader-panel>
              </el-select>
            </div>
     props: {
        multiple: true,
        value: "deptId",
        label: "deptName",
        checkStrictly: true,
        emitPath: false,
      },   
    allDeptList:[];//所有的部门信息,内部结构为:{deptId:1,deptName:"一级部门"}
    isSeach:false;//是否搜索状态
    tempExecutive:[];// 搜索前已选中的数据
    //搜索查询事件--是因为在cascaderChange事件中,对v-model的值重新赋值,导致下拉选时,会触发el-select的搜索事件,所以加了一个isFilter判断
    selectChange(val) {
      if (val !== "") {
        this.deptList = [];
        this.deptList = this.allDeptList.filter((item) => {
          return item.deptName.toLowerCase().indexOf(val.toLowerCase()) > -1;
        });
        this.isSeach = true;
        this.tempExecutive = this.executiveDepartment;
      } else {
        if (!this.isFilter) {
          this.deptList = this.handleTree(this.allDeptList, "deptId");
          this.isFilter = !this.isFilter;
        }
      }
    },
    visibleChange(e) {
      if (e) {
        this.isSeach = false;
        this.isFilter = false;
        this.deptList = this.handleTree(this.allDeptList, "deptId");
        this.initStatus();
      }
    },
    对全选状态进行重新赋值
    initStatus() {
      if (this.executiveDepartment.length == this.allDeptList.length) {
        this.allSelectModule = true;
        this.isIndeterminate = false;
      } else if (this.executiveDepartment.length == 0) {
        this.allSelectModule = false;
        this.isIndeterminate = false;
      } else {
        this.allSelectModule = false;
        this.isIndeterminate = true;
      }
    },
    //select框里回显的是选中部门的名称
    getDeptName() {
      const result = [];
      this.executiveDepartment.filter((item) => {
        this.allDeptList.map((i) => {
          if (item == i.deptId) {
            result.push(i.deptName);
          }
        });
      });
      return result;
    },
    seleRemoveTag(val) {
      if (val) {
        const result = this.allDeptList.find((item) => {
          if (item.deptName == val) {
            return item;
          }
        });
        this.executiveDepartment = this.executiveDepartment.filter(
          (item) => item !== result.deptId
        );
      }
    },
    // 下拉多选选中时触发的事件
    cascaderChange() {
      this.isFilter = true;
      //如果是搜索状态,讲之前选中的值和搜素状态下的值进行合并和去重,否则,之前选中的值会被清空
      if (this.isSeach) {
        this.executiveDepartment = [
          ...new Set([...this.tempExecutive, ...this.executiveDepartment]),
        ];
      }
      this.executiveDepartName = this.getDeptName();
      this.initStatus();
    },
    //全选事件
    allselect() {
      if (this.allSelectModule) {
        this.isIndeterminate = false;
        if (this.isSeach) {
          this.executiveDepartment = this.deptList.map((item) => item.deptId);
          this.executiveDepartName = this.getDeptName();
        } else {
          this.executiveDepartment = this.getAllIds(this.deptList);
          this.executiveDepartName = this.getDeptName();
        }
      } else {
        this.executiveDepartment = [];
        this.executiveDepartName = [];
      }
    },
    getAllIds(nodes) {
      let ids = [];
      (function getIds(nodes) {
        nodes.forEach((node) => {
          ids.push(node.deptId);
          if (node.children && node.children.length) {
            getIds(node.children);
          }
        });
      })(nodes);
      return ids;
    },

到此这篇关于vue实现下拉多选、可搜索、全选功能的文章就介绍到这了,更多相关vue下拉框多选内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue-cli如何关闭Uncaught error的全屏提示

    vue-cli如何关闭Uncaught error的全屏提示

    这篇文章主要介绍了vue-cli如何关闭Uncaught error的全屏提示问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • 一文详解vue各种权限控制与管理实现思路

    一文详解vue各种权限控制与管理实现思路

    这篇文章主要为大家介绍了vue各种权限控制与管理的实现思路详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • vue3+ts+vite+electron搭建桌面应用的过程

    vue3+ts+vite+electron搭建桌面应用的过程

    这篇文章主要介绍了vue3+ts+vite+electron搭建桌面应用的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • vue + typescript + 极验登录验证的实现方法

    vue + typescript + 极验登录验证的实现方法

    这篇文章主要介绍了vue + typescript + 极验 登录验证的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • vue之父子组件间通信实例讲解(props、$ref、$emit)

    vue之父子组件间通信实例讲解(props、$ref、$emit)

    组件间如何通信,也就成为了vue中重点知识了。这篇文章将会通过props、$ref和 $emit 这几个知识点,来讲解如何实现父子组件间通信。
    2018-05-05
  • 详解VueJs前后端分离跨域问题

    详解VueJs前后端分离跨域问题

    本篇文章主要介绍了详解VueJs前后端分离跨域问题,详细介绍了在项目内设置代理(proxyTable)的方式来解决跨域问题,有兴趣的可以了解一下
    2017-05-05
  • vue插槽slot的理解和使用方法

    vue插槽slot的理解和使用方法

    这篇文章主要给大家介绍了关于vue中插槽slot的理解和使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04
  • Vue3中实现选取头像并裁剪

    Vue3中实现选取头像并裁剪

    这篇文章主要详细介绍了在vue3中如何选取头像并裁剪,文章中有详细的代码示例,需要的朋友可以参考阅读
    2023-04-04
  • vue实现网易云音乐纯界面

    vue实现网易云音乐纯界面

    这篇文章主要为大家介绍了vue实现网易云音乐纯界面过程详解,附含详细源码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • vue的style绑定background-image的方式和其他变量数据的区别详解

    vue的style绑定background-image的方式和其他变量数据的区别详解

    今天小编就为大家分享一篇vue的style绑定background-image的方式和其他变量数据的区别详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-09-09

最新评论