vue實(shí)現(xiàn)仿淘寶結(jié)賬頁(yè)面實(shí)例代碼
雖然Vue最強(qiáng)大的是組件化開(kāi)發(fā),但是其實(shí)多頁(yè)面開(kāi)發(fā)也蠻適合的。下面小編給大家分享vue實(shí)現(xiàn)仿淘寶結(jié)賬頁(yè)面實(shí)例代碼,具體內(nèi)容大家參考下本文。
這個(gè)demo,是小編基于之前的 vue2.0在table中實(shí)現(xiàn)全選和反選 文章進(jìn)行更新后的demo,主要功能呢,是仿照淘寶頁(yè)面的結(jié)算購(gòu)物車(chē)商品時(shí)自動(dòng)算出合計(jì)價(jià)格的頁(yè)面,具體頁(yè)面效果請(qǐng)看下面的動(dòng)圖:(如果大家發(fā)現(xiàn)有什么問(wèn)題請(qǐng)及時(shí)提出幫小穎改正錯(cuò)誤呦,謝謝啦嘻嘻)
效果圖:
更新后的home.vue
<template> <div class="container"> <div class="checkout-title"> <span>購(gòu)物車(chē)</span> </div> <table class="product_table"> <tbody> <template v-for="(list,index) in table_list"> <tr> <td width="7%" min-width="94px" v-if="index===0"> <input type="checkbox" v-model='checked' @click='checkedAll'> </td> <td width="7%" v-if="index!==0"> <input type="checkbox" v-model='checkList' :value="list.id" @click=checkProductFun(index,$event)> </td> <td width="43%">{{list.product_inf}}</td> <td width="10%" v-if="index===0">{{list.product_price}}</td> <td width="10%" v-if="index!==0">¥{{list.product_price}}</td> <td width="10%" v-if="index===0">{{list.product_quantity}}</td> <td width="10%" v-if="index!==0"> <a class="numbers plus" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="changeMoney(index,-1)">-</a> <input class="txt_number" type="text" v-model="list.product_quantity" size="1" disabled> <a class="numbers reduce" href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="changeMoney(index,1)">+</a> </td> <td width="10%">{{list.total_amount}}</td> <td width="20%" v-if="index===0">編輯</td> <td width="20%" v-if="index!==0"> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="update">修改</a> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="delete">刪除</a> </td> </tr> </template> </tbody> </table> <div class="price_total_bottom"> <div class="price_total_ms"> <label>合計(jì):{{allProductTotal}}</label> <router-link to="/userAddress">結(jié)賬</router-link> </div> </div> </div> </template> <script> import userAddress from './address' export default { components: { userAddress }, data() { return { table_list: [{ 'id': 0, 'product_inf': '商品信息', 'product_price': '商品金額', 'product_quantity': '商品數(shù)量', 'total_amount': '總金額' }, { 'id': '1', 'product_inf': '女士銀手鏈', 'product_price': 100, 'product_quantity': 10, 'total_amount': 1000 }, { 'id': '2', 'product_inf': '女士銀手鐲', 'product_price': 200, 'product_quantity': 5, 'total_amount': 1000 }, { 'id': '3', 'product_inf': '女士銀耳環(huán)', 'product_price': 50, 'product_quantity': 10, 'total_amount': 500 }], checked: false, allProductTotal: null, checkList: ['1', '3'] } }, mounted: function() { var _this = this; // 根據(jù)data中默認(rèn)勾選的checkbox,計(jì)算當(dāng)前勾選的商品總價(jià) _this.allProductTotal = 0; this.checkList.forEach(function(element1, index1) { _this.table_list.forEach(function(element2, index2) { if (element1 == element2.id) { _this.$set(_this.table_list[index2], 'checked', true); _this.allProductTotal += element2.product_price * element2.product_quantity; } }); }); }, methods: { checkedAll: function() { var _this = this; _this.allProductTotal = 0; if (_this.checked) { //實(shí)現(xiàn)反選 _this.checkList = []; _this.table_list.forEach(function(item, index) { if (_this.table_list[index].checked) { _this.table_list[index].checked = false; } }); } else { //實(shí)現(xiàn)全選 _this.checkList = []; _this.table_list.forEach(function(item, index) { if (index > 0) { _this.checkList.push(item.id); if (!_this.table_list[index].checked) { _this.$set(_this.table_list[index], 'checked', true); } else { _this.table_list[index].checked = true; } if (item.checked) { _this.allProductTotal += item.product_price * item.product_quantity; } } }); } }, checkProductFun(index, event) { //根據(jù)checkbox是否勾選,計(jì)算勾選后的商品總價(jià) var _this = this; _this.allProductTotal = 0; if (event.target.checked) { if (!_this.table_list[index].checked) { _this.$set(_this.table_list[index], 'checked', true); } } else { if (_this.table_list[index].checked) { _this.table_list[index].checked = false; } } this.table_list.forEach(function(item, index) { if (item.checked) { _this.allProductTotal += item.product_price * item.product_quantity; } }); }, changeMoney: function(index, way) { if (way > 0) { this.table_list[index].product_quantity++; } else { this.table_list[index].product_quantity--; if (this.table_list[index].product_quantity < 1) { this.table_list[index].product_quantity = 1; } } this.calcTotalPrice(); }, calcTotalPrice: function() { var _this = this; _this.allProductTotal = 0; this.table_list.forEach(function(item, index) { if (index > 0) { //因?yàn)榈谝恍袨楸眍^不需要進(jìn)行計(jì)算 item.total_amount = item.product_price * item.product_quantity; //根據(jù)商品數(shù)量計(jì)算每一個(gè)商品對(duì)應(yīng)的總金額 } if (item.checked) { _this.allProductTotal += item.product_price * item.product_quantity; //根據(jù)是否否選該商品的checkbox,計(jì)算總價(jià) } }); }, }, watch: { //深度 watcher 'checkList': { handler: function(val, oldVal) { if (val.length === this.table_list.length - 1) { this.checked = true; } else { this.checked = false; } }, deep: true } } } </script> <!-- Add "scoped" attribute to limit CSS to this component only --> <style scoped> .container { padding: 69px 0 54px 0; } table { border-collapse: collapse; border-color: transparent; text-align: center; } .product_table, .product_table tbody { width: 100% } .product_table tr:first-child { background: #ece6e6; color: #e66280; font-size: 20px; } .product_table td { border: 1px solid #f3e8e8; height: 62px; line-height: 62px; } .product_table a.update:link, .product_table a.update:visited, .product_table a.update:hover, .product_table a.update:active { color: #1CE24A; } .product_table a.delete:link, .product_table a.delete:visited, .product_table a.delete:hover, .product_table a.delete:active { color: #ffa700; } .product_table .txt_number { text-align: center; } .product_table .numbers { font-weight: bold; } .price_total_bottom { font-size: 20px; padding: 20px 10px; } .price_total_ms { text-align: right; } .price_total_bottom .price_total_ms label { margin-right: 100px; } .price_total_bottom .price_total_ms a { cursor: default; text-align: center; display: inline-block; font-size: 20px; color: #fff; font-weight: bold; width: 220px; height: 54px; line-height: 54px; border: 0; background-color: #f71455; } </style>
總結(jié)
以上所述是小編給大家介紹的vue實(shí)現(xiàn)仿淘寶結(jié)賬頁(yè)面實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
一文帶你了解threejs在vue項(xiàng)目中的基本使用
three.js是一個(gè)用于在Web上創(chuàng)建三維圖形的JavaScript庫(kù),它可以用于創(chuàng)建各種類(lèi)型的三維場(chǎng)景,包括游戲、虛擬現(xiàn)實(shí)、建筑和產(chǎn)品可視化等,下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一文帶你了解threejs在vue項(xiàng)目中的基本使用,需要的朋友可以參考下2023-04-04vue.js踩坑之ref引用細(xì)節(jié)點(diǎn)講解
這篇文章主要介紹了vue.js踩坑之ref引用細(xì)節(jié)點(diǎn)講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03vue單頁(yè)應(yīng)用中如何使用jquery的方法示例
最近在工作中遇到的一個(gè)需求,需要在vue-cli建立的應(yīng)用中引入jquery的方式,通過(guò)查找相關(guān)的資料最終解決了,所以這篇文章主要給大家介紹了關(guān)于在vue單頁(yè)應(yīng)用中如何使用jquery的方法示例,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07使用寶塔面板中Nginx部署前端Vue項(xiàng)目完整步驟
在Kubernetes(K8S)部署前端Vue項(xiàng)目通常會(huì)涉及到使用Nginx作為靜態(tài)資源服務(wù)器的一個(gè)重要部分,這篇文章主要給大家介紹了關(guān)于使用寶塔面板中Nginx部署前端Vue項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2024-10-10VUE中的export default和export使用方法解析
export default和export都能導(dǎo)出一個(gè)模塊里面的常量,函數(shù),文件,模塊等,在其它文件或模塊中通過(guò)import來(lái)導(dǎo)入常量,函數(shù),文件或模塊。但是,在一個(gè)文件或模塊中export,import可以有多個(gè),export default卻只能有一個(gè)。2022-12-12解決element-ui的el-select選擇器的@blur事件失效的坑
這篇文章主要介紹了解決element-ui的el-select選擇器的@blur事件失效的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09