• 参考资料: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. ORACLE 增加两列字段

    declare v_cnt number; V_SQL VARCHAR2 (500) := '';begin select count(*) into v_cnt from dual where ex ...

  2. How to split DMG on macOS

    hdiutil segment /users/test/test1.dmg -segmentsize 4000m -o /users/test/test2.dmg

  3. git 命令使用集锦

    使用git mv重命名文件,而不是delete然后再add文件. git format常用命令: git format-patch -4 //从当前分支最新提交点往下共生成4个补丁 git forma ...

  4. Listview 包含 edittext 的解决方案

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

  5. 洛谷P1373小a和uim大逃离题解

    题目 这个题好坑啊,首先是他会卡空间,然后我们就只能把一种比较好理解的状态给舍弃,因为空间开不下,然而采用一种难理解的状态就是\(dp[i][j][l][0/1]\)表示\(i\),\(j\)位置,两 ...

  6. 【XSY2190】Alice and Bob VI 树形DP 树剖

    题目描述 Alice和Bob正在一棵树上玩游戏.这棵树有\(n\)个结点,编号由\(1\)到\(n\).他们一共玩\(q\)盘游戏. 在第\(i\)局游戏中,Alice从结点\(a_i\)出发,Bob ...

  7. eclipse中git推送上传错误 没有足够的数据写入

    Can't connect to any repository: https://github.com/jiashubing/test.git (https://github.com/jiashubi ...

  8. MT【276】正切的半角公式

    若$\Delta ABC$满足:$\tan\dfrac{A}{2}\cdot\tan\dfrac{C}{2}=\dfrac{1}{3},b=\dfrac{4}{3}a$,则$\sin B=$_____ ...

  9. QML 用QSortFilterProxyModel实现搜索功能

    搞了一晚上终于实现了,写个博客纪念一下吧. c++部分的代码: #include <QQmlApplicationEngine> #include <QQmlContext> ...

  10. Leetcode 75.颜色分类 By Python

    给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中,我们使用整数 0. 1 和 2 分别表示红色.白色和蓝色. ...