uniapp 单选/多选 选择激活的菜单或者选项

<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" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值