Vue3 Element-plus 封装Select下拉复选框选择器

废话不多说,样式如下,代码如下,需要自取

cpp 复制代码
<template>
  <el-select
    v-model="selectValue"
    class="checkbox-select"
    multiple
    :placeholder="placeholder"
    :style="{ width: width }"
    @change="changeSelect"
  >
    <div class="checkboxWrapper">
      <el-checkbox v-model="checked"> 全选 </el-checkbox>
    </div>
    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
      <el-checkbox :model-value="selectValue.includes(item.value)">
        {{ item.label }}
      </el-checkbox>
    </el-option>
  </el-select>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useVModel } from '@vueuse/core'

interface Option {
  label: string
  value: number
}

interface Props {
  modelValue: number[]
  options: Option[]
  placeholder?: string
  width?: string
}

interface Emits {
  (e: 'update:modelValue', value: number[]): void
  (e: 'change', value: number[]): void
}

const props = withDefaults(defineProps<Props>(), {
  placeholder: '请选择',
  width: '200px',
})

const emit = defineEmits<Emits>()

defineOptions({
  name: 'CheckboxSelect',
})

// 使用useVModel处理数据同步
const selectValue = useVModel(props, 'modelValue', emit)

// 计算全选状态
const checked = computed({
  get: () => {
    const resValues = props.options.map((item) => item.value)
    return selectValue.value.length === resValues.length
  },
  set: (value: boolean) => {
    const resValues = props.options.map((item) => item.value)
    if (!value) {
      // 如果checkbox为false,则为反选,选择数组为空
      emit('update:modelValue', [])
    } else {
      // 如果checkbox为true,则为全选,选择数组添加所有数据
      emit('update:modelValue', [...resValues])
    }
  },
})

// 选择变化处理
const changeSelect = (val: number[]) => {
  emit('change', val)
}
</script>

<style scoped>
.checkbox-select {
  width: 100%;
}

.checkboxWrapper {
  padding: 0px 20px;
}

/* 选项复选框样式 */
:deep(.el-select-dropdown__item .el-checkbox) {
  width: 100%;
  margin: 0;
}

:deep(.el-select-dropdown__item .el-checkbox .el-checkbox__label) {
  padding-left: 8px;
}
</style>
cpp 复制代码
 <CheckboxSelect
	v-model="searchForm.statusList"
	:options="statusOptions"
	placeholder="请选择状态"
	width="200px"
	@change="changeHandle"
/>

// 搜索表单
const searchForm = reactive({
  statusList: [] as number[], // 多选数组
})

// 状态选项
const statusOptions = ref([
  { label: '启用', value: 1 },
  { label: '禁用', value: 2 },
])

// 选择变化处理
const changeHandle = (val: number[]) => {
  console.log(val, '状态选择变化')
}
相关推荐
Jing_Rainbow8 分钟前
【Vue-2/Lesson62(2025-12-10)】模块化与 Node.js HTTP 服务器开发详解🧩
前端·vue.js·node.js
冴羽1 小时前
2026 年 Web 前端开发的 8 个趋势!
前端·javascript·vue.js
五仁火烧1 小时前
Vue3 项目的默认端口行为
服务器·vue.js·nginx·容器·vue
fengbizhe2 小时前
bootstrapTable转DataTables,并给有着tfoot的DataTables加滚动条
javascript·bootstrap
刘一说2 小时前
TypeScript 与 JavaScript:现代前端开发的双子星
javascript·ubuntu·typescript
EndingCoder2 小时前
类的继承和多态
linux·运维·前端·javascript·ubuntu·typescript
用户47949283569152 小时前
React 终于出手了:彻底终结 useEffect 的"闭包陷阱"
前端·javascript·react.js
Younglina3 小时前
一个纯前端的网站集合管理工具
前端·vue.js·chrome
木头程序员3 小时前
前端(包含HTML/JavaScript/DOM/BOM/jQuery)基础-暴力复习篇
开发语言·前端·javascript·ecmascript·es6·jquery·html5
哈__3 小时前
React Native 鸿蒙跨平台开发:PixelRatio 实现鸿蒙端图片的高清显示
javascript·react native·react.js