vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車功能
本文實(shí)例為大家分享了vue.js實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車的具體代碼,供大家參考,具體內(nèi)容如下
這次我將給大家?guī)硪粋€(gè)vue.js實(shí)現(xiàn)購(gòu)物車的小項(xiàng)目,如有不足請(qǐng)嚴(yán)厲指出。
購(gòu)物車功能有:全選和選擇部分商品,選中商品總價(jià)計(jì)算,商品移除,以及調(diào)整購(gòu)買數(shù)量等功能。
js主要有以下方法
加函數(shù),減函數(shù),手動(dòng)修改數(shù)量判斷庫存函數(shù),總價(jià)格計(jì)算函數(shù),單選事件,全選事件,一共分為6個(gè)事件
具體效果如下圖
代碼在這里
main.js
'use strict' var app = new Vue({ el: '#app', data: { list: [ { id: 1, name: 'iPhone 7', price: 6188, count: 1, check: true, }, { id: 2, name: 'iPad Pro', price: 5888, count: 1, check: false, }, { id: 3, name: 'MacBook Pro', price: 21488, count: 1, check: true, }, ] }, methods: { remove: function (index) { //移除商品 this.list.splice(index, 1); }, reduce: function (index) { //減少商品 this.list[index].count --; }, add: function (index) { //增加商品 this.list[index].count ++; }, selAll: function () { //商品全選 let isAll = document.querySelector('#all'); if (isAll.checked == true) { this.list.forEach(function(item, index) { item.check = true; }) } else { this.list.forEach(function(item, index) { item.check = false; }) } } }, computed: { totalPrices: function () { //計(jì)算總價(jià) let totalPrices = 0; this.list.forEach(function (val, index) { if (val.check == true) totalPrices += parseFloat(val.price * val.count); }) return totalPrices.toString().replace(/\B(?=(\d{3})+$)/g, ','); //每三位數(shù)中間加一個(gè)‘,' } } })
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="main.css" > </head> <body> <div id="app" v-cloak> <template v-if="list.length"> <table> <thead> <tr> <th>全選<input id="all" @click="selAll" type="checkbox" checked></th> <th>商品名稱</th> <th>商品單價(jià)</th> <th>購(gòu)買數(shù)量</th> <th>操作</th> </tr> </thead> <tbody> <template v-for="(item, index) in list"> <tr> <td> <input type="checkbox" :checked="item.check" @click="item.check = !item.check"> </td> <td> {{ item.name }} </td> <td> {{ item.price }} </td> <td> <button @click="reduce(index)" :disabled="item.count == 1">-</button> {{ item.count }} <button @click="add(index)">+</button> </td> <td> <button @click="remove(index)">移除</button> </td> </tr> </template> </tbody> </table> <div>總價(jià): ¥ {{ totalPrices }}</div> </template> <template v-else> 購(gòu)物車沒有商品 </template> </div> <script src="vue.js"></script> <script src="main.js"></script> </body> </html>
main.css
[v-cloak] { display: none; } #app { width: 500px; margin: 0 auto; } table { width: 100%; border: 1px solid #444; border-collapse: collapse; } th, td { padding: 8px 16px; border: 1px solid #444; text-align: left; } th { background: #89abd3; color: rgb(214, 224, 235); font-weight: 600; white-space: nowrap; }
更多文章可以點(diǎn)擊《Vue.js前端組件學(xué)習(xí)教程》學(xué)習(xí)閱讀。
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- Vuejs實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- Vue實(shí)現(xiàn)購(gòu)物車詳情頁面的方法
- vue實(shí)現(xiàn)購(gòu)物車小案例
- vue 實(shí)現(xiàn)購(gòu)物車總價(jià)計(jì)算
- vue實(shí)現(xiàn)購(gòu)物車功能(親測(cè)可用)
- 前端Vue學(xué)習(xí)之購(gòu)物車項(xiàng)目實(shí)戰(zhàn)記錄
相關(guān)文章
vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能
這篇文章主要介紹了vue 實(shí)現(xiàn)剪裁圖片并上傳服務(wù)器功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-03-03Vue響應(yīng)式原理模擬實(shí)現(xiàn)原理探究
這篇文章主要介紹了Vue響應(yīng)式原理,響應(yīng)式就是當(dāng)對(duì)象本身(對(duì)象的增刪值)或者對(duì)象屬性(重新賦值)發(fā)生了改變的時(shí)候,就會(huì)運(yùn)行一些函數(shù),最常見的示render函數(shù)2022-09-09vuex實(shí)現(xiàn)簡(jiǎn)易計(jì)數(shù)器
這篇文章主要為大家詳細(xì)介紹了vuex實(shí)現(xiàn)一個(gè)簡(jiǎn)易計(jì)數(shù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10vue3應(yīng)用elementPlus table并滾動(dòng)顯示問題
這篇文章主要介紹了vue3應(yīng)用elementPlus table并滾動(dòng)顯示問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12解決vue中使用Axios調(diào)用接口時(shí)出現(xiàn)的ie數(shù)據(jù)處理問題
今天小編就為大家分享一篇解決vue中使用Axios調(diào)用接口時(shí)出現(xiàn)的ie數(shù)據(jù)處理問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明(多種情況分析)
這篇文章主要介紹了vue3.0找不到模塊“./App.vue”或其相應(yīng)的類型聲明,報(bào)錯(cuò)原因是typescript?只能理解?.ts?文件,無法理解?.vue文件,本文通過多種情況分析給大家詳細(xì)講解,需要的朋友可以參考下2023-01-01