【uniapp 开发小程序】购物车功能,实现全选、反选、单选、计算总价

news/2024/7/20 2:09:23 标签: uni-app, 小程序

小程序购物车功能:

<template>
	<view>
		<view class="goodlistItem" v-for="(item,index) in goodList" :key='index'>
			<view class="group">
				<checkbox-group @change="changeitem(item)" checked class="checkbox">
					<checkbox :value="item.unitPrice" :checked="item.checked" />
				</checkbox-group>

				<view class="goods">
					<view class="price">¥{{item.unitPrice}}</view>
					<view class="name">{{item.name}}</view>
					<view class="order-info">
						<view>产地:{{item.home}}</view>
					</view>

					<view class="subtotal">
						<view class="text">
							<view>单品小计:</view>
							<view>¥{{item.subtotal}}</view>
						</view>
						<view class="number">
							<wm-numberBox :value='item.numberBox' model="1"
								@change="addShopCart($event, item, index)"></wm-numberBox>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="allbox">
			<view class="checkbox">
				<checkbox-group @change="selectAll">
					<checkbox :checked="allChecked">全选</checkbox>
				</checkbox-group>
			</view>

			<view class="total-box">
				<view class="total">合计:¥{{totalPrice}}</view>
			</view>

		</view>
	</view>

</template>

<script>
	import url from '../../static/api/url';
	export default {
		data() {
			return {
				goodList: [{
						id: 0,
						unitPrice: '23',
						name: '文具',
						home: '山东',
						subtotal: '23',
						numberBox: 1,
						checked: false
					},
					{
						id: 1,
						unitPrice: '50',
						name: '图书',
						home: '济阳',
						subtotal: '50',
						numberBox: 1,
						checked: false
					}
				],
				checkList: [], //选中值
				allChecked: false, //是否全选

			}
		},

		//计算总价
		computed: {
			totalPrice() {
				let totalPrice = 0
				this.goodList.map(item => {
					item.checked ? totalPrice += item.numberBox * item.unitPrice : totalPrice += 0
				})
				return totalPrice
			}
		},
		methods: {
		
			//单选
			changeitem(item) {
				item.checked = !item.checked
				if (!item.checked) {
					this.allChecked = false
				} else {
					// 判断每一个商品是否是被选择的状态
					const cartList = this.goodList.every(item => {
						return item.checked === true
					})
					if (cartList) {
						this.allChecked = true
					} else {
						this.allChecked = false
					}
				}
			},
			
			//全选,全不选
			selectAll() {
				this.allChecked = !this.allChecked
				if (this.allChecked) {
					this.goodList.map(item => {
						item.checked = true
					})
				} else {
					this.goodList.map(item => {
						item.checked = false
					})
				}
			},
		}

	}
</script>


http://www.niftyadmin.cn/n/476630.html

相关文章

element-ui中el-table设置固定高度后,底部合计栏被遮盖

如图: 解决办法:el-table加上ref"summaryTab",然后在自定义合计计算方法getSummaries中加上如下代码就ok了 this.$nextTick(() > {this.$refs.summaryTab.doLayout(); }); 没用使用自定义合计计算函数的,也可以写在updated中,如下: updated() {this.$nextTick((…

在k8s上部署vue

1. dockerfile镜像文件编写 # 拉取 nginx镜像 FROM nginx:1.24.0# 拷贝 nginx 配置文件到 docker中 COPY nginx.conf /etc/nginx/nginx.conf# 拷贝vue打包后的文件到 docker中 COPY webapp /usr/share/nginx/html# 新增时区设置 RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai…

最新Mac上如何安装pod

在Mac上安装CocoaPods需要使用Ruby&#xff0c;请按照以下步骤操作&#xff1a; 安装rvm&#xff0c;这是 Ruby Version Manager 的缩写&#xff0c;它是一个命令行工具&#xff0c;用于安装不同版本的 Ruby。打开终端并输入以下命令&#xff1a;$ curl -L https://get.rvm.io…

Java POI (2)—— Excel文件的上传与导出(实例演示)

一、单文件的上传功能 这里是一个demo的流程图&#xff0c;下面按照这个流程图做了一个简单的实现&#xff0c;有部分判断没有加上&#xff0c;实际操作中&#xff0c;可以根据自己的需求进行增加或者修改。并且此处还是在接受文件传入后将文件进行了下载&#xff0c;保存到本地…

React Hooks MUI ,父子传值,双向绑定,memo,useCallback

React Hooks MUI ,父子传值&#xff0c;双向绑定&#xff0c;memo,useCallback 用法。 useCallback 配合 memo使用避免重复渲染子组件。子组件封装成表单&#xff0c;方便直接调用。 子组件 import { useState, memo } from "react"; import {Grid,FormControl,Fo…

Jetson 开机报错:no module named jtop.core.jetson_variables

环境说明&#xff1a; ubuntu18.04&#xff0c;python3.6. jetpack4.6.4&#xff0c;jetson-stats4.0.0rc3 开机报错如下 报错分析 Error found when loading /etc/profile, 因此打开该文件查看&#xff0c;文件内容指向 /etc/profile.d 文件夹&#xff0c;打开该文件夹 发现存…

Redis 使用Lettuce,出现命令执行超时问题

问题出现&#xff1a; org.springframework.dao.QueryTimeoutException: Redis command timed out; nested exception is io.lettuce.core.RedisCommandTimeoutException: io.lettuce.core.RedisCommandTimeoutExcep …

Oracle Recovery Tools恢复csc higher than block scn---惜分飞

有客户强制关闭数据库,结果有数据块报坏块,dbv检查为:csc higher than block scn问题 该问题主要是由于scn异常导致通过Oracle Recovery工具进行修复 dbv再次验证数据块ok,Oracle Recovery完美代替bbed解决该问题 通过OraRecovery工具快速解决csc higher than block scn故障…