uniapp列表多选

在这里插入图片描述

选中并获取值,实现的代码如下:

<template>
	<view class="index">
		<view class="new_box" style="margin-top: 20upx;">
			<view class="bbox">
				<view class="list-box" v-for="(item,index) in list" :key="index">
					<view class="list-ed">
						<image class="list-img" src="../../static/logo.png" mode="aspectFill"></image>
						<view class="list-right">
							<view class="list-head" @click="choice(index)">
								<view :class="[item.selected?'selde':'noselde']">{{item.selected?"已选择":"选择"}}<text :class="[item.selected?'selde-q':'noselde-q']"></text></view>
							</view>
							<view class="list-name">{{item.title}}</view>
							<view class="list-da">
								<view>课程:<text>{{item.course}}</text></view>
								<view>时长:<text>{{item.duration}}</text></view>
							</view>
						</view>
					</view>
				</view>
				<view class="sure" @click="sure">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [{
						course_id: "001",
						title: "一个很长很长很长的标题一个很长很长很长的标题一个很长很长很长的标题一个很长很长很",
						course: "体育课",
						duration: "0",
						selected: false
					},
					{
						course_id: "002",
						title: "一个标题",
						course: "音乐课",
						duration: "120",
						selected: false
					},
					{
						course_id: "003",
						title: "好好学习天天向上",
						course: "信息科",
						duration: "80",
						selected: false
					},
				],
				selectId:[],
			};
		},
		methods: {
			//选择课程
			choice(index){
				if(this.list[index].selected == true){
					this.list[index].selected = false;
					//取消选中时删除数组中的值
					for(var i = 0; i < this.selectId.length; i++){
					    if(this.selectId[i] === this.list[index].course_id){
					        this.selectId.splice(i,1);
					    }
					}
					console.log("选中的值",this.selectId)
				}else{
					this.list[index].selected = true;
					this.selectId.push(this.list[index].course_id)
					console.log("选中的值",this.selectId)
				}
			},
			//提交
			sure(){
				//提交选中的值
				if(this.selectId.length==0){
					alert("请选择学科");
					return false;
				}
				var listIds = this.selectId.join(",")
				console.log("提交的数据",listIds)
			}
		
		}
	};
</script>

<style>
	page{
		background-color: #eee;
	}
	/* 已选择 */
	.selde {
		border: 1px solid red;
		background: red;
		color: #FFFFFF;
		border-radius: 20upx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 20upx;
		padding: 0 10upx;
	}

	.selde-q {
		width: 18upx;
		height: 18upx;
		border-radius: 50%;
		background: #FFFFFF;
		margin-left: 6upx;
	}

	/* 未选择 */
	.noselde {
		border: 1px solid #959595;
		background: #FFFFFF;
		color: #959595;
		border-radius: 20upx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 20upx;
		padding: 0 10upx;
	}

	.noselde-q {
		border: 1px solid #959595;
		width: 16upx;
		height: 16upx;
		border-radius: 50%;
		background: #FFFFFF;
		margin-left: 6upx;
	}
	.list-box {
		display: flex;
		flex-direction: column;
		background-color: #fff;
		margin: 0upx 16upx 16upx 16upx;
		padding: 16upx;
		border-radius: 10upx;
	}

	.list-ed {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.list-left {
		margin-right: 16upx;
	}

	.list-img {
		width: 160upx;
		height: 160upx;
		margin: 0upx 16upx 0upx 0upx;
	}

	.list-right {
		display: flex;
		flex-direction: column;
		width: 510upx;
		height: 180upx;
	}

	.list-right-img {
		width: 140upx;
		height: 38upx;
	}

	.noadsop {
		width: 120upx;
		height: 32upx;
	}

	.list-head {
		display: flex;
		justify-content: flex-end;
		margin-bottom: 10upx;
	}

	.list-name {
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		margin-bottom: 10upx;
	}

	.list-da {
		display: flex;
		flex-direction: row;
		font-size: 26upx;
	}

	.list-da view {
		width: 50%;
	}

	.list-da view text {
		color: red;
	}

	.sure {
		background: #FF6000;
		color: #FFFFFF;
		width: 600upx;
		height: 70upx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 40upx;
		margin: 60upx auto;
	}
</style>

记录此时此刻

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以使用uni-app提供的picker组件来实现多选省市的功能。具体实现步骤如下: 1. 在页面中引入picker组件,并定义一个数组用于存储已选中的省市信息: ``` <template> <view> <view class="picker-wrap"> <picker mode="multiSelector" :value="value" @change="onChange"> <view class="picker">{{provinceList[value[0]].name}} {{cityList[value[1]].name}}</view> <view class="picker">{{provinceList[value[2]].name}} {{cityList[value[3]].name}}</view> </picker> </view> </view> </template> <script> export default { data() { return { value: [0, 0, 0, 0], // 默认选中第一个省市 provinceList: [], // 省份列表 cityList: [], // 城市列表 selectedList: [] // 已选中的省市列表 } }, mounted() { // 初始化省份列表和城市列表 this.initData() }, methods: { initData() { // 省份列表和城市列表的数据可以通过接口获取 // 这里只是简单地模拟数据 let provinceList = [ {id: 1, name: '北京'}, {id: 2, name: '上海'}, {id: 3, name: '广东'} ] let cityList = [ [{id: 11, name: '北京市'}], [{id: 21, name: '上海市'}], [{id: 31, name: '广州市'}, {id: 32, name: '深圳市'}] ] this.provinceList = provinceList this.cityList = cityList }, onChange(e) { let value = e.detail.value let selectedList = [] selectedList.push(this.provinceList[value[0]].name + ' ' + this.cityList[value[1]].name) selectedList.push(this.provinceList[value[2]].name + ' ' + this.cityList[value[3]].name) this.selectedList = selectedList } } } </script> <style> .picker-wrap { display: flex; justify-content: center; align-items: center; height: 100vh; } .picker { font-size: 16px; color: #333; padding: 10px; } </style> ``` 2. 在mounted钩子函数中初始化省份列表和城市列表的数据。 3. 在onChange方法中获取当前选中的省市信息,并将其存储到selectedList数组中。 4. 在页面中展示已选中的省市信息。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值