- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue.js框架實(shí)現購物車(chē)功能
本文實(shí)例為大家分享了Vue.js框架實(shí)現購物車(chē)的具體代碼,供大家參考,具體內容如下
<!DOCTYPE html> <html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="../lib/vue.min.js"></script> </head> <body> <div id="app" style="position: relative;left: 30%"> <table cellpadding="10"> <thead> <th><input type="checkbox" v-model="cb" v-on:click="allSelect">全選</th> <th>名稱(chēng)</th> <th>單價(jià)</th> <th>數量</th> <th>金額</th> <th>操作</th> </thead> <tbody> <tr v-for="x in info"> <td><input type="checkbox" v-model="x.bol" v-on:click="sign()"></td> <td>{{x.name}}</td> <td>{{x.price}}</td> <td><input type="number" v-model="x.num" min="0" style="width: 50px;text-align: center" v-on:click="count(x)" v-on:change="count(x)"></td> <td>{{x.total}}</td> <td><button v-on:click="del(x)">刪除</button></td> </tr> </tbody> </table> <Br> <p>總金額:{{all}}</p> </div> <script> var vm = new Vue({ el: "#app", data: { all: 0, cb: false, info: [{ bol: false, name: "iphone7", price: 6000, num: 1, total: 6000 }, { bol: false, name: "榮耀6x", price: 1200, num: 1, total: 1200 }, { bol: false, name: "dell筆記本", price: 4000, num: 1, total: 4000 }] }, methods: { //計算單價(jià) count: function(obj) { for(var i = 0; i < this.info.length; i++) { // if(this.info[i] == obj) { this.info[i].total = obj.price * obj.num; } //如果被選中了,就計算總價(jià)格 if(obj.bol) { this.allSelect(); } } }, //刪除 del: function(obj) { this.info.splice(this.info.indexOf(obj), 1) this.allCount(); }, //單選 sign: function() { this.allCount(); }, //全選 allSelect: function() { for(var i = 0; i < this.info.length; i++) { this.info[i].bol = this.cb; } this.allCount(); }, //計算總價(jià) allCount: function() { //每次計算總價(jià)都要清空 this.all = 0; for(var i = 0; i < this.info.length; i++) { //計算被選中的商品 if(this.info[i].bol) { this.all += this.info[i].total; } } } } }) </script> </body> </html>
效果圖:
更多文章可以點(diǎn)擊《》學(xué)習閱讀。
關(guān)于vue.js組件的教程,請大家點(diǎn)擊專(zhuān)題進(jìn)行學(xué)習。
更多vue學(xué)習教程請閱讀專(zhuān)題
以上就是本文的全部?jì)热?,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站