vue?的全選組件封裝你知道多少
更新時間:2022年02月09日 10:49:43 作者:安果移不動
這篇文章主要為大家詳細介紹了vue的全選組件封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
效果
封裝的組件
<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, //全選默認不勾選 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, 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) }, //選中后計算全選 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": "應用寶", "yyh": "應用匯", };
父組件使用
<el-card shadow="hover"> <multiple-choice :item="item" label="渠道/廣告開關" 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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
vue?element-ui如何在el-tabs組件最右側(cè)添加按鈕
這篇文章主要介紹了vue?element-ui如何在el-tabs組件最右側(cè)添加按鈕問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07vue-baidu-map 進入頁面自動定位的解決方案(推薦)
這篇文章主要介紹了vue-baidu-map 進入頁面自動定位的解決方案,需要的朋友可以參考下2018-04-04