Vue實現(xiàn)本地購物車功能
更新時間:2018年12月05日 11:34:03 作者:Eclipextro
這篇文章主要為大家詳細介紹了Vue實現(xiàn)本地購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Vue實現(xiàn)本地購物車功能的具體代碼,供大家參考,具體內容如下
功能分析 : v-for顯示商品名字,價格,數(shù)量和對商品進行操作,全選的功能
結構仍然分成 : index.html , index.js , style.css
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>購物車實例</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>序號</th> <th>商品名稱</th> <th>商品單價</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>總價 : ¥{{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 : { //通過計算屬性獲取總價格 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 } //返回一個符合千分位格式的金額數(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 }) }, //單選,當全部選中時,改變全選按鈕的狀態(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; }
效果圖如下
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Vue 解決父組件跳轉子路由后當前導航active樣式消失問題
這篇文章主要介紹了Vue 解決父組件跳轉子路由后當前導航active樣式消失問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Vue 路由間跳轉和新開窗口的方式(query、params)
這篇文章主要介紹了Vue 路由間跳轉和新開窗口的方式,本文主要通過query方式和params方式介紹,需要的朋友可以參考下2019-12-12