vue實(shí)現(xiàn)購物車加減
本文實(shí)例為大家分享了vue實(shí)現(xiàn)購物車加減的具體代碼,供大家參考,具體內(nèi)容如下
通常我們會(huì)在模板中綁定表達(dá)式,模板是用來描述視圖結(jié)構(gòu)的。如果模板中的表達(dá)式存在過多的邏輯,模板會(huì)變得臃腫不堪,維護(hù)變得非常困難。因此,為了簡化邏輯,當(dāng)某個(gè)屬性的值依賴于其他屬性的值時(shí),我們可以使用計(jì)算屬性。
那么什么是計(jì)算屬性呢?
計(jì)算屬性就是當(dāng)其依賴屬性的值發(fā)生變化是,這個(gè)屬性的值會(huì)自動(dòng)更新,與之相關(guān)的DOM部份也會(huì)同步自動(dòng)更新。
實(shí)現(xiàn)的效果圖如下:
我是使用了bootstrap跟Vue去完成這個(gè)效果的。
首先導(dǎo)入包:
<link rel="stylesheet" href="css/bootstrap.css" /> <script type="text/javascript" src="js/vue.js" ></script>
接著是布局樣式:
<div id="app"> <div class="container"> <table class="table table-bordered table-hover"> <tr> <td> <input type="checkbox" v-model="checkAll" @click="selectAll" /> </td> <td> 商品名稱 </td> <td> 商品價(jià)格 </td> <td> 商品數(shù)量 </td> <td> 商品總額 </td> <td> 操作 </td> </tr> <tr v-for="(item,index) in listInfo"> <td> <input type="checkbox" :value="item.id" v-model="checkItem" @change="selectOne" /> </td> <td>{{item.shopName}}</td> <td>{{item.shopPrice}}</td> <td> <button class="btn btn-default" @click="reduce(index)">-</button> <input type="text" v-model="item.shopCount" /> <button class="btn btn-default" @click="add(index)">+</button> </td> <td> {{item.shopPrice*item.shopCount}} </td> <td> <button class="btn btn-warning" @click="del(index)">刪除</button> </td> </tr> </table> <p class="text-right"> 金額總計(jì):{{sum}} </p> <p class="text-right"> 商品數(shù)量:{{count}} </p> <hr /> <form> <div class="form-group"> <input class="form-control" placeholder="商品名稱" v-model="shopName" /> </div> <div class="form-group"> <input class="form-control" placeholder="商品價(jià)格" v-model = "shopPrice"/> </div> <div class="form-group"> <button class="btn btn-primary" type="button" @click="addInfo">增加</button> </div> </form> </div> </div>
最后是方法:
<script> new Vue({ el:"#app", data:{ listInfo:[ {id:1,shopName:"男裝1",shopPrice:1000,shopCount:0}, {id:2,shopName:"男裝2",shopPrice:2000,shopCount:0}, {id:3,shopName:"男裝3",shopPrice:3000,shopCount:0}, {id:4,shopName:"男裝4",shopPrice:4000,shopCount:0}, {id:5,shopName:"男裝5",shopPrice:5000,shopCount:0}, ], shopName:"", shopPrice:"", checkItem:[], checkAll:false }, methods:{ add:function(index){ this.listInfo[index].shopCount++ }, reduce:function(index){ if(this.listInfo[index].shopCount<=0){ this.listInfo[index].shopCount = 0 }else { this.listInfo[index].shopCount-- } }, del:function(index){ this.listInfo.splice(index,1) }, addInfo:function(){ // alert(1) var obj = { id:this.listInfo.length+1, shopName:this.shopName, shopPrice:this.shopPrice, shopCount:0 } console.log(obj) this.listInfo.push(obj) }, selectAll:function(){ this.checkItem = [] if(!this.checkAll){ for (var i=0;i<this.listInfo.length;i++) { this.checkItem.push(this.listInfo[i].id) } }else { this.checkItem = [] this.checkAll = false } }, selectOne(){ console.log(this.checkItem) if(this.checkItem.length == this.listInfo.length){ this.checkAll = true }else { this.checkAll = false } } }, computed:{ sum(){ var total = 0 for (var i=0;i<this.listInfo.length;i++) { total+=parseFloat(this.listInfo[i].shopPrice)*parseFloat(this.listInfo[i].shopCount) } return total }, count:function(){ var total = 0 for (var i=0;i<this.listInfo.length;i++) { total+=parseInt(this.listInfo[i].shopCount) } return total } } }) </script>
通過以上的代碼即可實(shí)現(xiàn)簡單的購物車加減與增加刪除!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3響應(yīng)式高階用法之shallowRef()的使用
在Vue3框架中,shallowRef()為開發(fā)者提供了細(xì)粒度的響應(yīng)式控制能力,特別適用于處理深層嵌套對象或需要部分響應(yīng)式的場景,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2024-09-09Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼
本篇文章主要介紹了Vue下滾動(dòng)到頁面底部無限加載數(shù)據(jù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04Vue不同項(xiàng)目之間傳遞、接收參數(shù)問題
這篇文章主要介紹了Vue不同項(xiàng)目之間傳遞、接收參數(shù)問題,主要針對是登錄操作,我們?yōu)榱送瓿蒘SO(Single Sign On)的效果,認(rèn)證和授權(quán)在UC完成,用戶發(fā)起資源請求,服務(wù)網(wǎng)關(guān)會(huì)進(jìn)行過濾,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04在Vue3中使用vue3-print-nb實(shí)現(xiàn)前端打印功能
在前端開發(fā)中,經(jīng)常需要打印頁面的特定部分,比如客戶列表或商品詳情頁,要快速實(shí)現(xiàn)這些功能,可以使用 vue3-print-nb 插件,本文就給大家介紹了如何在 Vue 3 中使用 vue3-print-nb 實(shí)現(xiàn)靈活的前端打印,需要的朋友可以參考下2024-06-06Vue實(shí)現(xiàn)側(cè)邊菜單欄手風(fēng)琴效果實(shí)例代碼
本文通過一段簡單的代碼給大家介紹了基于純vue實(shí)現(xiàn)側(cè)邊菜單欄手風(fēng)琴效果,代碼很簡單,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05vue項(xiàng)目運(yùn)行或打包時(shí),頻繁內(nèi)存溢出情況問題
這篇文章主要介紹了vue項(xiàng)目運(yùn)行或打包時(shí),頻繁內(nèi)存溢出情況的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04Vue elementUI 自定義表單模板組件功能實(shí)現(xiàn)
在項(xiàng)目開發(fā)中,我們會(huì)遇到這種需求,在管理后臺添加自定義表單,在指定的頁面使用定義好的表單,這篇文章主要介紹了Vue elementUI 自定義表單模板組件,需要的朋友可以參考下2022-12-12vue跨域處理方式(vue項(xiàng)目中baseUrl設(shè)置問題)
這篇文章主要介紹了vue跨域處理方式(vue項(xiàng)目中baseUrl設(shè)置問題),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05