• 参考资料: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购物车和地址选配(三)的更多相关文章

  1. 关于慕课网《使用vue2.0实现购物车和地址选配功能》的总结

    视频学习网址:http://www.imooc.com/learn/796 源码打包:https://codeload.github.com/fachaoshao/Vue-ShoppingCart/z ...

  2. vue实现购物车和地址选配

    参考文献        vue.js官网 项目演示:数据渲染,格式化数据,点击加,减号自动加减 项目准备 1. 项目css和js文件  https://github.com/4561231/hello ...

  3. vue实现购物车和地址选配(二)

    参考文献: vue官网: vue.js 效果展示:全选和取消全选,计算总金额 项目源代码:https://github.com/4561231/hello_world 项目核心代码实现及踩坑 1.全选 ...

  4. VUE2.0实现购物车和地址选配功能学习第六节

    第六节 地址列表过滤和展开所有的地址 html:<li v-for="(item,index) in filterAddress">js: new Vue({ el:' ...

  5. VUE2.0实现购物车和地址选配功能学习第二节

    第二节 创建VUE实例 购物车项目计划: 1.创建一个vue实例 2.通过v-for指令渲染产品数据 3.使用filter对金额和图片进行格式化 4.使用v-on实现产品金额动态计算 5.综合演示 ① ...

  6. VUE2.0实现购物车和地址选配功能学习第七节

    第七节 卡片选中,设置默认 1.卡片选中html:<li v-for="(item,index) in filterAddress" v-bind:class="{ ...

  7. VUE2.0实现购物车和地址选配功能学习第五节

    第五节 单件商品金额计算和单选全选功能 1.vue精髓在于操作data模型来改变dom,渲染页面,而不是直接去改变dom 2.加减改变总金额功能: html:<div class="c ...

  8. VUE2.0实现购物车和地址选配功能学习第四节

    第四节 v-on实现金额动态计算 用¥金额 进行格式处理,可以使用原生js进行转换,但是在vuei,使用filter过滤器更加方便 注: 1.es6语法=>和import等 好处在于res参数后 ...

  9. VUE2.0实现购物车和地址选配功能学习第三节

    第三节 使用v-for渲染商品列表 1.使用vue-resource插件引入json数据 (注:在谷歌中调试打断点-- ,console还可以输出vm,res等属性列表,或者productList等一 ...

随机推荐

  1. 【转】console.log 用法

    标签: 转自http://www.cnblogs.com/ctriphire/p/4116207.html 大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是 ...

  2. 第213天:12个HTML和CSS必须知道的重点难点问题

    12个HTML和CSS必须知道的重点难点问题 这12个问题,基本上就是HTML和CSS基础中的重点个难点了,也是必须要弄清楚的基本问题,其中定位的绝对定位和相对定位到底相对什么定位?这个还是容易被忽视 ...

  3. string.Format出现异常:输入字符串的格式不正确 Exception during StringFormat

    错误信息:Exception during StringFormat:输入字符串的格式不正确 “System.FormatException”类型的未经处理的异常在 mscorlib.dll 中发生 ...

  4. HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 之间的区别

    HttpRequest,WebRequest,HttpWebRequest,WebClient,HttpClient 今天我们来聊一下他们之间的关系与区别. HttpRequest 类 .NET Fr ...

  5. Ubuntu开发用新机安装流程

    1.SSH安装 Ubuntu缺省已安装客户端,此处安装服务端 sudo apt-get install openssh-server 确认sshserver是否启动 netstat -tlp | gr ...

  6. Listview 包含 edittext 的解决方案

    转载:https://blog.csdn.net/qq_28268507/article/details/53666576 一. 前几天在群里聊天,碰到一个哥们问listview的itemview中包 ...

  7. 二:C#对象、集合、DataTable与Json内容互转示例;

    导航目录: Newtonsoft.Json 概述 一:Newtonsoft.Json 支持序列化与反序列化的.net 对象类型:    二:C#对象.集合.DataTable与Json内容互转示例: ...

  8. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  9. 爬虫_淘宝(selenium)

    总体来说代码还不是太完美 实现了js渲染网页的解析的一种思路 主要是这个下拉操作,不能一下拉到底,数据是在中间加载进来的, 具体过程都有写注释 from selenium import webdriv ...

  10. Codeforces Round #520

    占个坑慢慢填 A ()[http://codeforces.com/contest/1062/problem/A] 题意:现在有一个长度为n的严格上升正整数序列 每个数的取值在[1, 1000] 现在 ...