当前位置: 首页 > article >正文

【el-select 一键便捷全选】

el-select 一键便捷全选

  • 基于el-select 二次封装
  • 页面组件应用
  • 示例图

基于el-select 二次封装

<template>
  <div>
    <!-- el-select 绑定的值 -->
    <el-select
      v-model="selectedValues"
      :multiple="multiple"
      :filterable="filterable"
      :clearable="clearable"
      :placeholder="`请选择${name}`"
      :style="{width: width}"
    >
      <!-- 全选复选框 -->
      <div
        v-if="multiple"
        style="display: flex; align-items: center; padding: 0 20px;"
      >
        <el-checkbox
          :value="isAllSelected"
          @change="handleSelectAll"
        >
          全选
        </el-checkbox>
      </div>
      <!-- 选项列表 -->
      <el-option
        v-for="item in options"
        :key="item[valueKey]"
        :label="item[labelKey]"
        :value="item[valueKey]"
      />
    </el-select>
  </div>
</template>

<script>
export default {
    /**
   *** 作者: Lenovo-【Lindon】
   *** 文件名称: SelectAll
   *** 文件创建日期: 2025/3/20 10:36
   ***
   */
    name: 'SelectAll',
    props: {
        multiple: {
            type: Boolean,
            default: true
        },
        width: {
            type: String,
            default: '370px'
        },
        filterable: {
            type: Boolean,
            default: true
        },
        clearable: {
            type: Boolean,
            default: true
        },

        name: {
            type: String,
            default: ''
        },
        // 选项
        options: {
            type: Array,
            default: () => []
        },
        labelKey: {
            type: String,
            default: 'label'
        },
        valueKey: {
            type: String,
            default: 'value'
        },
        // 已选中的值
        // 双向绑定的值,谨慎修改
        value: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {};
    },
    computed: {
    // 判断是否全选
        isAllSelected() {
            if (this.selectedValues) {
                const allValues = this.options.map(option => option[this.valueKey]);
                return this.selectedValues.length === allValues.length;
            }
        },
        // 双向绑定选中值
        selectedValues: {
            get() {
                debugger;
                return this.value;
            },
            set(val) {
                debugger;
                this.$emit('input', val);
            }
        }
    },
    methods: {
    // 全选逻辑
        handleSelectAll(val) {
            const allValues = this.options.map(option => option[this.valueKey]);
            if (val) {
                // 如果勾选全选,则将所有选项的值添加到选中值中
                this.selectedValues = allValues;
            } else {
                // 如果取消全选,则清空选中值
                this.selectedValues = [];
            }
        }
    }
};
</script>

<style scoped>
/* 全选复选框样式 */
.el-checkbox {
  margin-bottom: 10px;
}
</style>

页面组件应用

        <el-form-item
          label="合作店面"
          prop="orgIds"
        >
          <SelectAll
            v-model="form.orgIds"
            name="工作地方"
            width="370px"
            :options="AllOrg"
            :label-key="'name'"
            :value-key="'id'"
          />
        </el-form-item>

示例图

在这里插入图片描述


http://www.kler.cn/a/594781.html

相关文章:

  • 服务器托管如何抵御网络病毒?
  • AI小白的第七天:必要的数学知识(四)
  • Java面试核心知识点 深度拆解+高频易错
  • 设计模式之责任链模式:原理、实现与应用
  • 问题记录(一)——引入WebSocket依赖时的不兼容或冲突问题
  • 2025最新电脑IP地址修改方法:Win系统详细步骤
  • C++ - 从零实现Json-Rpc框架-1(JsonCpp Muduo 异步操作)
  • 四、小白学JAVA-石头剪刀布游戏
  • YZi Labs 谈对 Plume 的投资:利用区块链创造现实价值的典范项目
  • 【Linux】Makefile秘籍
  • 前端技巧:精准判断登录设备是移动端还是 PC 端
  • 数据可视化(matplotlib)-------辅助图标的设置
  • 一键融合,尽享全能生活:三网融合系统在酒店弱电方案中的应用探索
  • 【嵌入式】复刻SQFMI开源的Watchy墨水屏电子表——(2)软件部分
  • NineData云原生智能数据管理平台新功能发布|2025年2月版
  • ​《引力透镜:Relax Max用哈勃光学系统重构排泄物天体力学》​
  • MapStruct 使用教程
  • 技术分享 | MySQL内存使用率高问题排查
  • 如何用C++封装纯C写的函数库,如何处理C函数调用返回错误
  • OpenNJet:下一代云原生应用引擎,支持动态配置与高效管理,简化运维任务,提升应用灵活性与安全性。