vue實(shí)現(xiàn)全選組件封裝實(shí)例詳解
效果
封裝的組件
<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 的不確定狀態(tài),一般用于實(shí)現(xiàn)全選的效果 isIndeterminateBool: true, //全選默認(rèn)不勾選 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 }, //當(dāng)前選中項(xiàng) item: { type: Object, formDBName: { methods: { getArrayCheckList() { return (this.item[this.formDBName] || '').split("|").filter(str => (!!str)); //將數(shù)據(jù)返回給父組件 setChooseData(data) { this.$emit("choose-data", this.formDBName, data) //value 代表選中還是未選中 ture false兩個(gè)取值 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) //選中后計(jì)算全選 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": "應(yīng)用寶", "yyh": "應(yīng)用匯", };
父組件使用
<el-card shadow="hover"> <multiple-choice :item="item" label="渠道/廣告開關(guān)" form-d-b-name="channel" @choose-data="onCheckResult"></multiple-choice> </el-card>
item[channle] 是存入字符串的以|分割的數(shù)據(jù)
比如
baidu|anguo|yyb
這樣
onCheckResult
onCheckResult(dbName, res) { this.item[dbName] = res; }
到此這篇關(guān)于vue 全選組件封裝的文章就介紹到這了,更多相關(guān)vue 全選組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Vite搭建vue3+TS項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了使用Vite搭建vue3+TS項(xiàng)目的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01electron+vue?實(shí)現(xiàn)靜默打印功能
這篇文章主要介紹了electron+vue?實(shí)現(xiàn)靜默打印功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06在vue3項(xiàng)目中給頁(yè)面添加水印的實(shí)現(xiàn)方法
這篇文章主要給大家介紹一下在vue3項(xiàng)目中添加水印的實(shí)現(xiàn)方法,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,感興趣的小伙伴跟著小編一起來看看吧2023-08-08開啟Vue項(xiàng)目缺少node_models包的問題及解決
這篇文章主要介紹了開啟Vue項(xiàng)目缺少node_models包的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09