<template>
<view class="check--list">
<view class="check--list-title">
<text
class="fs32 c1 title"
v-if="title"
:style="{ width: showAll && multiple ? '88%' : '100%' }"
>
{{ title }}
</text>
<text class="fs26 c6 all" v-if="showAll && multiple" @click="handleCheckAll">全选</text>
</view>
<view class="check--list-data">
<view
class="list-data-item fs28 ls1"
:class="{ 'list-data-item-act': item.isAct }"
v-for="(item, index) in dataList"
:key="index"
@click="handleCheckItem(item, index)"
>
{{ item[label] }}
</view>
</view>
</view>
</template>
<script>
export default {
name: 'CheckList',
components: {},
props: {
data: {
type: Array,
default: () => []
},
title: {
type: String,
default: ''
},
showTitle: {
type: Boolean,
default: true
},
all: {
type: Boolean,
default: false
},
showAll: {
type: Boolean,
default: true
},
label: {
type: String,
default: 'title'
},
disabled: {
type: Boolean,
default: false
},
multiple: {
type: Boolean,
default: false
},
keys: {
type: String,
default: ''
},
active: {
type: Array,
default: () => []
},
idKey: {
type: String,
default: ''
},
useActKey: {
type: Boolean,
default: false
}
},
data() {
return {
dataList: []
};
},
onLoad(params) {},
created() {},
mounted() {},
methods: {
// 全选
handleCheckAll() {
if (this.disabled) return;
let bol = this.dataList.every((item) => item.isAct);
if (bol) this.dataList.map((item) => (item.isAct = false)) ;
else this.dataList.map((item) => (item.isAct = true));
let arr = this.dataList.map((item) => item[this.label]);
let arrIds = [];
if (this.idKey && !bol) {
arrIds = this.dataList.map((item) => item[this.idKey]);
}
if(bol){
arr = []
}
this.$emit('check', arr, this.multiple, this.keys, arrIds);
},
// 单个勾选
handleCheckItem(item, index) {
if (this.disabled) return;
let arr = [];
let arrIds = [];
if (this.multiple) {
this.dataList[index].isAct = !this.dataList[index].isAct;
arr = this.dataList.filter((items) => items.isAct);
arr = arr.map((items) => items[this.label]);
if (this.idKey) {
let arr = this.dataList.filter((items) => items.isAct);
arrIds = arr.map((items) => items[this.idKey]);
}
} else {
arr = item[this.label];
if (this.idKey) {
arrIds = item[this.idKey];
}
}
this.$emit('check', arr, this.multiple, this.keys, arrIds);
},
getActiveField() {
if (this.useActKey) return this.idKey;
return this.label;
}
},
computed: {
},
watch: {
data: {
handler(newVal, oldVal) {
let arr = [];
if (this.active?.length && this.multiple) {
arr = newVal.map((item) => ({
...item,
isAct: this.active.includes(item[this.getActiveField()]) || false
}));
} else if (this.active?.length && !this.multiple) {
arr = newVal.map((item) => ({
...item,
isAct: this.active[0].includes(item[this.getActiveField()]) || false
}));
} else {
arr = newVal.map((item) => ({ ...item, isAct: false }));
}
this.dataList = arr;
console.log('warch this.dataList..', this.dataList);
},
deep: true,
immediate: true
},
active: {
handler(newVal, oldVal) {
if (newVal) {
this.dataList = this.dataList.map((item) => ({
...item,
isAct: this.active.includes(item[this.getActiveField()]) || false
}));
}
},
deep: true,
immediate: true
}
}
};
</script>
<style scoped lang="scss">
.check--list {
.check--list-title {
width: 100%;
height: 72rpx;
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 20rpx;
.title {
width: 88%;
}
.all {
margin-left: auto;
width: 12%;
}
}
.check--list-data {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
margin-bottom: 30rpx;
.list-data-item {
width: 48%;
height: 100rpx;
color: #222222;
background: #f5f6fa;
border: 2rpx solid transparent;
border-radius: 10rpx;
margin-top: 20rpx;
padding: 20rpx;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.list-data-item-act {
background: #f1f5fe;
color: #4675f7;
border-color: #bccffa;
}
}
}
</style>
使用方式 :
<CheckList :data="areaDataList" keys="id" multiple showAll @check="handleCheckCity" />