1、预览

2、index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"> <meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0">
<title>Title</title>
<link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id="app">
<!--<h2>{{title}}</h2>-->
<li v-for="(item,index) in productList">
<div >产品名称:{{item.productName}}</div>
<dd v-for="part in item.parts" v-text="part.partsName"></dd>
<div>价格:{{item.productPrice+"--------------------"+index}}</div>
<div>数量:{{item.productQuentity}}</div>
<div>金额:{{item.productPrice*item.productQuentity | formatMoney}}</div>
<div>金额:{{item.productPrice*item.productQuentity | money("元")}}</div>
<!--<img src="item.productImage" alt="">-->
<!--<img src="{{item.productImage}}" alt="">-->
<img v-bind:src="item.productImage" alt=""> <a href="javascript:;" v-on:click="changeMoney(item,-1)">-</a>
<a href="javascript:;" @click="changeMoney(item,1)">+</a>
<input type="text" value="0" disabled v-model="item.productQuentity">
<!-- v-bind:class="{'check':item.checked}"这里的check用的是单引号-->
<a href="javascript:;" class="item-check-btn" v-bind:class="{'check':item.checked}" @click="selectedProduct(item);">
</a>
</li>
<div>
<!--<span class="item-check-btn" :class="{'check':checkAllFlag}" @click="checkAllFlag=true"></span>-->
<!--注意不要将true写成ture,还要将div写到vue的作用范围内的div中,即#app这个div中-->
<span class="item-check-btn" :class="{'check':checkAllFlag}" @click="checkAll(true);"></span>
<a href="" class="item-del-btn" @click="checkAll(false);">取消全选</a>
<div class="item-total">
总金额 <span class="total-price">{{totalCheckMoney| money("元")}}</span>
</div>
</div>
</div> <script src="js/lib/vue.min.js"></script>
<script src="js/lib/vue-resource.min.js"></script>
<script src="js/cart.js"></script>
</body>
</html>

3、cart.js

/**
* Created by kk on 2017/4/16.
*/
new Vue({
el:"#app",
data:{
// title:"hello vue"
totalMoney:0,
productList:[],
checkAllFlag:false,
totalCheckMoney:0
},
filters:{
formatMoney:function (value) {
return "¥"+value.toFixed(2)
}
},
mounted:function () {
//类似于jquery中的ready方法
this.$nextTick(function () {
this.cartView();
}) },
methods:{
cartView:function () {
// this.title="Vue hello"
//var _this=this;
// this.$http.get("data/cart.json",{"id":123}).then(function (res) {
// _this.productList=res.body.result. productList;
// _this.totalMoney=res.body.result.totalMoney;
// });
// ES6语法
let _this=this;
this.$http.get("data/cart.json",{"id":123}).then(res=> {
this.productList=res.body.result. productList;
this.totalMoney=res.body.result.totalMoney;
});
},
changeMoney:function (product,way) {
if(way>0)
{
product.productQuentity++;
}
else{
product.productQuentity--;
if(product.productQuentity<1){
product.productQuentity=1;
}
}
this.calcTotalPrice();
},
selectedProduct:function (item) {
//alert("1");
if(typeof item.checked=="undefined"){
//Vue.set(item,"checked",true);//全局注册checked
this.$set(item,"checked",true);//局部注册checked
}
else {
item.checked=!item.checked;
}
this.calcTotalPrice();
},
checkAll:function (flag) {
this.checkAllFlag=flag;
var _this=this;
this.productList.forEach(function (item,index) {
if(typeof item.checked=="undefined"){
_this.$set(item,"checked",_this.checkAllFlag);
}else {
item.checked=_this.checkAllFlag;
}
});
this.calcTotalPrice();
},
calcTotalPrice:function () {
var _this=this;
this.totalCheckMoney=0;
this.productList.forEach(function (item, index) {
if(item.checked){
_this.totalCheckMoney+=item.productPrice*item.productQuentity;
}
})
}
} });
Vue.filter("money",function (value,type) {
return "¥"+value.toFixed(2)+type;
});

vue使用方法计算总金额的更多相关文章

  1. Vue的computed计算属性是如何实现的

    一个开始 有如下代码,full是一个计算属性,开始,他的值是'hello world',1s后,msg变成了‘I like’, full的值同步变成了'I like world';其原理解析来看一下. ...

  2. vue.nextTick()方法的使用详解

    什么是Vue.nextTick()??   定义:在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 所以就衍生出了这个获取更新后的DOM的Vue方法 ...

  3. Vue的computed(计算属性)使用实例之TodoList

    最近倒腾了一会vue,有点迷惑其中methods与computed这两个属性的区别,所以试着写了TodoList这个demo,(好土掩面逃~); 1. methods methods类似react中组 ...

  4. 使用并行的方法计算斐波那契数列 (Fibonacci)

    更新:我的同事Terry告诉我有一种矩阵运算的方式计算斐波那契数列,更适于并行.他还提供了利用TBB的parallel_reduce模板计算斐波那契数列的代码(在TBB示例代码的基础上修改得来,比原始 ...

  5. vue调试方法

    vue调试方法有如下三种 1.chrome谷歌插件vue-devtools 2.console.log().console.error().alert().debugger 3.设置全局变量,分为两种 ...

  6. 面试题:两种方法计算n!

    直接上代码package com.face.test; public class Test { /** * 面试题:递归方法计算n! */ @org.junit.Test public void di ...

  7. 创建一个接口Shape,其中有抽象方法area,类Circle 、Rectangle实现area方法计算其面积并返回。又有Star实现Shape的area方法,其返回值是0,Star类另有一返回值boolean型方法isStar;在main方法里创建一个Vector,根据随机数的不同向其中加入Shape的不同子类对象(如是1,生成Circle对象;如是2,生成Rectangle对象;如是3,生成S

    题目补充: 创建一个接口Shape,其中有抽象方法area,类Circle .Rectangle实现area方法计算其面积并返回. 又有Star实现Shape的area方法,其返回值是0,Star类另 ...

  8. Spark Mllib里决策树回归分析使用.rootMeanSquaredError方法计算出以RMSE来评估模型的准确率(图文详解)

    不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型 ...

  9. Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型的准确率(图文详解)

    不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的 ...

随机推荐

  1. Python - 常用更新命令以及常见库安装

    库的安装方式一般有两种: 一. pip直接安装(或使用豆瓣源) pip install scrapy pip install -i https://pypi.douban.com/simple/ sc ...

  2. 深入剖析ConcurrentHashMap

    原文是09年时写的,在公司的邮件列表发过,同事一粟 和清英 创建的并发编程网 对这方面概念和实战有更好的文章,贴出来仅供参考.pdf格式在:http://www.slideshare.net/hong ...

  3. Orleans逐步教程

    参考文档:https://dotnet.github.io/orleans/Tutorials/index.html 一.通过模板创建Orleans ①下载vs插件:https://marketpla ...

  4. 使用windows脚本移动文件

    1. 移动脚本 在部署web项目时,一般需要将打包的war包发布到Tomcat目录下,所以自己就在网上查找资料写了一个简略的移动文件的脚本,如下: @echo off echo "使用bat ...

  5. Mac电脑 阿里云ECS(ContentOS) Apache+vsftpd+nodejs+mongodb建站过程总结

    简介:我这里采用的阿里云免费提供的6个月ECS服务器:制作了一个简单的爬虫程序:里面很多功能还么做:搜索里面功能回去的数据未做处理会崩溃(大家不要点搜索功能):地址:http://loldragon. ...

  6. java显示树结构

    /** * 显示多颗树的所有节点的信息 * * @param departmentList */ private void showTreeList(Collection<Department& ...

  7. 并查集 (Union-Find Sets)及其应用

    定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的 ...

  8. json字符串转换对象的方法1

    为了方便读者了解json的使用,读者直接粘贴下面代码看效果即可: var json1 = {'name':'小李','age':'11','sex':'女'};console.log(json1.na ...

  9. [原创]推荐一些在线API生成工具

    [原创]推荐一些在线API生成工具 最近发现Api文档维护是个大体力活,版本控制文档统一化特别麻烦,寻思着这个怎么处理,经高人指点开源有一些工具不错,具体如下: 1.Swagger   http:// ...

  10. C# 8.0中的模式匹配

    C# 8.0中的模式匹配相对C# 7.0来说有了进一步的增强,对于如下类: class Point{    public int X { get; }    public int Y { get; } ...