Vue方法实现一个较完整的购物车结算

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>todolist加购物车</title>
		<link rel="stylesheet" href="css/bootstrap.css" />
		<script type="text/javascript" src="js/vue.js" ></script>
	</head>
	<body>
		<div id="app">
			<div class="container">
				<table class="table table-bordered table-hover text-center">
					<tr>
						<td>
							<input type="checkbox" v-model="checkAll" @click="selectAll"/>
						</td>
						<td>
							商品名称
						</td>
						<td>
							商品价格
						</td>
						<td>
							商品数量
						</td>
						<td>
							商品总额
						</td>
						<td>
							操作
						</td>
					</tr>
					<tr v-for="(item,index) in listInfo">
						
						<td>
							<input 
								type="checkbox" 
								:value="item.id"
								v-model="checkItem"
								@change="selectOne(index)"
							>
						</td>
						<td>{{item.shopName}}</td>
						<td>{{item.shopPrice}}</td>
						<td>
							<button class="btn " @click="reduce(index)">-</button>
							<input type="text" v-model="item.shopCount" />
							<button class="btn " @click="add(index)">+</button>
						</td>
						<td>
							{{item.shopPrice*item.shopCount}}
						</td>
						<td>
							<button class="btn btn-danger" @click="del(index)">删除</button>
						</td>
					</tr>
				
				</table>
				<p class="text-right">
					金额总计:{{sum}}
				</p>
				<p class="text-right">
					商品数量:{{count}}
				</p>
				<hr />
				<form>
					<div class="form-group">
						<input class="form-control" placeholder="商品名称" v-model="shopName" />
					</div>
					<div class="form-group">
						<input class="form-control" placeholder="商品价格" v-model = "shopPrice"/>
					</div>
					<div class="form-group">
						<button class="btn btn-success" type="button" @click="addInfo">增加</button>
					</div>
				</form>
			</div>
		</div>
		<script>
			new Vue({
				el:"#app",
				data:{
					listInfo:[
						{id:1,shopName:"帽子",shopPrice:100,shopCount:1,isBuy:false},
						{id:2,shopName:"衣服",shopPrice:100,shopCount:1,isBuy:false},
						{id:3,shopName:"裤子",shopPrice:100,shopCount:1,isBuy:false},
						{id:4,shopName:"袜子",shopPrice:100,shopCount:1,isBuy:false},
						{id:5,shopName:"鞋子",shopPrice:100,shopCount:1,isBuy:false},
					],
					shopName:"",
					shopPrice:"",
					checkItem:[],
					checkAll:false,
					checkNum:0,
					totalPrice:0,
					aaArr:[],
				},
				methods:{
					add:function(index){
						this.listInfo[index].shopCount++
						 
					},
					reduce:function(index){
						if(this.listInfo[index].shopCount<=0){
							this.listInfo[index].shopCount = 0
						}else {
							this.listInfo[index].shopCount--
						}
						
					},
					addInfo:function(){
//						alert(1)
					
						var obj = {
							id:this.listInfo.length+1,
							shopName:this.shopName,
							shopPrice:this.shopPrice,
							shopCount:0,
							isBuy:false
						}
						console.log(obj)
						this.listInfo.push(obj)
						
					},
					del(index){
						if(confirm("确定完成了吗?")){
							this.listInfo.splice(index,1)
						}
					},
					selectOne(index,item){
						if(this.checkItem.length == this.listInfo.length){
							this.checkAll = true
						}else {
							this.checkAll = false
						}
						if(this.listInfo[index].isBuy){
							this.listInfo[index].isBuy = false
						}else{
							this.listInfo[index].isBuy = true
						}
						let isArr = []
						
						this.listInfo.map((item, index) => {
							if(item.isBuy){
								isArr.push(item)
							}
						})
						
						this.aaArr = []
						this.totalPrice = 0
						isArr.map(( item, index) => {
							let aa = item.id+'_'+ item.shopPrice
							this.aaArr.push(aa)
							this.totalPrice +=item.shopPrice
						})
						
                    },
					 // 全选反选
					selectAll(){
						this.checkItem = []
						this.aaArr = []
						this.totalPrice = 0
						if(!this.checkAll){
							for (var i=0;i<this.listInfo.length;i++) {
								let aa = this.listInfo[i].id+'_'+ this.listInfo[i].shopPrice
								this.totalPrice +=this.listInfo[i].shopPrice
								this.checkItem.push(this.listInfo[i].id)
								this.aaArr.push(aa)
								this.listInfo[i].isBuy = true
								this.checkAll = true
							}
						}else {
							this.checkItem = []
							this.checkAll = false
							for (var i=0;i<this.listInfo.length;i++) {
								this.listInfo[i].isBuy = false
							}
						}
						
					}
				},
				computed:{
					sum(){
						var total = 0
						for (var i=0;i<this.listInfo.length;i++) {
							if(this.listInfo[i].isBuy){
								total+=parseFloat(this.listInfo[i].shopPrice)*parseFloat(this.listInfo[i].shopCount)
							}
						}
						return total	
					},
					count:function(){
						var total = 0
						for (var i=0;i<this.listInfo.length;i++) {
							if(this.listInfo[i].isBuy){
								total+=parseInt(this.listInfo[i].shopCount)
							}
							
						}
						return total
					}
				}
			})
		</script>
	</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值