elementUI选择框实现全选功能
如默
撰写于 2023年 04月 29 日

前言

如题,前两天写一个功能的时候需要用到选择框的多选功能,记录一下

正文

elementUI的官方选择框是没有全选功能的,整理一下思路,在选择框里添加一个新的选项,全选,点击全选的时候,isAll方法用来确定是否点击了全选,selectClick方法则用来进行判断,逻辑很简单,代码如下:

对应的element组件:

      <el-form-item label="考核人员" prop="userIds">
        <el-select
          v-model="form.userIds"
          multiple
          filterable
          placeholder="请选择考核人员"
          style="width: 80%"
        >
        <!-- 全选功能 -->
          <el-option
            key="all"
            value="all"
            label="全选"
            @click.native="isAll"
          ></el-option>
          <!-- 选择人员 -->
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
            @click.native="selectClick"
          >
          </el-option>
        </el-select>
      </el-form-item>

实现方法:

    selectClick() {
      if (this.userList.length > this.form.userIds.length && this.form.userIds.includes("all")) {
          this.form.userIds.splice(this.form.userIds.indexOf("all"), 1);
      } else if (this.userList.length === this.form.userIds.length && !this.form.userIds.includes("all")) {
        this.form.userIds = ['all', ...this.form.userIds]
      } else {
        if (this.form.userIds.includes("all")) {
        this.form.userIds.splice(this.form.userIds.indexOf("all"), 1);
      }
      }
    },
    isAll() {
      if (this.form.userIds.includes("all")) {
        this.form.userIds = [
          "all",
          ...this.userList.map((item) => item.userId),
        ];
      } else {
        this.form.userIds = [];
      }
    }

elementUI选择框实现全选功能

温馨提示:

本文最后更新于2023年04月29日,已超过663天没有更新,若内容或图片失效,请留言反馈。

前言

如题,前两天写一个功能的时候需要用到选择框的多选功能,记录一下

正文

elementUI的官方选择框是没有全选功能的,整理一下思路,在选择框里添加一个新的选项,全选,点击全选的时候,isAll方法用来确定是否点击了全选,selectClick方法则用来进行判断,逻辑很简单,代码如下:

对应的element组件:

      <el-form-item label="考核人员" prop="userIds">
        <el-select
          v-model="form.userIds"
          multiple
          filterable
          placeholder="请选择考核人员"
          style="width: 80%"
        >
        <!-- 全选功能 -->
          <el-option
            key="all"
            value="all"
            label="全选"
            @click.native="isAll"
          ></el-option>
          <!-- 选择人员 -->
          <el-option
            v-for="item in userList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
            @click.native="selectClick"
          >
          </el-option>
        </el-select>
      </el-form-item>

实现方法:

    selectClick() {
      if (this.userList.length > this.form.userIds.length && this.form.userIds.includes("all")) {
          this.form.userIds.splice(this.form.userIds.indexOf("all"), 1);
      } else if (this.userList.length === this.form.userIds.length && !this.form.userIds.includes("all")) {
        this.form.userIds = ['all', ...this.form.userIds]
      } else {
        if (this.form.userIds.includes("all")) {
        this.form.userIds.splice(this.form.userIds.indexOf("all"), 1);
      }
      }
    },
    isAll() {
      if (this.form.userIds.includes("all")) {
        this.form.userIds = [
          "all",
          ...this.userList.map((item) => item.userId),
        ];
      } else {
        this.form.userIds = [];
      }
    }


赞 (0)

猜您想看

  • AWS亚马逊云入坑记录

    作为云服务的老大,想来品质应该是可以的,听说有一年的免费主机,所以试试。

    2019年02月07日
  • Python实现Gitee码云webhook

    之前使用Vuepress做文档网站,每次更新之后都需要手动构建前端页面,繁琐,不方便,想起来webhook,自动配置之后发现还是方便,记录一下。

    2023年11月23日
  • Windows10 1903版更新体验

    前一阵子太忙了,各种乱七八糟的事情,以及新备案了一个网站(某个莫名其妙的项目),所以网站关闭了好一阵子。 草稿很多,但是都没有完善,今天水一篇。

    2019年06月12日
  • 微信开放平台账号注销方法

    最近需要注册一个公众号,发现邮箱都被占用了,需要将微信开放平台的邮箱释放,但是官方没有注销的入口,几经周折,最后终于注销,特此记录。

    2022年08月05日
  • VScode编写C语言中文乱码问题

    在VScode上写C语言,总是出现中文乱码问题,找了好多方法都不管用,最后发现了一个方法解决了问题,特此记录。

    2020年03月04日
  • 关于京东京造和耐时干电池的使用体验

    由于平时键盘和鼠标以及Xbox手柄用的都是干电池,所以也关注了市面上的很多电池,下面就记录一下使用情况,供大家参考。

    2024年04月28日

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论

Vaptcha 初始化中...