Vue實(shí)現(xiàn)本地購物車功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)本地購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
功能分析 : v-for顯示商品名字,價(jià)格,數(shù)量和對(duì)商品進(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; }
效果圖如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue 解決父組件跳轉(zhuǎn)子路由后當(dāng)前導(dǎo)航active樣式消失問題
這篇文章主要介紹了Vue 解決父組件跳轉(zhuǎn)子路由后當(dāng)前導(dǎo)航active樣式消失問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼
本篇文章主要介紹了vue中用H5實(shí)現(xiàn)文件上傳的方法實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Vue之解決Echarts組件使用ID不能復(fù)用的問題
這篇文章主要介紹了Vue之解決Echarts組件使用ID不能復(fù)用的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03Vue自定義指令學(xué)習(xí)及應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了Vue中自定義指令的學(xué)習(xí)以及如何利用Vue制作一個(gè)簡(jiǎn)單的學(xué)生信息管理系統(tǒng),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05Vue 路由間跳轉(zhuǎn)和新開窗口的方式(query、params)
這篇文章主要介紹了Vue 路由間跳轉(zhuǎn)和新開窗口的方式,本文主要通過query方式和params方式介紹,需要的朋友可以參考下2019-12-12