• 参考资料: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. php1

    正则表达式 $p = '/name:(\w+\s?\w+)/'; $str = "name:steven jobs"; preg_match($p, $str, $match); ...

  2. Essential Phone刷机教程

    安装fastboot驱动(Essential-PH1-WindowsDrivers) 下载ADB刷机指令工具:platform-tools(ADB): 进入开发者选项,打开 USB 调试,OEM解锁选 ...

  3. Redis之父表示ARM服务器没戏!

    ARM表示Neoverse N1平台和E1 CPU即将发布,Neoverse N1和E1采用7nm制程,并且为服务器和通信设备增加重要提升,拥有高可扩展性.高处理量以及高性能,将分别在2020年和20 ...

  4. mysql 自带的性能压力测试工具

    mysqlslap -h127.0.0.1 -uroot -p --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate ...

  5. linux shell系列10 判断某个月中的星期六和星期天

    #!/bin/bashread -p "请输入月份:" month #输入要查找的月份 mon=`date -d "0 month ago" +%m` #计算本 ...

  6. [WC2018]州区划分——FWT+DP+FST

    题目链接: [WC2018]州区划分 题目大意:给n个点的一个无向图,点有点权,要求将这n个点划分成若干个部分,每部分合法当且仅当这部分中所有点之间的边不能构成欧拉回路.对于一种划分方案,第i个部分的 ...

  7. Typecho——数据库无法连接问题

    报错 对不起,无法连接数据库,请先检查数据库配置再继续进行安装 解决方案 创建数据库 reate database databaseName; 远程权限 开启远程权限 GRANT ALL PRIVIL ...

  8. POJ1015-Jury Compromise-dp

    略复杂的dp题. 有n个人,每个人有两个分数di,pi.从中选出m个人,要求|sigma(di)-sigma(pi)|最小,相同时则输出sigma(di)+sigma(pi)最大的情况. 答案完整输出 ...

  9. Basic remains POJ - 2305 同余模 高精度处理

    题意 给出B(10以内大于0)进制下 p (1000位以内)和m(9位以内) 求 p%m 在b进制下等于什么 思路: 可以计算   1e9不会溢出Int所以m在int值以内  先求m  要处理p  每 ...

  10. hdu 2844 Coins (多重背包+二进制优化)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 思路:多重背包 , dp[i] ,容量为i的背包最多能凑到多少容量,如果dp[i] = i,那么代表 ...