vue 循环遍历Json数据 填充Table表格 学习 例子 整理 记录 自学 前端

                                   vue 循环遍历Json数据 填充Table表格 学习 例子 整理 记录 自学 前端

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
</head>
<body>
	<div id="app">
		<table>
			<thead>
				 <tr>
					<th>名称</th>
					<th>价格</th>
					<th>数量</th>
					<th>总价</th>
				 </tr>
			 </thead>
			 <tbody>
				 <tr v-for="cm in package1">
					 <td v-text="cm.name"></td>
					 <td v-text="cm.price"></td>
					 <td v-text="cm.count"></td>
					 <td v-text="cm.price * cm.count"></td>
				 </tr>
				 <tr v-for="cm in package2">
					 <td v-text="cm.name"></td>
					 <td v-text="cm.price"></td>
					 <td v-text="cm.count"></td>
					 <td v-text="cm.price * cm.count"></td>
				 </tr>
			 </tbody>
		</table>
		总价:{{ prices }}
	</div>
	<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
	<script>
		var app = new Vue({
			el:'#app',
			data:{
				package1:[
					{
						name:'iphonex',
						price:2000,
						count:2
					},
					{
						name:'ipad',
						price:1800,
						count:1
					}
				],
				package2:[
					{
						name:'apple',
						price:3,
						count:5
					},
					{
						name:'banana',
						price:5,
						count:10
					}
				]
			},
			computed:{
				prices:function(){
					var prices = 0;
					for (var i = 0; i < this.package1.length; i++) {
						prices += this.package1[i].price * this.package1[i].count;
					}
					for (var i = 0; i < this.package2.length; i++) {
						prices += this.package2[i].price * this.package2[i].count;
					}
					return prices;
				}
			}
		})
	</script>
</body>
</html>

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值