午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

vue實現購物車加減

瀏覽:164日期:2023-01-17 11:27:59

本文實例為大家分享了vue實現購物車加減的具體代碼,供大家參考,具體內容如下

通常我們會在模板中綁定表達式,模板是用來描述視圖結構的。如果模板中的表達式存在過多的邏輯,模板會變得臃腫不堪,維護變得非常困難。因此,為了簡化邏輯,當某個屬性的值依賴于其他屬性的值時,我們可以使用計算屬性。

那么什么是計算屬性呢?

計算屬性就是當其依賴屬性的值發生變化是,這個屬性的值會自動更新,與之相關的DOM部份也會同步自動更新。實現的效果圖如下:

vue實現購物車加減

我是使用了bootstrap跟Vue去完成這個效果的。

首先導入包:

<link rel='stylesheet' href='http://www.leifengta.com.cn/bcjs/css/bootstrap.css' /><script type='text/javascript' src='http://www.leifengta.com.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> 商品價格 </td> <td> 商品數量 </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'> 金額總計:{{sum}} </p> <p class='text-right'> 商品數量:{{count}} </p> <hr /> <form> <div class='form-group'> <input placeholder='商品名稱' v-model='shopName' /> </div> <div class='form-group'> <input placeholder='商品價格' 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>

通過以上的代碼即可實現簡單的購物車加減與增加刪除!

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 一区二区三区免费看 | 中文字幕在线播 | 亚洲91久久 | 免费在线黄 | 污污的视频在线免费观看 | 久久国产成人 | 在线看日韩av | 婷婷久久综合 | 一区二区三区视频免费观看 | 日女人的逼 | 国产日韩欧美成人 | 蜜臀av在线 | 黄色av免费播放 | 97中文字幕| 久精品在线观看 | 成人免费超碰 | 黄色激情视频在线观看 | 四虎com| 亚洲va欧美va天堂v国产综合 | www毛片 | 国产99免费视频 | 在线视频区| 婷婷综合视频 | 艳母动漫在线免费观看 | 日韩欧美一级视频 | 九九热精品免费视频 | 四虎永久免费网站 | 骚虎在线视频 | 在线观看免费黄色 | 欧美日韩免费做爰视频 | 久久久久香蕉 | 日本韩国在线 | 91日韩| 黄色av大全 | 日韩免费黄色片 | 日本中文字幕一区 | 日韩在线免费观看视频 | 夜夜嗨av一区二区三区 | 涩涩一区| 亚洲第一黄色 | 五月天视频网 |