vue實(shí)現(xiàn)全選組件封裝實(shí)例詳解
更新時(shí)間:2022年02月07日 16:10:29 作者:安果移不動(dòng)
這篇文章主要介紹了vue?全選組件封裝,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(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,
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="渠道/廣告開(kāi)關(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)文章
如何使用 Deepseek 寫(xiě)的uniapp油耗計(jì)算器
這篇文章主要介紹了如何使用 Deepseek 寫(xiě)的uniapp油耗計(jì)算器,下面是一個(gè)基于 Uniapp 的油耗計(jì)算器實(shí)現(xiàn),包含 Vue 組件和頁(yè)面代碼,需要的朋友可以參考下2025-04-04
window.onresize在vue中只能使用一次,自適應(yīng)resize報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了window.onresize在vue中只能使用一次,自適應(yīng)resize報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了如何使用Vue實(shí)現(xiàn)點(diǎn)擊按鈕小球加入購(gòu)物車動(dòng)畫(huà),文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

