欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vue做一个购物车

程序员文章站 2022-03-02 09:38:42
...

今天用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的文档看熟后,要写的逻辑比直接写要简单快捷很多很多

相关标签: vue