Vue實(shí)現(xiàn)購(gòu)物車(chē)基本功能
Vue實(shí)現(xiàn)購(gòu)物車(chē)商品 加、減、單選、全選、刪除、價(jià)格更新等功能
Dome和Vue代碼
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>商城</title> <link rel="stylesheet" href="./css/common.css" > <link rel="stylesheet" href="./css/cart.css" > </head> <body> <div id="main"> <div class="container"> <div id="cart"> <h1>購(gòu)物車(chē)</h1> <form action="#" method="post"> <table class="form"> <thead> <tr> <th width="8%">選擇</th> <th width="50%">商品</th> <th width="13%">單價(jià)(元)</th> <th width="15%">數(shù)量</th> <th width="14%">金額(元)</th> </tr> </thead> <tbody id="cart-goods-list"> <tr v-for="cart in productList"> <td> <input type="checkbox" name="good-id" :value="1" v-model="cart.select"> </td> <td class="goods"> <div class="goods-image"> <img v-bind:src="cart.pro_img"> </div> <div class="goods-information"> <h3>{{cart.pro_name}}</h3> <ul> <li>{{cart.pro_purity}}</li> <li>{{cart.pro_service}}</li> </ul> </div> </td> <td> <span class="price">¥<em class="price-em">{{cart.pro_price.toFixed(2)}}</em></span> </td> <td> <div class="combo"> <input type="button" name="minus" value="-" class="combo-minus" @click="cart.pro_num<2?cart.pro_num=1:cart.pro_num--"> <input type="text" name="count" v-model.number="cart.pro_num" class="combo-value"> <input type="button" name="plus" value="+" class="combo-plus" v-on:click="cart.pro_num++"> </div> </td> <td> <strong class="amount">¥<em class="amount-em">{{(cart.pro_price*cart.pro_num).toFixed(2)}}</em></strong> </td> </tr> </tbody> <tfoot v-show="productList.length!=0"> <tr> <td colspan="2"> <label> <input type="checkbox" name="all" v-model="isSelectAll"> <span @click="">全選</span> </label> <a href="#" rel="external nofollow" id="cart-delete" @click="del()">刪除</a> </td> <td colspan="3"> <span>合計(jì):</span> <strong id="total-amount">¥<em id="total-amount-em">{{getTotal}}</em></strong> <input type="submit" value="立即結(jié)算" id="settlement"> </td> </tr> </tfoot> </table> </form> <div v-show="productList.length===0"> 購(gòu)物車(chē)還是空的哦~快來(lái)購(gòu)物吧~ </div> </div> </div> </div> </body> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el:"#cart", data:{ productList:[ { 'pro_name': 'Dior 迪奧 花漾甜心小姐 女士淡香水',//產(chǎn)品名稱(chēng) 'pro_purity': '50ml',//規(guī)格 'pro_service': "不支持7天無(wú)理由退貨",//售后 'pro_num': 1,//數(shù)量 'pro_img': 'img/1.jpg',//圖片鏈接 'pro_price': 498,//單價(jià), 'select': true ,//選中狀態(tài) }, { 'pro_name': '迪奧(dior)口紅CD烈艷藍(lán)金唇膏',//產(chǎn)品名稱(chēng) 'pro_purity': '350g',//規(guī)格 'pro_service': "不支持7天無(wú)理由退貨",//售后 'pro_num': 1,//數(shù)量 'pro_img': 'img/2.jpg',//圖片鏈接 'pro_price': 268,//單價(jià) 'select': true //選中狀態(tài) }, { 'pro_name': 'LANCÔME 蘭蔻 嫩肌活膚精華肌底液',//產(chǎn)品名稱(chēng) 'pro_purity': '50ml',//規(guī)格 'pro_service': "不支持7天無(wú)理由退貨",//售后 'pro_num': 1,//數(shù)量 'pro_img': 'img/3.jpg',//圖片鏈接 'pro_price': 598,//單價(jià) 'select': true //選中狀態(tài) } ] }, computed:{ getTotal:function(){ var newArr=this.productList.filter(function(val){ return val.select===true; }) var price=0; for(var i=0;i<newArr.length;i++){ price+=newArr[i].pro_num*newArr[i].pro_price } return price.toFixed(2) }, isSelectAll:{ get:function(){ return this.productList.every(function(val){ return val.select===true; }) }, set:function(newValue){ for(var i=0;i<this.productList.length;i++){ this.productList[i].select=newValue; } } } }, methods:{ del:function(){ if(confirm("確定要?jiǎng)h除嗎")){ var newArr=[]; for(var i=0;i<this.productList.length;i++){ if(this.productList[i].select===false){ newArr.push(this.productList[i]) } } this.productList=newArr; } } } }) </script> </html>
購(gòu)物車(chē)部分CSS代碼
@charset "utf-8"; #main{ padding: 30px 0px; } #cart{ background: #FFFFFF; padding: 40px; } #cart h1{ line-height: 40px; padding: 0px 0px 10px 0px; } table.form{ border-collapse: collapse; empty-cells: show; margin: 20px 0px; padding: 0px; table-layout: fixed; width: 100%; } table.form th, table.form td{ border-bottom: 1px solid #DDDDDD; padding: 15px 10px; text-align: left; } table.form{ border-top: 3px solid #DDDDDD; } .goods .goods-image img{ border: 1px solid #DDDDDD; float: left; height: 100px; margin: 0px 20px 0px 0px; } .goods .goods-information{ float: left; } .goods .goods-information ul{ color: #666666; font-size: 12px; line-height: 20px; margin:10px 0px 0px 0px; } .price, .amount, #total-amount{ color: #E00000; } #total-amount{ font-size: 22px; } .price em, .amount em, #total-amount em{ font-style: normal; } .combo .combo-minus, .combo .combo-value, .combo .combo-plus{ background: #FFFFFF; border: 1px solid #DDDDDD; color: #333333; float: left; font-weight: bold; margin: 0px; outline: none; text-align: center; } .combo .combo-minus, .combo .combo-plus{ font-size: 16px; height: 26px; line-height: 26px; padding: 0px; width: 24px; } .combo .combo-value{ border-left: none; border-right: none; height: 20px; line-height: 20px; padding: 2px; width: 40px; } #cart-delete{ margin-left: 20px; } #settlement{ background: #E00000; border: none; color: #FFFFFF; float: right; font-size: 16px; height: 40px; line-height: 40px; margin: 0px; outline: none; padding: 0px; width: 160px; }
注:CSS樣式代碼由于太多上面沒(méi)有給全,只給了主要代碼。小伙伴們可以根據(jù)實(shí)際情況修改樣式
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專(zhuān)題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
更多vue學(xué)習(xí)教程請(qǐng)閱讀專(zhuān)題《vue實(shí)戰(zhàn)教程》
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue+vant實(shí)現(xiàn)購(gòu)物車(chē)全選和反選功能
- Vuex實(shí)現(xiàn)購(gòu)物車(chē)小功能
- vue實(shí)現(xiàn)購(gòu)物車(chē)列表
- vuex實(shí)現(xiàn)購(gòu)物車(chē)的增加減少移除
- vuex實(shí)現(xiàn)購(gòu)物車(chē)功能
- vuejs手把手教你寫(xiě)一個(gè)完整的購(gòu)物車(chē)實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車(chē)功能
- Vue實(shí)現(xiàn)購(gòu)物車(chē)功能
- vue實(shí)現(xiàn)商城購(gòu)物車(chē)功能
- Vue實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車(chē)功能
相關(guān)文章
vue+antDesign實(shí)現(xiàn)樹(shù)形數(shù)據(jù)展示及勾選聯(lián)動(dòng)
本文主要介紹了vue+antDesign實(shí)現(xiàn)樹(shù)形數(shù)據(jù)展示及勾選聯(lián)動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02vue調(diào)試工具vue-devtools的安裝全過(guò)程
這篇文章主要介紹了vue調(diào)試工具vue-devtools的安裝全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06vue3中封裝axios請(qǐng)求最新實(shí)現(xiàn)步驟
這篇文章主要給大家介紹了關(guān)于vue3中封裝axios請(qǐng)求的最新實(shí)現(xiàn)步驟,在Vue 3中可以通過(guò)封裝axios來(lái)實(shí)現(xiàn)接口的統(tǒng)一管理和調(diào)用,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-04-04vue 本地環(huán)境跨域請(qǐng)求proxyTable的方法
今天小編就為大家分享一篇vue 本地環(huán)境跨域請(qǐng)求proxyTable的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue集成openlayers加載geojson并實(shí)現(xiàn)點(diǎn)擊彈窗教程
這篇文章主要為大家詳細(xì)介紹了vue集成openlayers加載geojson并實(shí)現(xiàn)點(diǎn)擊彈窗教程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過(guò)程
這篇文章主要給大家介紹了關(guān)于Vue項(xiàng)目中大文件切片上傳實(shí)現(xiàn)秒傳與斷點(diǎn)續(xù)傳的詳細(xì)實(shí)現(xiàn)過(guò)程, 在開(kāi)發(fā)中,如果上傳的文件過(guò)大,可以考慮分片上傳,分片就是說(shuō)將文件拆分來(lái)進(jìn)行上傳,將各個(gè)文件的切片傳遞給后臺(tái),然后后臺(tái)再進(jìn)行合并,需要的朋友可以參考下2023-08-08從Element日期組件源碼中學(xué)到的兩個(gè)工具方法技巧
這篇文章主要介紹了從Element日期組件源碼中學(xué)到的兩個(gè)工具方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08