天天看点

Vue自适应完美购物车

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>购物车</title>
		<link rel="stylesheet" type="text/css" href="css/bootstrap.css" target="_blank" rel="external nofollow" />
		<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/bootstrap.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/vue-2.6.9.min.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			* {
				margin: 0;
				padding: 0;
			}

			[v-cloak] {
				display: none;
			}

			.demo {
				/* margin: 50px 300px; */
			}

			table {
				width: 800px;
				border-collapse: collapse;
				text-align: center;
			}

			 table th {
				height: 30px;
				text-align: center;
				line-height: 30px!important;
			}

			table td {
				height: 25px;
				line-height: 25px!important;
			}
 
			table td:nth-of-type(2) {
				color: #008080;
			}

			table td:nth-of-type(3) {
				color: red;
			}

			table .zong td {
				text-align: right;
				padding-right: 10px;
				color: red;
			}

			.saveGoodsArea {
				width: 100%;
				/* height: 400px; */
				list-style: none;
				margin: 30px auto;
				display: flex;
				justify-content: space-around;
			}
			
			.saveGoodsArea li {
				width: 23%;
				/* height: 100%; */
				box-shadow: 0 0 3px rgba(0, 0, 0, .6);
			}

			.saveGoodsArea li img {
				width: 100%;
				display: block;
			}

			.saveGoodsArea li p {
				width: 100%;
				height: 30px;
				line-height: 30px;
				text-align: center;
			}

			.saveGoodsArea li button {
				width: 100%;
				height: 30px;
				outline: none;
				border: none;
			}

			.saveGoodsArea li:hover button {
				background-color: orange;
				cursor: pointer;
			}

			.shangpin {
				margin-right: 20px;
				float: right;
				margin-top: 15px;
				box-sizing: border-box;
			} 
			button{
				outline: none!important;
			}
			.form-control{
				width: 200px;
				display: inline-block!important;
			}
			table tr:nth-of-type(1){
				box-sizing: border-box!important;
				padding-top: 50%!important;
				}
				.btn-danger{
					margin-top: 15px;
				}
		</style>
	</head>
	<body>
		<div class="demo" v-cloak>
			<!-- 库存 -->
			<div class="container">
				<ul class="saveGoodsArea col-lg-3 col-md-3 col-sm-3 col-xs-3">
					<li v-for="saveGood of saveGoods">
						<img :src="saveGood.imgSrc" class="img-responsive">
						<p>{{saveGood.name}}&nbsp;&nbsp;&nbsp;{{saveGood.price}}</p>
						<button @click="addGood(saveGood)">加入购物车</button>
					</li>
				</ul>
			</div>
			<!-- 购物车 -->
			<!-- <button @click="paixu" class="btn btn-xs  alert-info" :title="titleMoney">排序</button> -->
			<!-- <button @click="shaixuan" class="butt" :title="titleChoice">筛选</button> -->
			<div class="table-responsive container" style="border:none;">
				<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
					<table class="table table-responsive
					table-hover table-bordered 
					text-center table-condensed">
								<!-- table-hover鼠标悬浮高亮 -->
								<!-- table-bordered 带边框 -->
								<!-- table-condensed 紧凑型表格  内边距从8调到5 -->
								<!-- table-responsive 响应式表格 -->
								<!-- .table-striped表格-斑马线表格 -->
								<caption>购物车<button @click="paixu" class="btn btn-xs  alert-info" :title="titleMoney">排序</button></caption>
								<tbody>
									<tr>
										<td>
											<button @click="deletaAll" class="btn btn-danger btn-sm">批量删除</button>
										</td>
										<td colspan="5">
											<p class="shangpin form-group">
												筛选:<input 
												type="text" 
												:placeholder="placeholderValue" 
												v-model="filterText" 
												@keyup.enter="filterFn" 
												class="form-control input-sm"/>
											</p>
										</td>
									</tr>
									<tr>
										<td>
											全选:<input type="checkbox" v-model="allStatus" @click="changeAllStatus">
										</td>
										<th>商品名称</th>
										<th>商品单价</th>
										<th>商品数量</th>
										<th>商品总价</th>
										<th>操作</th>
									</tr>
									<tr v-for="good of goods">
										<td class="chack">
											<input 
												type="checkbox" 
												@click="checkStatus(good)"
												v-model="good.status" 
												:value="good.name">
										</td>
										<td>{{good.name}}</td>
										<td>{{good.price}}</td>
										<td>
											<button @click="cut(good)" class="danjia btn btn-warning btn-xs">-</button>
											{{good.numder}}
											<button @click="add(good)" class="danjia btn btn-warning btn-xs">+</button>
										</td>
										<td>{{good.price*good.numder}}</td>
										<td><button @click="yichu(good)" class="btn btn-warning btn-xs" :title="titleValue">{{yichuValue}}</button></td>
									</tr>
									<tr class="zong">
										<td class="sumPrice" colspan="6"><strong>总价</strong>:{{sumMoney}}</td>
									</tr>
								</tbody>
							</table>
				</div>
			</div>
		</div>
	</body>
	<script type="text/javascript">
		/*创建根实例*/
		var demo = new Vue({
			/*挂载根实例到元素上*/
			el: '.demo',
			/* 绑定数据 */
			data: {
				goods: [
					{name: '三星',price: 4000,numder: 1,status:false},
					{name: 'vivo',price: 3300,numder: 1,status:false},
					{name: 'oppo',price: 2800,numder: 1,status:false},
					{name: '华为',price: 7000,numder: 1,status:false},
					{name: '小米',price: 2000,numder: 1,status:false},
					{name: '诺基亚',price: 300,numder: 1,status:false}
					],
				/*库存*/
				saveGoods: [
					{name: '健身一',price: 1500,imgSrc: 'img/01.jpg'},
					{name: '健身二',price: 2060,imgSrc: 'img/02.jpg'},
					{name: '健身三',price: 3080,imgSrc: 'img/03.jpg'},
					{name: '健身四',price: 900,imgSrc: 'img/04.jpg'}
				],
				titleValue: '点击删除商品',
				titleMoney: '点击按价钱排序',
				titleChoice: '点击筛选价格',
				yichuValue: '移除',
				placeholderValue: '请输入要搜索的商品',
				filterText: '',
				selectAllList:[],
				allStatus:false
			},
			/* 方法 */
			methods: {
				/* 对商品的价格从低到高排序 */
				paixu: function() {
					this.goods = this.goods.sort(function(x, y) {
						return x.price - y.price
					})
				},
				/*
				只要大于2000的商品
				 shaixuan: function() {
					this.goods = this.goods.filter(function(element) {
						if (element.price > 2000) {
							return element
						}
					})
				}, */
				yichu: function(good) {
					/*删除商品*/
					var confirmFn = confirm('您确定删除该商品吗?')
					if (confirmFn) {
						/* 获取商品索引 */
						var index = this.goods.indexOf(good)
						this.goods.splice(index, 1)
					} else {
						alert('好的,您在看看')
					}
				},
				cut: function(good) {
					/* 减少商品数量 */
					if (good.numder <= 1) {
						alert('单个商品最少一个')
					} else {
						return good.numder -= 1
					}
				},
				add: function(good) {
					/*增加商品数量*/
					return good.numder += 1
				},
				/*增加购物车*/
				addGood(saveGood) {
					/*1、创建名称新数组用于筛选遍历*/
					var searchArray = [];
					this.goods.map(function(item, index, self) {
						searchArray.push(item.name)
					})
					/*2、遍历判断,购物车是否包含当前商品*/
					var searchIndex = searchArray.indexOf(saveGood.name);
					/*3、分类判断*/
					if (searchIndex == -1) {
						/*原来没有*/
						var newGood = {
							name: saveGood.name,
							price: saveGood.price,
							numder: 1
						}
						this.goods.push(newGood)
					} else {
						/*原来有*/
						this.goods[searchIndex].numder += 1;
					}

				},
				/*筛选购物车*/
				filterFn() {
					/*1、创建名称新数组用于筛选遍历*/
					var filterArray = [];
					this.goods.map(function(item, index, self) {
						filterArray.push(item.name)
					})
					/*2、输入值索引判断,判断是否在当前数组里*/
					var filterIndex = filterArray.indexOf(this.filterText);
					/*3、分类判断*/
					if (filterIndex == -1) {
						alert('抱歉,暂无此商品')
						this.filterText = '';
					} else {
						this.goods = this.goods.filter(function(item, index, self) {
							return filterIndex == index;
						})
						this.filterText = '';
					}
				},
				/*全选与全不选*/
				changeAllStatus(){
					var _this = this;
					if(!_this.allStatus){
						/*全选*/
						for(var i=0;i<_this.goods.length;i++){
							_this.goods[i].status = true;
						}
					}else{
						/*全不选*/
						for(var i=0;i<_this.goods.length;i++){
							_this.goods[i].status = false;
						}
					}
				},
				/*跟踪定位单个选项*/
				checkStatus(good){
					/*1、当前按钮状态*/
					if(!good.status){
						good.status = true;
					}else{
						good.status = false;
					}
					/* 2、every全真即真、some有真即真 */
					var _this = this;
					var result = _this.goods.every(function(item,index,self){
						return item.status;
					})
					if(!result){
						_this.allStatus = false;
					}else{
					_this.allStatus = true;
					}					
				},
				/*批量删除*/
				deletaAll(){
					var _this = this;
					_this.goods = _this.goods.filter(function(item,index,self){
						return item.status == false;
					})
					if(_this.allStatus){
						_this.allStatus = false;
					}
				}
			},
			/* 计算属性 */
			computed: {
				/*商品总价*/
				sumMoney() {
					var sum = 0;
					this.goods.forEach(function(item, index, self) {
						sum += item.price * item.numder;
					})
					return sum;
				}
			}

		})
	</script>
</html>