vue实现全选组件封装实例详解

Judy ·
更新时间:2024-11-10
· 835 次阅读

效果

 封装的组件

<template> <el-form-item :label="label"> <el-checkbox :indeterminate="isIndeterminateBool" v-model="checkAll" @change="handleCheckAllChange">全选 </el-checkbox> <el-checkbox-group v-model="checkList" @change="handleCheckedCitiesChange"> <el-checkbox :label="key" v-for="(item,key) in this.channelList" :key="key">{{ item }} </el-checkbox> </el-checkbox-group> </el-form-item> </template> <script> import {channelList} from "@/utils/app-channel"; export default { name: "Index", data() { return { //渠道列表 全部渠道 channelList: channelList, // checkbox 的不确定状态,一般用于实现全选的效果 isIndeterminateBool: true, //全选默认不勾选 checkAll: false, data: this.checkList, } }, computed: { checkList: { get: function () { return (this.item[this.formDBName] || '').split("|").filter(str => (!!str)); }, set: function (newValue) { this.item[this.formDBName] = newValue.join("|"); } props: { //表单名称 label: { type: String, required: true }, //当前选中项 item: { type: Object, formDBName: { methods: { getArrayCheckList() { return (this.item[this.formDBName] || '').split("|").filter(str => (!!str)); //将数据返回给父组件 setChooseData(data) { this.$emit("choose-data", this.formDBName, data) //value 代表选中还是未选中 ture false两个取值 handleCheckAllChange(value) { const chooseChannel = Object.keys(this.channelList) this.checkList = value ? chooseChannel : []; this.isIndeterminateBool = false; this.checkAll = value; const formData = this.checkList.join("|"); this.setChooseData(formData) //选中后计算全选 handleCheckedCitiesChange(value) { const chooseChannel = Object.keys(this.channelList); let checkedCount = value.length; this.checkAll = checkedCount === chooseChannel.length; this.isIndeterminateBool = checkedCount > 0 && checkedCount < chooseChannel.length; const formData = value.join("|"); mounted() { // .split("|").filter(str => (!!str && typeof (str) == 'string')) } } </script> <style scoped> </style>

渠道列表 

// export const channelList = { "anguo": "安果", "baidu": "百度", "huawei": "华为", "samsung": "三星", "oppo": "OPPO", "sanliuling": "360", "meizu": "魅族", "vivo": "VIVO", "wandoujia": "豌豆荚", "xiaomi": "小米", "yyb": "应用宝", "yyh": "应用汇", };

父组件使用

<el-card shadow="hover"> <multiple-choice :item="item" label="渠道/广告开关" form-d-b-name="channel" @choose-data="onCheckResult"></multiple-choice> </el-card>

item[channle] 是存入字符串的以|分割的数据

比如

baidu|anguo|yyb

这样

onCheckResult onCheckResult(dbName, res) { this.item[dbName] = res; }

到此这篇关于vue 全选组件封装的文章就介绍到这了,更多相关vue 全选组件内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!



VUE 封装 全选

需要 登录 后方可回复, 如果你还没有账号请 注册新账号