vue實(shí)現(xiàn)購物車加減
本文實(shí)例為大家分享了vue實(shí)現(xiàn)購物車加減的具體代碼,供大家參考,具體內(nèi)容如下
通常我們會在模板中綁定表達(dá)式,模板是用來描述視圖結(jié)構(gòu)的。如果模板中的表達(dá)式存在過多的邏輯,模板會變得臃腫不堪,維護(hù)變得非常困難。因此,為了簡化邏輯,當(dāng)某個(gè)屬性的值依賴于其他屬性的值時(shí),我們可以使用計(jì)算屬性。
那么什么是計(jì)算屬性呢?
計(jì)算屬性就是當(dāng)其依賴屬性的值發(fā)生變化是,這個(gè)屬性的值會自動更新,與之相關(guān)的DOM部份也會同步自動更新。實(shí)現(xiàn)的效果圖如下:
我是使用了bootstrap跟Vue去完成這個(gè)效果的。
首先導(dǎo)入包:
<link rel='stylesheet' href='http://m.4tl426be.cn/bcjs/css/bootstrap.css' /><script type='text/javascript' src='http://m.4tl426be.cn/bcjs/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 @click='reduce(index)'>-</button> <input type='text' v-model='item.shopCount' /> <button @click='add(index)'>+</button> </td> <td> {{item.shopPrice*item.shopCount}} </td> <td> <button @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 placeholder='商品名稱' v-model='shopName' /> </div> <div class='form-group'> <input placeholder='商品價(jià)格' v-model = 'shopPrice'/> </div> <div class='form-group'> <button 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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python獲取抖音關(guān)注列表封號賬號的實(shí)現(xiàn)代碼2. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 解決Python 進(jìn)程池Pool中一些坑5. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼6. Python如何讀寫CSV文件7. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介8. ajax請求添加自定義header參數(shù)代碼9. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法10. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁
