vue實(shí)現(xiàn)購(gòu)物車全部功能的簡(jiǎn)單方法
主要功能如下:
- 增加商品信息
- 修改商品信息
- 刪除單個(gè)商品
- 刪除多個(gè)商品
- 清空購(gòu)物車
- 對(duì)商品單價(jià)進(jìn)行降序排序
- 根據(jù)商品名稱實(shí)現(xiàn)查找
- 實(shí)現(xiàn)商品數(shù)量加減
- 全選反選
- 實(shí)現(xiàn)勾選商品的總價(jià)計(jì)算
效果圖如下:

由于功能比較多就不一個(gè)個(gè)講了,我們先來(lái)講幾個(gè)主要功能的邏輯(增刪改查),最后放全放部代碼
首先我們先來(lái)看增加商品功能
//先來(lái)一個(gè)按鈕綁定顯示事件,點(diǎn)擊添加后出現(xiàn)一個(gè)彈窗 <button type="button" @click="xian">添加</button>
//return 中定義了一個(gè)dis默認(rèn)為false
xian() {
if (!this.dis == false) {
this.dis = false
} else {
this.dis = true
}
},
然后再?gòu)棿爸悬c(diǎn)擊確認(rèn)修改,綁定一個(gè)事件把商品信息添加進(jìn)去
<button type="button" @click="tian">確認(rèn)添加</button>
//將要添加的商品信息push到一個(gè)新的數(shù)組中,添加完成之后關(guān)閉彈窗
tian() {
if (this.name == "" || this.counrty == "" || this.price == "") {
alert("商品信息不允許為空!")
return false
} else {
this.shopPings.push({
name: this.name,
counrty: this.counrty,
price: this.price,
count: this.count,
})
}
this.name = "",
this.counrty = "",
this.price = "",
this.count = ""
this.dis = false
},
商品增加進(jìn)去之后突然發(fā)現(xiàn)商品信息寫(xiě)錯(cuò)了?。???不要慌,接下來(lái)帶大家看看修改功能
還是老規(guī)矩先定義一個(gè)按鈕綁定顯示事件,然后綁定顯示事件,除了事件名稱之外,跟上面添加類同,我們直接來(lái)看確認(rèn)修改功能
//定義按鈕綁定事件
<button type="button" @click="xiugai">確認(rèn)修改</button>
//將購(gòu)物車中的商品信息跟修改之后的進(jìn)行賦值修改,修改完成之后關(guān)閉彈窗
xiugai() {
console.log(this.int)
let index = this.int
console.log(this.name, this.price, this.count, )
this.shopPings[index].name = this.name
this.shopPings[index].price = this.price
this.shopPings[index].counrty = this.counrty
this.shopPings[index].count = this.count
this.dis1 = false
},
有了增加修改之后我們?cè)賮?lái)寫(xiě)一個(gè)刪除
定義一個(gè)按鈕綁定事件,傳入一個(gè)index值最后splice根據(jù)下標(biāo)刪除一條
定義一個(gè)按鈕綁定事件,傳入一個(gè)index值最后splice根據(jù)下標(biāo)刪除一條
<button @click="del(index)">刪除</button>
del(index) {
this.shopPings.splice(index, 1);
},
清空購(gòu)物車的話就比較簡(jiǎn)單了直接設(shè)置按鈕點(diǎn)擊后數(shù)組為空即可
alldel() {
this.shopPings = []
},
最后我們來(lái)看一下購(gòu)物車中的查詢功能
//return中設(shè)置input的value值 //定義一個(gè)input框,綁定value值,設(shè)置enter鍵盤(pán)事件并且綁定搜索事件 <input type="text" placeholder="請(qǐng)輸入要查詢的商品名稱" v-model="input_value" @keyup.13="search">
具體看注釋
//先來(lái)一個(gè)判斷判斷搜索框?yàn)榭盏臅r(shí)候進(jìn)行查詢會(huì)有提示信息不允許為空
//然后拿到數(shù)組中的每一項(xiàng)的名稱進(jìn)行查找如果沒(méi)有這個(gè)商品名稱則提示沒(méi)有該商品
//最后對(duì)數(shù)組filter進(jìn)行篩選,通過(guò)搜索框中輸入的商品名稱對(duì)比購(gòu)物車中的商品名稱來(lái)找到對(duì)應(yīng)商品
search() {
if (!this.input_value) {
return alert('請(qǐng)輸入內(nèi)容')
}
if (
this.shopPings.every((v) => {
v.name.indexOf(this.input_value) == -1
})
) {
this.input_value = ''
return alert('沒(méi)有此商品')
}
this.shopPings = this.shopPings.filter((v) => {
v.name.replace(this.input_value, this.input_value)
return v.name.indexOf(this.input_value) != -1
})
}
全部代碼:
<template>
<div class="shopCar">
<header>
<button type="button" @click="delSelect">批量刪除</button>
<button type="button" @click="alldel">清空購(gòu)物車</button>
<button type="button" @click="xian">添加</button>
<button type="button" @click="jiang">排序</button>
<input type="text" placeholder="請(qǐng)輸入要查詢的商品名稱" v-model="input_value" @keyup.13="search">
<div class="xiu" v-show="dis1">
<input type="text" placeholder="名稱" v-model="name">
<input type="text" placeholder="價(jià)格" v-model="price">
<input type="text" placeholder="數(shù)量" v-model="count">
<input type="text" placeholder="產(chǎn)地" v-model="counrty">
<button type="button" @click="xiugai">確認(rèn)修改</button>
</div>
<div class="add" v-show="dis">
<input type="text" placeholder="名稱" v-model="name">
<input type="text" placeholder="價(jià)格" v-model="price">
<input type="text" placeholder="數(shù)量" v-model="count">
<input type="text" placeholder="產(chǎn)地" v-model="counrty">
<button type="button" @click="tian">確認(rèn)添加</button>
</div>
</header>
<main>
<ul>
<li>
<p><input type="checkbox" v-model="allChecked">
全選</p>
<p>名稱</p>
<p>產(chǎn)地</p>
<p>數(shù)量</p>
<p>單價(jià)</p>
<p>小計(jì)</p>
<p>操作</p>
</li>
<li v-for="(item,index) in shopPings" :key="index">
<p><input type="checkbox" v-model="item.checked">{{item.id}}</p>
<p>{{item.name}}</p>
<p>{{item.counrty}}</p>
<p><button type="button" @click="add(item)">+</button>
<input type="text" v-model="item.count" style="width:20px">
<button type="button" @click="remove(item)">-</button>
</p>
<p>{{item.price}}</p>
<p>{{item.price*item.count |suffix}}</p>
<p>
<button type="button" @click="xiu(index)"> 修改</button>
<button @click="del(index)">刪除</button>
</p>
</li>
</ul>
</main>
<footer>
<p>總計(jì){{state.sum |suffix}}</p>
</footer>
</div>
</template>
<style scoped lang="scss">
.shopCar {
width: 1000px;
border: 2px solid black;
margin: 100px auto;
overflow: auto;
header {
display: flex;
justify-content: space-between;
width: 600px;
height: 27px;
overflow: hidden;
.add {
width: 400px;
background: #e4e1e1;
position: absolute;
left: 39%;
top: 40%;
input {
display: block;
margin: 20px auto;
}
button {
display: block;
margin: 0 auto;
}
}
.xiu {
width: 400px;
background: #e4e1e1;
position: absolute;
left: 39%;
top: 40%;
input {
display: block;
margin: 20px auto;
}
button {
display: block;
margin: 0 auto;
}
}
}
main {
// height: 400px;
margin-top: 10px;
ul {
li {
height: 78px;
border-bottom: 2px solid black;
display: flex;
justify-content: space-between;
p {
float: left;
width: 140px;
height: 27px;
border: 2px solid black;
text-align: center;
}
}
}
}
footer {
height: 50px;
margin-top: 13px;
line-height: 50px;
}
}
</style>
<script>
const shopData = [{
id: "",
name: "鞋子",
counrty: "山西",
count: 1,
price: 800,
},
{
id: "",
name: "櫥柜",
counrty: "北京",
count: 1,
price: 3200,
},
{
id: "",
name: "口紅",
counrty: "河北",
count: 2,
price: 200,
},
{
id: "",
name: "漢堡",
counrty: "河南",
count: 2,
price: 200,
},
]
export default {
//過(guò)濾器
filters: {
suffix(value) {
let price = Number(value)
return `¥ ${price.toFixed(2)}`
//在金額前面插入一個(gè)¥符號(hào)然后定義小數(shù)點(diǎn)后面為倆位數(shù)字
}
},
computed: {
//全選
allChecked: {
get() {
const checkeds = this.shopPings.filter((item) => item.checked)
return checkeds.length === this.shopPings.length
},
set(state) {
// console.log(state)
this.shopPings.map((item) => {
item.checked = state
return item
})
}
},
//小計(jì)計(jì)算
totalPrice: function () {
var total = 0;
for (var i = 0; i < this.checkList.length; i++) {
var item = this.checkList[i];
total += item.price * item.count;
}
return total.toLocaleString();
},
//選中的商品總價(jià)計(jì)算
state() {
const checkeds = this.shopPings.filter((item) => item.checked)
const checked = checkeds.length === this.shopPings.length
const sum = checkeds.reduce((a, b) => {
return a + b.count * b.price;
}, 0)
return {
count: checkeds.length,
sum
}
},
},
data() {
return {
shopPings: [],
dis: false, //確認(rèn)提交
dis1: false, //確認(rèn)修改
id: "",
name: "", //名稱
price: "", //單價(jià)
count: "", //數(shù)量
counrty: "", //產(chǎn)地
input_value: "", //查詢框中input的值
}
},
mounted() {
window.fetch("/").then(() => {
this.shopPings = shopData.map((item) => {
item.checked = false
return item
})
})
},
methods: {
//添加商品
xian() {
if (!this.dis == false) {
this.dis = false
} else {
this.dis = true
}
},
//確認(rèn)添加
tian() {
if (this.name == "" || this.counrty == "" || this.price == "") {
alert("商品信息不允許為空!")
return false
} else {
this.shopPings.push({
name: this.name,
counrty: this.counrty,
price: this.price,
count: this.count,
})
}
this.name = "",
this.counrty = "",
this.price = "",
this.count = ""
this.dis = false
},
//刪除商品
del(index) {
this.shopPings.splice(index, 1);
},
//刪除選中的商品
delSelect() {
this.shopPings = this.shopPings.filter((item) => {
if (!item.checked) {
return item
}
})
},
//全部刪除
alldel() {
this.shopPings = []
},
//減少購(gòu)買
remove(data) {
if (data.count > 0) {
data.count--
}
if (data.count === 0) {
data.checked = false
}
},
//增加購(gòu)買
add(data) {
data.count++
},
//修改商品
xiu(i) {
this.int = i
if (!this.dis1 == false) {
this.dis1 = false
} else {
this.dis1 = true
}
},
// 確認(rèn)修改
xiugai() {
console.log(this.int)
let index = this.int
console.log(this.name, this.price, this.count, )
this.shopPings[index].name = this.name
this.shopPings[index].price = this.price
this.shopPings[index].counrty = this.counrty
this.shopPings[index].count = this.count
this.dis1 = false
},
//降序
jiang() {
this.shopPings.sort((a, b) => {
//排序基于的數(shù)據(jù)
return a.price - b.price;
})
},
search() {
if (!this.input_value) {
return alert('請(qǐng)輸入內(nèi)容')
}
if (
this.shopPings.every((v) => {
v.name.indexOf(this.input_value) == -1
})
) {
this.input_value = ''
return alert('沒(méi)有此商品')
}
this.shopPings = this.shopPings.filter((v) => {
v.name.replace(this.input_value, this.input_value)
return v.name.indexOf(this.input_value) != -1
})
}
}
}
</script>
總結(jié)
到此這篇關(guān)于vue實(shí)現(xiàn)購(gòu)物車全部功能的文章就介紹到這了,更多相關(guān)vue實(shí)現(xiàn)購(gòu)物車功能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- vue+vant-UI框架實(shí)現(xiàn)購(gòu)物車的復(fù)選框全選和反選功能
- Vue.js實(shí)現(xiàn)的購(gòu)物車功能詳解
- Vuejs實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)的仿淘寶購(gòu)物車功能詳解
- vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
- Vue.js實(shí)現(xiàn)開(kāi)發(fā)購(gòu)物車功能的方法詳解
相關(guān)文章
Vue + AnimeJS實(shí)現(xiàn)3d輪播圖的詳細(xì)代碼
輪播圖在開(kāi)發(fā)中是經(jīng)常用到的,3D輪播圖是其中最常用的一種,所以在這篇文章中將給大家介紹Vue + AnimeJS實(shí)現(xiàn)3d輪播圖,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-01-01
在Vue3中使用vue3-print-nb實(shí)現(xiàn)前端打印功能
在前端開(kāi)發(fā)中,經(jīng)常需要打印頁(yè)面的特定部分,比如客戶列表或商品詳情頁(yè),要快速實(shí)現(xiàn)這些功能,可以使用 vue3-print-nb 插件,本文就給大家介紹了如何在 Vue 3 中使用 vue3-print-nb 實(shí)現(xiàn)靈活的前端打印,需要的朋友可以參考下2024-06-06
Vue transx組件切換動(dòng)畫(huà)庫(kù)示例詳解
這篇文章主要為大家介紹了Vue transx組件切換動(dòng)畫(huà)庫(kù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
element el-table 表格限制多選個(gè)數(shù)功能
這篇文章主要介紹了element el-table 表格限制多選個(gè)數(shù)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03
vue中的attribute和property的具體使用及區(qū)別
本文主要介紹了vue中的attribute和property的具體使用及區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
詳解webpack打包vue項(xiàng)目之后生成的dist文件該怎么啟動(dòng)運(yùn)行
這篇文章主要介紹了詳解webpack打包vue項(xiàng)目之后生成的dist文件該怎么啟動(dòng)運(yùn)行,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
解決VUE項(xiàng)目在IIS部署出現(xiàn):Uncaught SyntaxError: Unexpected&n
這篇文章介紹了解決VUE項(xiàng)目在IIS部署出現(xiàn):Uncaught SyntaxError: Unexpected token < 報(bào)錯(cuò)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04

