vue?的全選組件封裝你知道多少
更新時間:2022年02月09日 10:49:43 作者:安果移不動
這篇文章主要為大家詳細(xì)介紹了vue的全選組件封裝,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
效果

封裝的組件
<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,
required: true
},
formDBName: {
type: String,
required: true
}
},
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兩個取值
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("|");
this.setChooseData(formData)
},
},
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;
}總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue實(shí)現(xiàn)大轉(zhuǎn)盤抽獎功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)大轉(zhuǎn)盤抽獎功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信
全局事件總線就是一種組件間通信的方式,適用于任意組件間通信,下面這篇文章主要給大家介紹了關(guān)于Vue2中如何使用全局事件總線實(shí)現(xiàn)任意組件間通信的相關(guān)資料,需要的朋友可以參考下2022-12-12
vue與electron實(shí)現(xiàn)進(jìn)程間的通信詳情
這篇文章主要介紹了vue與electron實(shí)現(xiàn)進(jìn)程間的通信詳情,本文主要介紹electron渲染進(jìn)程和主進(jìn)程間的通信,以及在渲染進(jìn)程和主進(jìn)程中常用的配置項(xiàng),需要的朋友可以參考一下2022-09-09
如何利用Vue3+Element?Plus實(shí)現(xiàn)動態(tài)標(biāo)簽頁及右鍵菜單
標(biāo)簽頁一般配合菜單實(shí)現(xiàn),當(dāng)你點(diǎn)擊一級菜單或者二級菜單時,可以增加對應(yīng)的標(biāo)簽頁,當(dāng)你點(diǎn)擊對應(yīng)的標(biāo)簽頁,可以觸發(fā)對應(yīng)的一級菜單或者二級菜單,下面這篇文章主要給大家介紹了關(guān)于如何利用Vue3+Element?Plus實(shí)現(xiàn)動態(tài)標(biāo)簽頁及右鍵菜單的相關(guān)資料,需要的朋友可以參考下2022-11-11
使用WebSocket+SpringBoot+Vue搭建簡易網(wǎng)頁聊天室的實(shí)現(xiàn)代碼
這篇文章主要介紹了使用WebSocket+SpringBoot+Vue搭建簡易網(wǎng)頁聊天室的實(shí)現(xiàn),具體的步驟如下,需要的朋友可以參考下2023-03-03

