vue购物车和地址选配(三)
- 参考资料:vue.js官网
- 项目演示:

- 项目源代码:
- 核心代码及踩坑
删除:
new Vue({
el:'#app',
data:{
productlist:[],
totalMoney:0,
checkAllFrag:false,//默认没有全选
deFlag:false,
//当前的存起来
curProduct:''
},
//必须加mounted函数,这是页面初加载,如果不写这个函数,network中将请求不到数据
mounted:function(){
this.cartView();
},
//局部过滤器
filters:{
formatMoney:function(value){
return "$" + value.toFixed(2);
}
},
methods:{
cartView:function(){
var _this=this; //要保存这个this,
this.$http.get('data/cartData.json',{'id':'123'}).then(function(res){
_this.productlist=res.data.result.list; //这里的this已经不是实例对象了
});
},
changeMoney:function(product,way){
if(way>=1){
product.productQuantity++;
}else{
product.productQuantity--;
if(product.productQuantity<1){
product.productQuantity=1;
}
}
this.calcTotalPrice();
},
selectedProduct:function(item){
//每次选中的时候先判断当前这个item.checked属性是否存在
if(typeof item.check=="undefined"){
this.$set(item,'check',true);//如果不存在就先给他设置一个
}else{
item.check=!item.check;
}
this.calcTotalPrice();
},
//
checkAll:function(flag){
this.checkAllFrag=flag; //将前面传来的标志记录下来,全选=true,不全选=false
var _this=this;
_this.productlist.forEach(function(item,index){ //就把上面的单选按钮的check都设置为true
if(typeof item.check =="undefined"){
_this.$set(item,'check',_this.checkAllFrag); //把单选按钮设置成和全选时=true,不全选时=false
}else{
item.check=_this.checkAllFrag;
}
});
this.calcTotalPrice();
},
calcTotalPrice:function(){
var _this=this;
_this.totalMoney=0;//每次计算都要清零处理
_this.productlist.forEach(function(item,index){
if(item.check){//如果被选中,就计算总金额,并且把每一项累加
_this.totalMoney+=item.productPrice*item.productQuantity;
}
});
},
delProduct:function(){
//
// this.productlist.indexOf(this.curProduct);
this.productlist.splice(this.index,1);
this.deFlag=false;
}
}
});
Vue.filter('money',function(value,type){
return '$' + value.toFixed(2)+type;
});
删除的实例.js
点击删除按钮要弹出那个确定框:
第一步:同样用v-bind绑定一个属性,当点击删除按钮时,deFlag=true,弹出确定框
//这是显示那个确定框的
<div class="md-modal modal-msg md-modal-transition" id="showModal" v-bind:class="{'md-show':deFlag}"> //这是删除按钮
<a href="javascript:void 0" class="item-edit-btn" @click="deFlag=true"> //在实例data中,定义了deFlag=false,当点击按钮时,deFlag=true框显示
第二步:定义和调用删除函数
//yes和no调用的是同一个函数,当传入的参数为1的时候删除,为0的时候不删除 <button class="btn btn--m" id="btnModalConfirm" @click="delProduct(1)">Yes</button>
<button class="btn btn--m btn--red" id="btnModalCancel" @click="delProduct(0)">No</button>
</div>
delProduct:function(type){
this.productlist.splice(this.index,type);
this.deFlag=false;
}
踩坑:踩坑一:在vue1.0中可以用$delete函数来直接删除
采坑二:确定框的显示,用上面的方式
地址选配
地址过滤方式渲染数据,让购物车地址默认只显示三条
new Vue({
el:'.container',
data:{
addresslist:[],
limitNum:3
},
filters:function(){
},
mounted:function(){
this.$nextTick(function(){
this.getaddresslist();
});
},
computed:{
filterAddress: function(){
return this.addresslist.slice(0,3);
}
},
methods:{
getaddresslist:function(){
var _this=this
this.$http.get('data/address.json',{'id':123}).then(function(res){
_this.addresslist=res.data.result;
});
}
}
});
默认只显示三条.js
也是用v-for渲染数据,但是与之前的有区别
//filterAddress 是有computed重新过滤的数组
<li v-for="(item,index) of filterAddress" >
<dl>
<dt>{{item.userName}}</dt>
<dd class="address">{{item.streetName}}</dd>
<dd class="tel">{{item.tel}}</dd>
</dl>
</li>
computed函数过滤
computed:{
filterAddress: function(){
//只返回数组从0-3的数据
return this.addresslist.slice(0,3);
}
},
展示更多:直接在默认显示三条的基础上将limitNum,通过loadmore函数改变
<div class="shipping-addr-more">
<a class="addr-more-btn up-down-btn" href="javascript:" @click="loadmore()">
more
<i class="i-up-down">
<i class="i-up-down-l"></i>
<i class="i-up-down-r"></i>
</i>
loadmore函数:
loadmore:function(){
return this.limitNum=this.addresslist.length;
}
设为默认地址:“设为默认地址”和“默认地址”之间是一对互斥事件,isDefault是存在数据中的数据项,
item.isDefault=false,就显示
<div class="addr-opration addr-set-default" v-if="item.isDefault">
<a href="javascript:;" class="addr-set-default-btn" @click="SetDefault(item.addressId)"><i>设为默认</i></a>
</div>
<div class="addr-opration addr-default" v-if="!item.isDefault">默认地址</div>
通过点击SetDefault()函数来实现
SetDefault:function(addressId){
//遍历
this.addresslist.forEach(function(obj,index){
//当前遍历的id是否等于我们点击的id,如果相等,就设为默认
if(obj.addressId==addressId){
obj.isDefault=true;
}else{
obj.isDefault=false;
}
});
}
配送方式的选择,也是两个互斥事件
<li v-bind:class="{'check':shippingmethod==1}" @click="shippingmethod=1">
<div class="name" >标准配送</div>
<div class="price">Free</div>
</li>
<li v-bind:class="{'check':shippingmethod==2}" @click="shippingmethod=2">
<div class="name" >高级配送</div>
<div class="price">180</div>
</li>
vue购物车和地址选配(三)的更多相关文章
- 关于慕课网《使用vue2.0实现购物车和地址选配功能》的总结
视频学习网址:http://www.imooc.com/learn/796 源码打包:https://codeload.github.com/fachaoshao/Vue-ShoppingCart/z ...
- vue实现购物车和地址选配
参考文献 vue.js官网 项目演示:数据渲染,格式化数据,点击加,减号自动加减 项目准备 1. 项目css和js文件 https://github.com/4561231/hello ...
- vue实现购物车和地址选配(二)
参考文献: vue官网: vue.js 效果展示:全选和取消全选,计算总金额 项目源代码:https://github.com/4561231/hello_world 项目核心代码实现及踩坑 1.全选 ...
- VUE2.0实现购物车和地址选配功能学习第六节
第六节 地址列表过滤和展开所有的地址 html:<li v-for="(item,index) in filterAddress">js: new Vue({ el:' ...
- VUE2.0实现购物车和地址选配功能学习第二节
第二节 创建VUE实例 购物车项目计划: 1.创建一个vue实例 2.通过v-for指令渲染产品数据 3.使用filter对金额和图片进行格式化 4.使用v-on实现产品金额动态计算 5.综合演示 ① ...
- VUE2.0实现购物车和地址选配功能学习第七节
第七节 卡片选中,设置默认 1.卡片选中html:<li v-for="(item,index) in filterAddress" v-bind:class="{ ...
- VUE2.0实现购物车和地址选配功能学习第五节
第五节 单件商品金额计算和单选全选功能 1.vue精髓在于操作data模型来改变dom,渲染页面,而不是直接去改变dom 2.加减改变总金额功能: html:<div class="c ...
- VUE2.0实现购物车和地址选配功能学习第四节
第四节 v-on实现金额动态计算 用¥金额 进行格式处理,可以使用原生js进行转换,但是在vuei,使用filter过滤器更加方便 注: 1.es6语法=>和import等 好处在于res参数后 ...
- VUE2.0实现购物车和地址选配功能学习第三节
第三节 使用v-for渲染商品列表 1.使用vue-resource插件引入json数据 (注:在谷歌中调试打断点-- ,console还可以输出vm,res等属性列表,或者productList等一 ...
随机推荐
- powerdesigner 16.5 不允许有扩展属性,或对象不存在
创建完之后这边会出现 选择刚创建的用户 这样就可以了
- How to RAMDISK on macOS
diskutil erasevolume HFS+ 'RAM Disk' `hdiutil attach -nomount ram://8388608`
- github上传时出现error: src refspec master does not match any解决办法22
1 error:src refspec master does not match any这个问题,我之前也遇到过,这次又遇到了只是时间间隔比较长了,为了防止以后再遇到类似问题,还是把这个方法简单记录 ...
- DRF 解析器和渲染器
一,DRF 解析器 根据请求头 content-type 选择对应的解析器就请求体内容进行处理. 1. 仅处理请求头content-type为application/json的请求体 from dja ...
- Elasticsearch 聚合统计与SQL聚合统计语法对比(一)
Es相比关系型数据库在数据检索方面有着极大的优势,在处理亿级数据时,可谓是毫秒级响应,我们在使用Es时不仅仅进行简单的查询,有时候会做一些数据统计与分析,如果你以前是使用的关系型数据库,那么Es的数据 ...
- subprocess 模块
import subprocess # 就用来执行系统命令 import os cmd = r'dir D:\上海python全栈4期\day23 | findstr "py"' ...
- Java实现月份递减
问题:从当前月份开始,往前3年的所有月份 返回map类型,key是String,value是Date,map倒序排列 public static Map<String, Date> get ...
- gogs : 添加 ssh An error has occurred : addKey: fail to parse public key: exec: "ssh-keygen": executable file not found in %PATH% - exec: "ssh-keygen": executable file not found in %PATH%
服务器上缺少配置 ssh-keygen.exe的 环境变量.git的环境变量 在path 环境变量加上.重启gogs服务
- 字符串数据结构模板/题单(后缀数组,后缀自动机,LCP,后缀平衡树,回文自动机)
模板 后缀数组 #include<bits/stdc++.h> #define R register int using namespace std; const int N=1e6+9; ...
- JLOI2015 DAY2 简要题解
「JLOI2015」骗我呢 题意 问有多少个 \(n \times m\) 的矩阵 \(\{x_{i, j}\}\) 满足 对于 \(\forall i \in [1, n], j \in [1, m ...