Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
開(kāi)始更新前端框架Vue.JS的相關(guān)博客。
功能概述
學(xué)習(xí)了Vue.JS的一些基礎(chǔ)知識(shí),現(xiàn)在利用指令、數(shù)據(jù)綁定這些基礎(chǔ)知識(shí)開(kāi)發(fā)一個(gè)簡(jiǎn)單的購(gòu)物車(chē)功能。功能要點(diǎn)如下:
(1)展示商品的名稱(chēng)、單價(jià)和數(shù)量;
(2)商品的數(shù)量可以增加和減少;
(3)購(gòu)物車(chē)的總價(jià)要實(shí)時(shí)更新,即數(shù)量發(fā)生變動(dòng),總價(jià)也要相應(yīng)的改變;
(4)商品可以從購(gòu)物車(chē)中移除;
(5)具有選擇功能,只計(jì)算選中的商品的總價(jià)。
上一張截圖,如下:
代碼
代碼分成三部分,分別是HTML、JS、CSS。關(guān)鍵的是HTML和JS。
HTML
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue 購(gòu)物車(chē)</title> <script src="../js/vue.min.js"></script> <link href="../css/cart.css" rel="stylesheet"> </head> <body> <div id="app" v-cloak> <template v-if="list.length"> <table> <thead> <tr> <th><input type="checkbox" v-on:click="swapCheck" v-model="checked"></th> <th>商品名稱(chēng)</th> <th>商品單價(jià)</th> <th>商品數(shù)量</th> <th>操作</th> </tr> </thead> <tbody> <tr v-for="(item,index) in list"> <td><input type="checkbox" v-model="selectList" :id="item.id" :value="index" name="selectList" ></td> <td>{{ item.name }}</td> <td>{{ item.price }}</td> <td> <button @click="handleReduce(index)" :disabled="item.count === 1">-</button> {{ item.count }} <button @click="handleAdd(index)">+</button> </td> <td><button @click="handleRemove(index)">移除</button></td> </tr> </tbody> </table> <div>總價(jià):¥ {{ totalPrice }}</div> </template> <div v-else>購(gòu)物車(chē)為空!</div> </div> <script src="../js/cart.js"></script> </body> </html>
JS
var app = new Vue({ el:'#app', data:{ list:[ { id:1, name:'iPhone 8', price:8888, count:1 }, { id:2, name:'Huwei Mate10', price:6666, count:1 }, { id:3, name:'Lenovo', price:6588, count:1 } ], selectList:[], checked:false }, computed:{ totalPrice:function(){ var total = 0; for(var i = 0,len = this.selectList.length;i < len;i++){ var index = this.selectList[i]; var item = this.list[index]; if(item){ total += item.price * item.count; } else{ continue; } } return total.toString().replace(/\B(?=(\d{3})+$)/g,','); } }, methods:{ handleReduce:function(index){ var item = this.list[index]; if(item.count < 2){ return; } item.count--; }, handleAdd:function(index){ var item = this.list[index]; item.count++; }, handleRemove:function(index){ this.list.splice(index,1); }, swapCheck:function(){ var selectList = document.getElementsByName('selectList'); var len = selectList.length; if(this.checked){ for(var i = 0;i < len;i++){ var item = selectList[i]; item.checked = false; } this.checked = false; this.selectList = []; } else{ for(i = 0;i < len;i++){ item = selectList[i]; if(item.checked === false){ item.checked = true; this.selectList.push(selectList[i].value); } } this.checked = true; } } } });
CSS
[v-cloak]{ display: none; } table{ border: 1px solid black; border-collapse: collapse; border-spacing: 0; empty-cells: show; } th,td{ padding: 8px 16px; border:1px solid black; text-align: center; } th{ background-color: gray; }
關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
- Vue實(shí)現(xiàn)購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)商城購(gòu)物車(chē)功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能示例
- Vue實(shí)現(xiàn)購(gòu)物車(chē)詳情頁(yè)面的方法
- vue實(shí)現(xiàn)購(gòu)物車(chē)小案例
- vue 實(shí)現(xiàn)購(gòu)物車(chē)總價(jià)計(jì)算
- vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)購(gòu)物車(chē)功能(親測(cè)可用)
- 前端Vue學(xué)習(xí)之購(gòu)物車(chē)項(xiàng)目實(shí)戰(zhàn)記錄
相關(guān)文章
vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解
這篇文章主要介紹了vuex中數(shù)據(jù)持久化插件vuex-persistedstate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue.set() this.$set()引發(fā)的視圖更新思考及注意事項(xiàng)
this.$set()和Vue.set()本質(zhì)方法一樣,前者可以用在methods中使用。這篇文章主要介紹了Vue.set() this.$set()引發(fā)的視圖更新思考及注意事項(xiàng),需要的朋友可以參考下2018-08-08vue3圖片剪裁插件vue-img-cutter使用小結(jié)
Vue.js是一款流行的JavaScript前端框架,很受用戶(hù)喜愛(ài),這篇文章主要介紹了vue3圖片剪裁插件vue-img-cutter使用小結(jié),本文結(jié)合示例代碼講解的非常詳細(xì),感興趣的朋友一起看看吧2024-01-01如何測(cè)量vue應(yīng)用運(yùn)行時(shí)的性能
這篇文章主要介紹了如何測(cè)量vue應(yīng)用運(yùn)行時(shí)的性能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,,需要的朋友可以參考下2019-06-06Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法
這篇文章主要介紹了Element實(shí)現(xiàn)表格嵌套、多個(gè)表格共用一個(gè)表頭的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Vue 路由 過(guò)渡動(dòng)效 數(shù)據(jù)獲取方法
這篇文章主要介紹了Vue 路由 過(guò)渡動(dòng)效 數(shù)據(jù)獲取方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07