ElementPlus 之 el-select 多选实现全选功能

前言

经常会遇到这种多选下拉框支持全选的需求,在此简单记录一下在 ElementPlus 框架下,如何使得多选的 el-select 控件支持实现全选功能。

一、示例代码

(1)/src/views/Example/ElSelect/index.vue

<template>
  <div class="index">
    <el-select
      size="small"
      placeholder="请选择游戏"
      value-key="id"
      style="width: 100%"
      multiple
      filterable
      clearable
      collapse-tags
      :reserve-keyword="false"
      collapse-tags-tooltip
      v-model="gameParam.gameSelectedList"
      @remove-tag="handleGameItemRemove"
    >
      <el-option
        v-for="item in gameParam.gameList"
        :key="item.id"
        :label="item.name"
        :title="item.name"
        :value="item"
        @click="handleGameItemClick(item)"
      >
      </el-option>
    </el-select>
  </div>
</template>

<script setup>
import { h, onMounted, onUnmounted, ref, getCurrentInstance, reactive, watch, nextTick } from 'vue'
 
// 代理对象
const { proxy } = getCurrentInstance()

// 游戏参数
const gameParam = reactive({
  // 选中的游戏对象列表
  gameSelectedList: [],
  
  // 游戏列表
  gameList: [
    { 'id': 0, 'name': '全选' },
    { 'id': 1, 'name': '梦幻西游' },
    { 'id': 2, 'name': '暗黑破坏神2' },
    { 'id': 3, 'name': '国家崛起2' },
    { 'id': 4, 'name': '侠盗猎车手圣安地列斯' },
    { 'id': 5, 'name': '月影传说' },
    { 'id': 6, 'name': '地下城守护者2' },
  ],
})

/**
 * 游戏改变事件句柄方法
 */
const handleGameItemClick = (item) => {
  console.log('item =>', item)

  // 当点击“全选”选项时
  if (item.name == '全选') {
    // 判断“已选列表”是否包含“全选”
    let isContain = false
    for (let vo of gameParam.gameSelectedList) {
      if (vo.name == item.name) {
        isContain = true
      }
    }
    console.log('isContain =>', isContain)

    if (isContain) {
      // 若包含“全选”,则全部选中
      gameParam.gameSelectedList = gameParam.gameList
    } else {
      // 若不包含“全选”,则全部不选中
      gameParam.gameSelectedList = []
    }
  }
  // 当点击其它选项时
  else {
    // 判断“已选列表”是否包含“全选”
    const index = gameParam.gameSelectedList.findIndex(
      (item) => item.name === '全选'
    )
    if (index != -1) {
      console.log(gameParam.gameSelectedList.length)
      console.log(gameParam.gameList.length)
      if ((gameParam.gameSelectedList.length + 1) <= gameParam.gameList.length) {
        gameParam.gameSelectedList.splice(index, 1)
      }
    } else {
      console.log(gameParam.gameSelectedList.length)
      console.log(gameParam.gameList.length)
      if ((gameParam.gameSelectedList.length + 1) == gameParam.gameList.length) {
        gameParam.gameSelectedList.push(
          {
            'id': 0,
            'name': '全选'
          }
        )
      }
    }
  }
}

/**
 * 单个移除标签
 */
const handleGameItemRemove = (item) => {
  console.log('item =>', item)

  // 当点击“全选”选项时
  if (item.name == '全选') {
    // 判断“已选列表”是否包含“全选”
    let isContain = false
    for (let vo of gameParam.gameSelectedList) {
      if (vo.name == item.name) {
        isContain = true
      }
    }
    console.log('isContain =>', isContain)

    if (isContain) {
      // 若包含“全选”,则全部选中
      gameParam.gameSelectedList = gameParam.gameList
    } else {
      // 若不包含“全选”,则全部不选中
      gameParam.gameSelectedList = []
    }
  }
  // 当点击其它选项时
  else {
    // 判断“已选列表”是否包含“全选”
    const index = gameParam.gameSelectedList.findIndex(
      (item) => item.name === '全选'
    )
    if (index != -1) {
      console.log(gameParam.gameSelectedList.length)
      console.log(gameParam.gameList.length)
      if ((gameParam.gameSelectedList.length + 1) <= gameParam.gameList.length) {
        gameParam.gameSelectedList.splice(index, 1)
      }
    } else {
      console.log(gameParam.gameSelectedList.length)
      console.log(gameParam.gameList.length)
      if ((gameParam.gameSelectedList.length + 1) == gameParam.gameList.length) {
        console.log(1111)
        gameParam.gameSelectedList.push(
          {
            'id': 0,
            'name': '全选'
          }
        )
      }
    }
  }
}
</script>

<style lang="less" scoped>
.index {
  width: auto;
  height: 100%;
  padding: 0 100px;
  display: grid;
  align-items: center;
  text-align: center;
  background-color: #fafafa;
}
</style>

二、运行效果

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue.js是一种流行的JavaScript框架,用于构建界面。它采用了组件化的开发方式,使得开发者可以将页面拆分成多个独立的组件,每个组件都有自己的逻辑和样式。Vue.js具有简单易学、灵活高效的特点,因此在前端开发中得到了广泛应用。 在Vue.js中,el-select是一个下拉选择框组件,可以用于实现单选或多选功能。要实现el-select多选全选功能,可以通过以下步骤进行: 1. 在Vue组件中引入el-select组件,并设置multiple属性为true,表示启用多选功能。 2. 使用v-model指令绑定一个数组类型的数据,用于存储用户选择的选项。 3. 添加一个全选选项,并使用v-model指令将其与一个布尔类型的数据进行绑定。 4. 监听全选选项的变化,当全选选项被选中时,将所有选项添加到已选择的选项数组中;当全选选项取消选中时,清空已选择的选项数组。 下面是一个示例代码,演示了如何实现el-select多选全选功能: ```html <template> <div> <el-select v-model="selectedOptions" multiple> <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> <el-checkbox v-model="selectAll">全选</el-checkbox> </div> </template> <script> export default { data() { return { options: [ { label: '选项1', value: 'option1' }, { label: '选项2', value: 'option2' }, { label: '选项3', value: 'option3' }, // 其他选项... ], selectedOptions: [], selectAll: false }; }, watch: { selectAll(value) { if (value) { this.selectedOptions = this.options.map(option => option.value); } else { this.selectedOptions = []; } } } }; </script> ``` 在上述代码中,options数组存储了所有的选项,selectedOptions数组存储了用户选择的选项,selectAll变量表示全选选项的状态。通过监听selectAll的变化,可以实现全选功能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值