Vue實(shí)現(xiàn)本地購物車功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)本地購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
功能分析 : v-for顯示商品名字,價(jià)格,數(shù)量和對商品進(jìn)行操作,全選的功能
結(jié)構(gòu)仍然分成 : index.html , index.js , style.css
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>購物車實(shí)例</title>
<link rel="stylesheet" href="style.css" >
</head>
<body>
<div id="app" v-cloak>
<template v-if="list.length">
<table>
<thead>
<tr>
<th><input type="checkbox" @click='checkAll' :checked='allCheck'></th>
<th>序號(hào)</th>
<th>商品名稱</th>
<th>商品單價(jià)</th>
<th>購買數(shù)量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in list">
<td><input type="checkbox" :checked='item.isChecked' @click="singleCheck(index)"></td>
<td>{{index + 1}}</td>
<td>{{item.name}}</td>
<td>{{item.price}}</td>
<td>
<button @click="handleReduce(index)" :disable="item.count === 1 ">-</button>
{{item.count}}
<button @click="handleAdd(index)">+</button>
</td>
<td>
<button @click="handleRemove">移除</button>
</td>
</tr>
</tbody>
</table>
<div>總價(jià) : ¥{{totalPrice}}</div>
</template>
<div v-else>購物車為空</div>
</div>
</body>
<!-- 通過cdn引入-->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="index.js"></script>
</html>
index.js
const app = new Vue({
el : '#app',
data : {
allCheck:false,
list : [
{
id: 1 ,
name :'iPhone 8 ',
price: 6188 ,
count: 1 ,
isChecked : false
},
{
id: 2 ,
name :'小米 8 ',
price: 5888 ,
count: 1 ,
isChecked : false
},
{
id: 3 ,
name :'iPad Pro ',
price: 11000 ,
count: 1 ,
isChecked : false
},
{
id: 4 ,
name :'雷神SE9',
price: 6188 ,
count: 10 ,
isChecked : false
},
]
},
computed : {
//通過計(jì)算屬性獲取總價(jià)格
totalPrice:function() {
let total = 0;
const newArr = this.list.filter(value => {
return value.isChecked == true
})
for(var i = 0 ;i<newArr.length;i++) {
total += this.list[i].price * this.list[i].count
}
//返回一個(gè)符合千分位格式的金額數(shù)
//return total.toString().replace(/\B(?=(\d{3})+$)/g,',')
return total
}
},
methods : {
//減法
handleReduce:function(index) {
if(this.list[index].count === 1) return ;
this.list[index].count--;
},
//加法
handleAdd:function(index) {
this.list[index].count++
},
//移除
handleRemove:function(index) {
this.list.splice(index,1)
},
//全選
checkAll() {
this.allCheck = !this.allCheck
this.list.forEach(value => {
value.isChecked = this.allCheck
})
},
//單選,當(dāng)全部選中時(shí),改變?nèi)x按鈕的狀態(tài)
singleCheck(index) {
this.list[index].isChecked = !this.list[index].isChecked
const selectData = this.list.filter(value => {
return value.isChecked == true
})
this.allCheck = selectData.length === this.list.length ? true : false
}
}
})
style.css
[v-cloak] {
display: none;
}
table {
border: 1px solid #e9e9e9;
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
}
th,td {
padding: 8px 16px;
border: 1px solid #e9e9e9;
text-align: left;
}
th {
background: yellowgreen;
color: #5c6b77;
font-weight: 600;
white-space: nowrap;
}
效果圖如下

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue+ECharts實(shí)現(xiàn)中國地圖的繪制及各省份自動(dòng)輪播高亮顯示
這篇文章主要介紹了Vue+ECharts實(shí)現(xiàn)中國地圖的繪制以及拖動(dòng)、縮放和各省份自動(dòng)輪播高亮顯示,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互
這篇文章主要介紹了詳解基于Vue-cli搭建的項(xiàng)目如何和后臺(tái)交互,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
解決antd 下拉框 input [defaultValue] 的值的問題
這篇文章主要介紹了解決antd 下拉框 input [defaultValue] 的值的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue如何設(shè)置定時(shí)器和清理定時(shí)器
這篇文章主要介紹了vue如何設(shè)置定時(shí)器和清理定時(shí)器,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
基于Vue uniapp實(shí)現(xiàn)貪吃蛇游戲
貪吃蛇游戲想必是很多70、80后的回憶,一直到現(xiàn)在也深受大家的喜歡。本文將利用Vue+uniapp實(shí)現(xiàn)這一經(jīng)典的游戲,感興趣的可以了解一下2022-04-04

