vue购物车效果

本文介绍了如何使用Vue.js实现购物车效果,包括全选、加减操作和总价计算。通过创建表格并利用v-for指令渲染数据,当全选状态改变时,更新选中商品列表。单选变化时,根据选中商品数量与总商品数量比较更新全选状态。加减按钮点击事件中,确保数量在合理范围内,并更新总价,总价为选中商品价格之和。

一、介绍

vue购物车效果实现了全选、加减和总价等效果

 二、过程

首先写一个表格,第一行写序号、名称等、后面通过v-for遍历data里面的list渲染页面,最后写总价,每一个商品后面跟的单独的总价是当前商品的单价乘以当前商品的数量:

<table id="app" border="1" cellspacing="0" cellpadding="0" width="600" height="500">
			<tr align="center">
				<th><input type="checkbox" v-model="check" @change="checkedchange" /></th>
				<th>序号</th>
				<th>名称</th>
				<th>个数</th>
				<th>单价</th>
				<th>库存</th>
				<th>总价</th>
			</tr>
			<tr v-for="item in list">
				<td><input type="checkbox" v-model="listxin" :value="item" @change="hun"/></td>
				<th>{{item.id}}</th>
				<th>{{item.name}}</th>
				<th><button @click="jian(item)">-</button>{{item.quantity}}<button @click="jia(item)">+</button></th>
				<th>{{item.price}}</th>
				<th>{{item.amount-item.quantity}}</th>
				<th>{{item.quantity*item.price}}</th>
			</tr>
			<tr>
				<th colspan="7">总价:{{zongj()}}</th>
			</tr>
		</table>

data

data: {
				listxin: [],
				shu: 1,
				check: false,
				list: [{
					"id": "001",
					"name": "iPhone12",
					"price": 8900,
					"amount": 5,
					"quantity": 1,
					"total": 8900,
					"check": false
				}, {
					"id": "002",
					"name": "iPhone11",
					"price": 6000,
					"amount": 8,
					"quantity": 1,
					"total": 6000,
					"check": false
				}, {
					"id": "003",
					"name": "iPhoneMax10",
					"price": 3000,
					"amount": 10,
					"quantity": 1,
					"total": 3000,
					"check": false
				}, {
					"id": "004",
					"name": "iPhoneMin9",
					"price": 2000,
					"amount": 12,
					"quantity": 1,
					"total": 2000,
					"check": false
				}]
			},

全选改变时触发checkedchange事件判断当前check的值,全选通过v-model绑定check,如果是true把list赋值给listxin,反之listxin的值为空,从第二行的每一行的CheckBox都通过v-model绑定listxin,如果listxin为空是false,反之是true。

checkedchange() {
					if(this.check){
						this.listxin=this.list
					}else{
						this.listxin=[]
					}
				}

单选改变时触发hun事件,判断listxin的长度与list的长度是否相等。如果相等,代表所有的单选都已经选中,所以check为true。如果不相等,代表单选没有全部选中,所以check为false:

hun(){
					if(this.listxin.length===this.list.length){
						this.check=true
					}else{
						this.check=false
					}
				}

加减是点击之后分别触发jia和jian事件,把当前商品作为实参接收。如果是加的或需要判断当前数量减去库存是否小于0,如果大于等于0,则数量等于当前商品的总库存。如果是减的话,需要判断数量是否大于0,如果不是数量等于1.

jian(item){
					item.quantity--
					if(item.quantity<=0){
						item.quantity=1
					}
				},
				jia(item){
					item.quantity++
					if(item.quantity-item.amount>=0){
						item.quantity=item.amount
					}
				},

所有商品的总价是加上listxin里面的商品的总价得来的,因为只有选中的商品的价格才会相加。创建一个值为0的变量,然后遍历listxin,最后每个商品的数量乘以单价相加返回最后的值,就得到了总价:

zongj(){
    var sun=0;
    for(var i=0;i<this.listxin.length;i++){
        sun+=this.listxin[i].quantity*this.listxin[i].price
    }
    return sun
}

总结:

到这里购物车就介绍完了,祝大家生活愉快

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值