Vue.js实战第五章练习2答案

本来看网上有实现方法,不怎么想写的,但网上的答案都有点问题,题目的意思是使用二维数组实现,但网上的答案实际上还是使用对象的数组来实现,如https://blog.csdn.net/Clara_G/article/details/89840364
使用的是类似这样的形式[{name:“a”,{},{}},…],其实不必,可以使用二维数组,形式是这样的[[{},{}],…],这样才是使用二维数组实现。代码如下。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style type="text/css">
		[v-cloak]{
			display: none;
		}
		table{
			border: 1px solid #e9e9e9;
			border-collapse: collapse;
			border-spacing: 0;
			empty-cells: show;
		}
		th,td{
			padding: 8px 16px;
			border: 1px solid #e9e9e9;
			text-align: left;
		}
		th{
			background: #f7f7f7;
			color: #5c6b77;
			font-weight: 600;
			white-space: nowrap;
		}
	</style>
</head>
<body>
	<div id="app" v-cloak>
		<template v-if="list.length">
			<table v-for="(tableItem,tableIndex) in list">
				<thead>
					<tr>
						<th>{{goods[tableIndex]}}</th>
						<th>商品名称</th>
						<th>商品单价</th>
						<th>购买数量</th>
						<th>操作</th>
						<th><button
							@click="checkAll(tableIndex)">全选</button></th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="(item,index) in list[tableIndex]">
						<td>{{index+1}}</td>
						<td>{{item.name}}</td>
						<td>{{item.price}}</td>
						<td>
							<button
							@click="handleReduce(tableIndex,index)"
							:disabled="item.count===1">-</button>
							{{item.count}}
							<button @click="handleAdd(tableIndex,index)">+</button>
						</td>
						<td>
							<button @click="handleRemove(tableIndex,index)">移除</button>
						</td>
						<td>
							<input type="checkbox"
							:checked="item.checked===true"
							@click="changeCheck(tableIndex,index)"/>
						</td>
					</tr>
				</tbody>
			</table>
		<div>总价:¥{{totalPrice}}</div>
		</template>
		<div v-else>购物车为空</div>
	</div>
	<script src="vue.js"></script>
	<script>
		var app=new Vue({
			el:'#app',
			data:{
				goods:["电子产品","生活用品","果蔬"],
				flag:[0,0,0],
				list:[
				[
				{
					id:1,
					name:'iPhone7',
					price:6188,
					count:1,
					checked:''
				},
				{
					id:2,
					name:'iPad Pro',
					price:5888,
					count:1,
					checked:''
				},
				{
					id:3,
					name:'MacBook Pro',
					price:21488,
					count:1,
					checked:''
				}
				],
				[
				{
					id:1,
					name:'脸盆',
					price:20,
					count:1,
					checked:''
				},
				{
					id:2,
					name:'牙膏',
					price:5,
					count:1,
					checked:''
				},
				{
					id:3,
					name:'牙杯',
					price:10,
					count:1,
					checked:''
				}
				],
				[
				{
					id:1,
					name:'苹果',
					price:5,
					count:1,
					checked:''
				},
				{
					id:2,
					name:'荔枝',
					price:7,
					count:1,
					checked:''
				},
				{
					id:3,
					name:'橘子',
					price:10,
					count:1,
					checked:''
				}
				]
				]
			},
			computed:{
				totalPrice:function(){
					var total=0;
					for(var i=0;i<this.list.length;i++){
						for(var j=0;j<this.list[i].length;j++){
							let item=this.list[i][j];
							if(item.checked===true){
							total+=item.price*item.count;
							}
						}
					}
					return total.toString().replace(/\B(?=(\d{3})+$)/g,',');
				}
			},
			methods:{
				handleReduce:function(tableIndex,index){
					if(this.list[tableIndex][index].count===1) return;
					this.list[tableIndex][index].count--;
				},
				handleAdd:function(tableIndex,index){
					this.list[tableIndex][index].count++;
				},
				handleRemove:function(tableIndex,index){
					this.list[tableIndex].splice(index,1);
				},
				changeCheck:function(tableIndex,index){
					this.list[tableIndex][index].checked=this.list[tableIndex][index].checked===''?true:'';
					/*Vue.set(list[tableIndex][index],checke,true);*/
				},
				checkAll:function(tableIndex){
					if(this.flag[tableIndex]===0){
						for(let i=0;i<this.list[tableIndex].length;i++){
						this.list[tableIndex][i].checked=true;
						}
						this.flag[tableIndex]=1;
					}else if(this.flag[tableIndex]===1){
						for(let i=0;i<this.list[tableIndex].length;i++){
						this.list[tableIndex][i].checked='';
						}
						this.flag[tableIndex]=0;
					}
					
				}
			}
		});
	</script>
</body>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

returnadsss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值