vue做一个购物车

今天用vue做一个购物车

话不多数直接上代码,

注:这里我用了bootstrap的样式

html

<div id="app" v-cloak>
			<div class="container" >
				<table class="table table-bordered table-hover"  v-if="listInfo.length">
					<tr>
						<td>
							<input type="checkbox" v-model="isCheck" @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" 
								v-model="checkItem" 
								:value="item.id" 
								@change="selectOne(index)"
								/>
						</td>
						<td>{{item.shopName}}</td>
						<td>{{item.shopPrice}}</td>
						<td>
							<button class="btn btn-default" @click="reduce(index)">-</button>
							<input type="text" v-model="item.shopCount" />
							<button class="btn btn-default" @click="add(index)">+</button>
						</td>
						<td>
							{{item.shopPrice*item.shopCount}}
						</td>
						<td>
							<button class="btn btn-danger" @click="handleRemove(index)">删除</button>
						</td>
					</tr>
				
				</table>
				
				<div v-else class="alert alert-danger alert-dismissable" >
				
				<strong>重要提示!</strong>您的购物空空如也!
				</div>
				<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-primary" type="button" @click="addInfo">增加</button>
					</div>
				</form>
			</div>
		</div>

javascript

new Vue({
				el:"#app",
				data:{
					listInfo:[
						{id:1,shopName:"男装",shopPrice:300,shopCount:1,isBuy:false},
						{id:2,shopName:"女装",shopPrice:500,shopCount:1,isBuy:false},
						{id:3,shopName:"手机",shopPrice:9810,shopCount:1,isBuy:false},
						{id:4,shopName:"电脑",shopPrice:14000,shopCount:1,isBuy:false},
						{id:5,shopName:"鞋子",shopPrice:1500,shopCount:1,isBuy:false},
					],
					shopName:"",
					shopPrice:"",
					isCheck:false,
					checkItem:[]
				},
				methods:{
					add:function(index){
						this.listInfo[index].shopCount++
						 
					},
					reduce:function(index){
						if(this.listInfo[index].shopCount<=1){
							this.listInfo[index].shopCount = 1
						}else {
							this.listInfo[index].shopCount--
						}
						
					},
					handleRemove: function(index) {
							this.listInfo.splice(index, 1);
						},
					addInfo:function(){
						var obj = {
							id:this.listInfo.length+1,
							shopName:this.shopName,
							shopPrice:this.shopPrice,
							shopCount:1,
							isBuy:false
						}
						console.log(obj)
						this.listInfo.push(obj)
						
					},
					selectAll:function(){
						this.checkItem = []
						if(!this.isCheck){
//							alert(1)
							for(var i=0;i<this.listInfo.length;i++){
								this.listInfo[i].isBuy = true
								this.checkItem.push(this.listInfo[i].id)
							}
							this.isCheck = true
						}else {
//							alert(2)
							for(var i=0;i<this.listInfo.length;i++){
								this.listInfo[i].isBuy = false
							}
							this.isCheck = false
							this.checkItem = []
						}
					},
					selectOne(index){
						if(this.checkItem.length == this.listInfo.length){
							this.isCheck = true
						}else {
							this.isCheck = false
						}
						if(this.listInfo[index].isBuy){
							this.listInfo[index].isBuy = false
						}else {
							this.listInfo[index].isBuy = true
						}
					}
				},
				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
					}
				}
			})

可以说把vue的文档看熟后,要写的逻辑比直接写要简单快捷很多很多

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值