明霞山资源网 Design By www.htccd.com
本文实例为大家分享了vue实现淘宝购物车的具体代码,供大家参考,具体内容如下
淘宝购物车功能,效果如下图
非常简单的逻辑,没有做代码的封装,代码如下
<div class="list-container">
<div class="top-ops">
<div>
<img src="/UploadFiles/2021-04-02/HomeRecommendShopInfoAdress@2x.png">
export default {
components: {
},
name: "life",
data() {
return {
cartStatus:"account", //购物车状态,account结算,edit删除编辑状态
cartShops: [], //店铺列表
sumPrice:0, //合计金额
totalNumber: 0, //总数
shopList:[], //店铺列表
goodsList:[], //商品列表
};
},
watch: {
},
mounted() {
this.getCartDetail();
},
methods: {
//购物车列表
getCartDetail: function(){
this.$http.get("api/product/v1/getCartDetail").then( res => {
if(res.data.code === 200){
//console.log(res.data.data)
this.cartShops = res.data.data.cartShops;
}else{
Toast(res.data.msg);
}
}).catch( error => {
console.log(error)
})
},
//商品选择
goodsCkeck: function(event,goodsList,shopList,shopId){
//商品列表+-,店铺是否checked(店铺列表+-),全选是否checked
var input = document.getElementsByTagName('input')
if(event.currentTarget.checked){
this.goodsList.push(String(event.currentTarget.value));
//如果店铺内所有商品全选,店铺选中
var newArr = this.goodsList;
var tt = goodsList.every(function(itemValue){
return (newArr.indexOf(String(itemValue.cartDetailId)) != -1)
})
if(tt){
//店铺内全选,店铺checked,店铺列表+
for(var i = 0;i<input.length;i++){
if(input[i].value == shopId){
input[i].checked = true;
}
}
this.shopList.push(String(shopId)); //防止shopid是number类型造成麻烦
//如果所有店铺都全选,则全选按钮checked
if(this.shopList.length === shopList.length){
//所有店铺全选
for(var i = 0;i<input.length;i++){
if(input[i].name == 'allRadio'){
input[i].checked = true;
}
}
}
}
}else{
//商品列表--
this.goodsList.splice(this.goodsList.indexOf(event.currentTarget.value),1)
//如果店铺checked,则取消,店铺列表--
for(var i = 0;i<input.length;i++){
if(input[i].value == shopId){
if(input[i].checked){
input[i].checked = false;
this.shopList.splice(this.shopList.indexOf(String(shopId)),1); //防止shopid是number类型造成麻烦
}
}
//任意一个不选,全选取消
if(input[i].name == 'allRadio'){
input[i].checked = false;
}
}
}
//计算总价和数量
this.caculate();
},
//店铺选择
shopCheck: function(event,shopList){
//店铺选中则对应商品全选,否则全不选
//console.log(event.currentTarget)
var input = document.getElementsByTagName('input')
if(event.currentTarget.checked){
//店铺列表+,店铺checked,店铺内商品全checked,商品列表++
//console.log(this.shopList)
this.shopList.push(String(event.currentTarget.value));
//店铺内商品全checked
for(var i = 0;i<input.length;i++){
if(input[i].getAttribute('dataId') == event.currentTarget.value){
//将没有选中的checked,并加入列表,去重
if(!input[i].checked){
input[i].checked = true;
//商品列表++
this.goodsList.push(String(input[i].value))
}
}
}
//所有店铺全选
if(this.shopList.length === shopList.length){
for(var i = 0;i<input.length;i++){
if(input[i].name == 'allRadio'){
input[i].checked = true;
}
}
}
}else{
//店铺取消checked,店铺列表--,店铺内所有商品取消checked,商品列表--
this.shopList.splice(this.shopList.indexOf(String(event.currentTarget.value)),1);
//店铺内所有商品取消checked
for(var i = 0;i<input.length;i++){
if(input[i].getAttribute('dataId') == event.currentTarget.value){
input[i].checked = false;
//商品列表--
this.goodsList.splice(this.goodsList.indexOf(input[i].value),1);
}
//任意一个不选,全选取消
if(input[i].name == 'allRadio'){
input[i].checked = false;
}
}
}
//计算总价和数量
this.caculate();
},
//所有全选
allCheck: function(event){
var input = document.getElementsByTagName('input')
if(event.currentTarget.checked){
//全选checked,所有店铺checked,店铺列表++,所有商品checked,商品列表++
for(var i = 0;i<input.length;i++){
//去重
if(!input[i].checked){
input[i].checked = true;
if(input[i].name == 'shopRadio'){
this.shopList.push(String(input[i].value))
}
if(input[i].name == 'goodRadio'){
this.goodsList.push(String(input[i].value))
}
}
}
}else{
//全不选取消checked,店铺全部取消checked,店铺列表清空,所有商品取消checked,商品列表清空
for(var i = 0;i<input.length;i++){
input[i].checked = false;
this.shopList = [];
this.goodsList = [];
}
}
//计算总价和数量
this.caculate();
},
//计算总金额总数量
caculate: function(){
var input = document.getElementsByTagName('input');
var newArr = [];
for(var i = 0;i<input.length;i++){
if(input[i].name == 'goodRadio' && input[i].checked){
var num = input[i].parentNode.parentNode.children[2].children[2].children[2].innerHTML;
newArr.push(
{
'price': input[i].getAttribute('price'),
'num': num
}
)
}
}
this.totalNumber = newArr.length;
//归零
this.sumPrice = 0;
for(var j = 0,len = newArr.length;j<len;j++){
this.sumPrice += newArr[j].price * newArr[j].num;
}
},
//数量减小
numDecrease: function(num){
//如果当前input选中,则修改数量计算价格,如果当前input没有选中,则修改数量不计算价格
var spanList = event.currentTarget.parentNode.children;
for(var i = 0,len = spanList.length;i<len;i++){
if(spanList[i].getAttribute("class") == 'beeforCacul'){
spanList[i].style.display = 'none';
}
if(spanList[i].getAttribute("class") == 'cacul'){
spanList[i].style.display = 'block';
var caculNum = spanList[i].innerHTML;
if(caculNum < 2){
Toast('宝贝不能再少了哦');
}else{
caculNum --
spanList[i].innerHTML = caculNum;
}
}
}
if(event.currentTarget.parentNode.parentNode.parentNode.children[0].children[0].checked){
this.caculate();
}
},
//数量增加
numAdd: function(num){
var spanList = event.currentTarget.parentNode.children;
//console.log(event.currentTarget.parentNode.children)
for(var i = 0,len = spanList.length;i<len;i++){
if(spanList[i].getAttribute("class") == 'beeforCacul'){
spanList[i].style.display = 'none';
}
if(spanList[i].getAttribute("class") == 'cacul'){
spanList[i].style.display = 'block';
var caculNum = spanList[i].innerHTML;
caculNum ++;
spanList[i].innerHTML = caculNum;
}
}
if(event.currentTarget.parentNode.parentNode.parentNode.children[0].children[0].checked){
this.caculate();
}
},
//去结算
cauSum:function(){
if(this.sumPrice === 0){
Toast('您还没有选择宝贝哦');
}else{
this.$router.push('/cart/order')
}
},
}
};
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
vue,淘宝,购物车
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
