Vue购物车

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
		<style type="text/css">
			input[type="text"] {
				border: none;
				text-align: center;
				outline: none;
				width: 30px;
			}

			.jiajian {
				border: 1px solid #333;
				border-radius: 10px;
				width: 20px;
				height: 20px;
				text-align: center;
				line-height: 20px;
				cursor: pointer;
				color: #333333;
			}

			.xuanzhong {
				color: darkorange;
			}

			img {
				width: 100px;
				height: 100px;
			}

			tr>th,
			tr>td {
				width: 130px;
				text-align: center;
			}
		</style>
	</head>
	<body>
		<div id="app">
			<table border="0" cellspacing="" cellpadding="">
				<tr style="display: flex;align-items: center;">
					<th >
						
						<span @click="allselect">全选</span>
						/
						<span @click="allcancel">全不选</span>
					</th>
					<th>商品名</th>
					<th>商品图片</th>
					<th>单价</th>
					<th>数量</th>
				</tr>
				<tr v-for="(item,index) in arr" :class="{xuanzhong:item.checked}" style="display: flex;align-items: center;">
					<td>
						<input type="checkbox" v-model="item.checked" />
					</td>
					<td>{{item.name}}</td>
					<td>
						<img v-bind:src="item.imgUrl">
					</td>
					<td>{{item.price}}</td>
					<td style="display: flex;justify-content: center;color: #333333;">
						<span class="jiajian" @click="if(--item.count == 0) item.count=1">-</span>
						<span style="margin: 0 10px;width: 20px;">{{item.count}}</span>
						<span class="jiajian" @click="item.count++">+</span>
					</td>
				</tr>
			</table>
			总价:<span>{{total.toFixed(2)}}</span>
			<br> <button type="button" @click="del">删除所选内容</button>
		</div>
	</body>
	<script type="text/javascript">
		var app = new Vue({
			el: '#app',
			computed: {
				total() {
					let sum = 0;
					this.arr.map((item, index) => {
						if (item.checked) sum += item.price * item.count
					})
					return sum
				}
			},
			data: {
				arr: [{
					name: '手机',
					imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/d880ff45a9a3b70527770e01521fc939.jpg?thumb=1&w=400&h=400&f=webp&q=90',
					price: 10.1,
					count: 1,
					checked: true
				}, {
					name: '电视',
					imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/ef4c68fed730ec26bf2fa0ff620975c5.jpg?thumb=1&w=400&h=400&f=webp&q=90',
					price: 110.1,
					count: 1,
					checked: true
				}, {
					name: '洗衣机',
					imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/ec20453216dcd42f982cffe5fdbc3115.jpg?thumb=1&w=400&h=400&f=webp&q=90',
					price: 210.1,
					count: 1,
					checked: false
				}, {
					name: '耳机',
					imgUrl: 'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/50649d7b5706f4cd9f658e93db6b6564.jpg?thumb=1&w=400&h=400&f=webp&q=90',
					price: 20.1,
					count: 1,
					checked: false
				}]
			},
			methods: {
				handleChange(value) {
					console.log(value);
				},
				del() {
					var xuanzhong = false;
					this.arr.map((item, index) => {
						if (item.checked) {
							xuanzhong = true;
						}
					})
					if (!xuanzhong) {
						alert('请打勾')
						return;
					}
					this.arr = this.arr.filter(item => !item.checked)
					if (this.arr.length == 0) {
						window.location.href = 'none.html'
					}
				},
				allselect() {
					this.arr.map(item => item.checked = true)
				},allcancel(){
					this.arr.map(item => item.checked = false)
				}
			}
		});
	</script>
</html>

上面我引用的Vue是本地的,大家记得去官网下载保存哦

这里是当你点击添加购物车时,会显示空的错误信息

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<img src="https://cdn.cnbj1.fds.api.mi-img.com/staticsfile/cart/cart-empty.png" >
		您的购物车还是空的!请先去添加购物车!!!
	</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悄悄顽皮的猴头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值