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, '状态选择变化')
}
相关推荐
丷丩3 小时前
MapLibre GL JS第19课:实时更新要素
前端·javascript·gis·map·mapbox·maplibre gl js
xiaohua0708day4 小时前
Lodash库
前端·javascript·vue.js
万物皆对象6664 小时前
切换路由时页面空白问题(vue3)
前端·vue.js·typescript
突然好热4 小时前
TS 调试技巧
前端·javascript·typescript
h64648564h4 小时前
Flutter 国际化(i18n)全指南:一键切换中/英/日多语言
前端·javascript·flutter
丷丩6 小时前
MapLibre GL JS第8课:禁用滚动缩放
javascript·mapbox·maplibre gl js
李剑一6 小时前
小红书前端架构面试问的挺深入啊!面试官:Vue中组合式API与选项式API的设计权衡
vue.js·面试
一 乐7 小时前
汽车租赁|基于SprinBoot+vue的汽车租赁管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·汽车·论文·毕设·汽车租赁管理系统
kyriewen7 小时前
面试8家前端岗位后,我发现了一个残酷的事实:AI不是加分项,是门槛
前端·javascript·面试
MageGojo10 小时前
做节日活动页时,如何用 API 快速生成对联内容
javascript·python·节日·对联生成