第027个
专栏目标
在vue和element UI联合技术栈的操控下,本专栏提供行之有效的源代码示例和信息点介绍,做到灵活运用。
需求背景
本示例是演示两个列表的互动的场景,模仿购物车添加、删除商品的状态. 如果商品添加到购物车上,则显示已加入购物车,否则显示未加入购物车; 购物车中的商品,可以删除,同时更改商品列表的状态。 本文用到了array的改写,添加,删除等方法。
示例效果
示例源代码(共135行)
/*
* @Author: 大剑师兰特(xiaozhuanlan),还是大剑师兰特(CSDN)
* @此源代码版权归大剑师兰特所有,可供学习或商业项目中借鉴,未经授权,不得重复地发表到博客、论坛,问答,git等公共空间或网站中。
* @Email: 2909222303@qq.com
* @weixin: gis-dajianshi
* @First published in CSDN
* @First published time: 2022-09-06
*/
<template>
<div class="container">
<div class="top">
<h3>商品列表与购物车列表,数据联动 </h3>
<div class="author">大剑师兰特, 还是大剑师兰特,gis-dajianshi</div>
</div>
<h5>商品列表</h5>
<div class="oneLine" v-for="(item,index) in productList" :key="index">
<div class="fl20"><img :src="item.thumbnail_pic_s" alt=""></div>
<div class="fl20">{{item.title}} </div>
<div class="fr20">
<el-link type="danger" v-show="!item.cart" @click="addCart(index)">未加入购物车</el-link>
<el-link type="default" v-show="item.cart" @click="removeCart(index,item.title)">已加入购物车</el-link>
</div>
</div>
<h5>购物车列表</h5>
<div v-for="(item,index) in cartList" :key="item.index" class="oneLine">
<div class="fl20"><img :src="item.thumbnail_pic_s" alt=""></div>
<div class="fl20">{{item.title}} </div>
<div class="fr20">
<el-link type="danger" @click='delItem(index,item.title)'> 删除</el-link>
</div>
</div>
</div>
</template>
<script>
export default {
data() {
return {
cartList: [],
productList: []
}
},
methods: {
getdata() {
let url = "/listdata"
this.$request(url, {}, "GET")
.then((res) => {
this.productList = res.data.data
})
},
updateData() {
for (let i = 0; i < this.productList.length; i++) {
if (this.cartList.length > 0) {
let xx = this.productList[i].title;
this.cartList.some(item => {
if (item.title == xx) {
this.$set(this.productList[i], "cart", true);
}
})
}
}
},
addCart(x) {
console.log(this.productList[x])
this.cartList.unshift(this.productList[x])
this.updateData()
},
removeCart(x, y) {
this.cartList = this.cartList.filter((item) => {
return item.title != y
})
this.$set(this.productList[x], "cart", false);
},
delItem(i, d) {
this.cartList.splice(i, 1);
for (let i = 0; i < this.productList.length; i++) {
if (this.productList[i].title == d) {
this.$set(this.productList[i], "cart", false);
}
}
},
},
mounted() {
this.getdata();
},
updated() {
this.updateData()
},
}
</script>
<style scoped>
.container {
width: 1000px;
height: 580px;
margin: 50px auto;
border: 1px solid green;
}
.top {
margin: 0 auto 30px;
padding: 10px 0;
background: #cddeef;
}
.oneLine {
width: 100%;
height: 50px;
line-height: 50px;
background: #eee;
margin-top: 10px;
overflow: hidden;
cursor: pointer;
text-align: left;
}
.oneLine .fl20 {
float: left;
padding: 0 10px;
min-width: 150px;
}
.oneLine .fr20 {
float: right;
min-width: 50px;
padding-right: 15px;
}
</style>
核心代码
updateData() {
for (let i = 0; i < this.productList.length; i++) {
if (this.cartList.length > 0) {
let xx = this.productList[i].title;
this.cartList.some(item => {
if (item.title == xx) {
this.$set(this.productList[i], "cart", true);
}
})
}
}
},
addCart(x) {
console.log(this.productList[x])
this.cartList.unshift(this.productList[x])
this.updateData()
},
removeCart(x, y) {
this.cartList = this.cartList.filter((item) => {
return item.title != y
})
this.$set(this.productList[x], "cart", false);
},
delItem(i, d) {
this.cartList.splice(i, 1);
for (let i = 0; i < this.productList.length; i++) {
if (this.productList[i].title == d) {
this.$set(this.productList[i], "cart", false);
}
}
},