vue實現(xiàn)全選組件封裝實例詳解
更新時間:2022年02月07日 16:10:29 作者:安果移不動
這篇文章主要介紹了vue?全選組件封裝,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
效果

封裝的組件
<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),一般用于實現(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)前選中項
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兩個取值
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": "應(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

