Vue之简单的统计功能

                                    Vue之简单的统计功能

今天我们来看看vue怎么实现简单的统计价格功能。

效果图:

最后的价格自动根据选择的项目来计算最终价格。

1.首先绑定每个项目绑定class 如果选中则增加样式active,改变背景颜色 :class="{'active':item.active}"

2.写计算事件,过滤掉集合中未选中的项目,在循环集合计算总价格。

全部代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
    <title>Title</title>
</head>
<style type="text/css">
ul li{
	list-style: none;
	width: 200px;
	background-color: #e35885;
	margin-top: 5px;
	height: 40px;
	padding: 5px;
}
ul li strong{
	float: left;
	font-size: 20px;
}
ul li span{
	float: right;
	font-size: 20px;
	
}
ul li.active{
    background-color:#8ec16d;
}
</style>
<script src="vue.min.js"></script>
<body>
	<div id="test">
		<h1>Total</h1>
		<ul>
			<li v-for="item in phoneList" @click="item.active=!item.active" :class="{'active':item.active}"><strong>{{item.name}}</strong><span>{{item.price}}</span></li>
			
		</ul>
		<h1>FinalPrice:{{getTotal.totalPrice}}</h1>
	</div>
</body>
<script >
	new Vue({
		el : "#test",
		data : {
			phoneList:[
				{
					name:'apple',
					price : 5000,
					active :true
				},
				{
					name:'mi',
					price : 3000,
					active :false
				},
				{
					name:'sang',
					price : 4000,
					active :false
				},
				{
					name:'onePlus',
					price : 3500,
					active :false
				},
				{
					name:'oppo',
					price : 2000,
					active :false
				}
			]
		},
		computed:{
			getTotal :function(){
				var entry = this.phoneList.filter(function(val){return val.active})
				totalPrice = 0;
				for (var i = 0,len = entry.length; i < len; i++) {
					totalPrice +=entry[i].price;
				}
				return {totalPrice:totalPrice};
			}
		}
	})
</script>
</html>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值