电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

vue實現購物車的小練習

瀏覽:6日期:2022-10-17 13:58:40

今天從網上找了一個購物車的小例子,照著敲了一下,收獲不少。下面的用一個小動圖展示一下成果:

vue實現購物車的小練習

接下來上代碼:

<!DOCTYPE html><html> <head> <meta charset='UTF-8'> <link href='http://www.hdgsjgj.cn/bcjs/css/shoppingcart.css' rel='stylesheet' type='text/css' /> <title></title> </head> <body> <div id='app'> <h2>購物清單</h2> <div class='nav'> <div><span v-bind: @click='allSelect(ifAllselect)'></span>全選</div> <div>商品</div> <div>數量</div> <div>單價(元)</div> <div>金額(元)</div> <div>操作</div> </div> <table class='goods'> <tbody> <tr v-for='(item,index) in goods'> <td><span v-bind: @click='item.isSelect=!item.isSelect'></span></td> <td> <div > <img v-bind:src=’item.gimg’ /> <div> <h3>{{item.gname}}</h3> <span>{{item.gbrand}} &nbsp;&nbsp;{{item.gplace}}</span><br /> <span>{{item.gpurity}} &nbsp;&nbsp;{{item.gminnum}}</span><br /> <span>{{item.gstore}}</span> </div> </div> </td> <td><input type='number' v-model='item.gnum' min='0'/></td> <td><span>¥{{item.gprice}}</span></td> <td><span>¥{{item.gprice*item.gnum}}</span></td> <td><button @click='deleteSingle(index)'>刪除</button></td> </tr> </tbody> </table> <div class='footer'> <button @click='deleteSel'>刪除所選商品</button> <button>繼續購物</button> <span><span>{{getTotal.num}}</span>件商品(不含運費)總計:<span>¥{{getTotal.allprice}}</span></span> <button>去結算</button> </div> </div> </div> </body> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script> <script> var vm=new Vue({ el:’#app’, data:{ goods:[ {gname:’佳潔士美白牙膏’, gbrand:’品牌:佳潔士’, gplace:’產地:上海’, gpurity:’規格:120g’, gminnum:’起訂量:10件’, gstore:’倉庫地:上海滄海倉儲’, gprice:’800’, gimg:’img/good.jpg’, gnum:’3’, isSelect:false, }, {gname:’佳潔士美白牙膏’, gbrand:’品牌:佳潔士’, gplace:’產地:上海’, gpurity:’規格:120g’, gminnum:’起訂量:10件’, gstore:’倉庫地:上海滄海倉儲’, gimg:’img/good.jpg’, gprice:’400’, gnum:’5’, isSelect:false, } ] }, computed:{ //實時判斷是否全選 ifAllselect:function(){ var all; for(var i=0;i<this.goods.length;i++){ if(this.goods[i].isSelect==false){ all=false; break; } all=true; } return all; }, //獲取總件數和總金額數 getTotal:function(){ var num= 0 ; var allprice=0; for(var i=0;i<this.goods.length;i++){ if(this.goods[i].isSelect==true){ num=parseInt(num)+parseInt(this.goods[i].gnum); allprice=allprice+this.goods[i].gnum*this.goods[i].gprice; } } return{num:num,allprice:allprice} } }, methods:{ //全選或者取消全部選中 allSelect:function(ifAllselect){ for(var i=0;i<this.goods.length;i++){ this.goods[i].isSelect=!ifAllselect; } }, //刪除單個商品 deleteSingle:function(index){ if(this.goods[index].isSelect==true) this.goods.splice(index,1); else alert(’請選擇您要刪除的商品!’); }, //刪除選中的商品 deleteSel:function(){ this.goods=this.goods.filter(function(item){return !item.isSelect}) } } }) </script></html>

*{ padding: 0; margin: 0;}html,body{ width: 100%; overflow-x:hidden ;}#app{ width: 90%; margin: 50px auto; border: 1px solid gainsboro; border-top: none;}h2{ display: block; border-top: 4px solid dodgerblue; font-size: 17px; padding-left: 20px; line-height: 50px; color: dodgerblue;}.nav{ width: 100%; height: 40px; border:1px solid gainsboro ; border-left:none ; border-right: none;}.nav>div{ height: 100%; float: left; display: flex; justify-content: center; align-items: center;}.nav div:nth-child(1){ width: 15%;}.nav div:nth-child(2){ width:37%;}.nav div:nth-child(3){ width: 12%;}.nav div:nth-child(4){ width: 12%;}.nav div:nth-child(5){ width: 12%;}.nav div:nth-child(6){ width: 12%; }.noselected{ display: inline-block; width: 17px; height:17px; margin-right: 5px; background: url(../img/nocheck.png) no-repeat; background-size: contain;}.goods{ width: 100%; height: auto;}.goods tr{ width: 100%;}.goods tr td{ padding: 20px 0;}.goods tr>td:nth-child(1){ width: 17%; text-align: center;}.goods tr>td:nth-child(2){ width: 35%;}.goods tr>td:nth-child(3){ width: 12%; text-align: center;}.goods tr>td:nth-child(4){ width: 12%; text-align: center;}.goods tr>td:nth-child(5){ width: 12%; text-align: center;}.goods tr>td:nth-child(6){ width: 12%; text-align: center;}.good{ width: 100%; display: flex; align-items: center;}.good img{ width:120px; height: 120px; float: left; border: 2px solid gainsboro; margin-right: 30px;}.good>div{ font-size: 13px; line-height: 20px;}.good>div h3{ font-size: 11px; margin-bottom: 5px;}.goods input[type=number]{ width: 50px;}.goods tr td:nth-child(4),.goods tr td:nth-child(5){ color: red;}button{ cursor: pointer; border: none; outline: none; background-color: white;}.footer{ display: flex; align-items: center; width: 100%; height: 50px; background-color: #F7F7F7; position: relative;}.footer button{ border: none; background-color: #F7F7F7; font-size: 15px;}.footer button:nth-child(1){ margin-left: 30px;}.footer button:nth-child(2){ margin-left: 60px;}.footer button:nth-child(4){ height: 100%; position: absolute; right: 0; padding:0 20px; background-color: orange;}.footer>span{ position: absolute; right: 100px;}.footer>span span{ color: red;}.selected{ background: url(../img/check.png) no-repeat; background-size: contain;}

以上為所有的html和css文件代碼。

【總結】

1、computed:此處用computed主要有兩個作用。一是判斷是否全選。如果全選則添加selected這一class,如果沒有則不添加;二是計算選擇的總商品數和總金額。當用戶更改商品數量時,總商品數和總金額也隨之改變。

2、return返回兩個值:第一次接觸function里邊return的值是兩個這種情況,這種要通過對象的屬性訪問方法。例如:

function add(a,b){ var sum; var sub return{ sum:a+b, sub:a-b } }var obj = add(5,2);console.log(obj.sum);console.log(obj.sub);

3、js的數組方法filter():

filter() 方法創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。

注意: filter() 不會對空數組進行檢測。注意: filter() 不會改變原始數組。

array.filter(function(currentValue,index,arr), thisValue) currentValue: 必須。當前元素的值 index: 可選。當前元素的索引值 arr:可選。當前元素屬于的數組對象 thisValue:可選。對象作為該執行回調時使用,傳遞給函數,用作 “this” 的值。如果省略了 thisValue ,“this” 的值為 “undefined”

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | C形臂_动态平板DR_动态平板胃肠机生产厂家制造商-普爱医疗 | 吨袋包装机|吨包秤|吨包机|集装袋包装机-烟台华恩科技 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 小区健身器材_户外健身器材_室外健身器材_公园健身路径-沧州浩然体育器材有限公司 | 消电检公司,消电检价格,北京消电检报告-北京设施检测公司-亿杰(北京)消防工程有限公司 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 药品仓库用除湿机-变电站用防爆空调-油漆房用防爆空调-杭州特奥环保科技有限公司 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | 扒渣机,铁水扒渣机,钢水扒渣机,铁水捞渣机,钢水捞渣机-烟台盛利达工程技术有限公司 | 深圳货架厂家_金丽声精品货架_广东金丽声展示设备有限公司官网 | 维泰克Veertek-锂电池微短路检测_锂电池腐蚀检测_锂电池漏液检测 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 企业VI设计_LOGO设计公司_品牌商标设计_【北京美研】 | 微型驱动系统解决方案-深圳市兆威机电股份有限公司 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 陕西鹏展科技有限公司| 湖南长沙商标注册专利申请,长沙公司注册代理记账首选美创! | 液压升降货梯_导轨式升降货梯厂家_升降货梯厂家-河南东圣升降设备有限公司 | 二维运动混料机,加热型混料机,干粉混料机-南京腾阳干燥设备厂 | 黑龙江「京科脑康」医院-哈尔滨失眠医院_哈尔滨治疗抑郁症医院_哈尔滨精神心理医院 | 便携式高压氧舱-微压氧舱-核生化洗消系统-公众洗消站-洗消帐篷-北京利盟救援 | 宿松新闻网 宿松网|宿松在线|宿松门户|安徽宿松(直管县)|宿松新闻综合网站|宿松官方新闻发布 | 北京晚会活动策划|北京节目录制后期剪辑|北京演播厅出租租赁-北京龙视星光文化传媒有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | 氧化铁红厂家-淄博宗昂化工 | 工装定制/做厂家/公司_工装订做/制价格/费用-北京圣达信工装 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 杭州顺源过滤机械有限公司官网-压滤机_板框压滤机_厢式隔膜压滤机厂家 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 蒸汽吸附分析仪-进口水分活度仪|康宝百科 | 超声波反应釜【百科】-以马内利仪器 | 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | 广东西屋电气有限公司-广东西屋电气有限公司 | 闸阀_截止阀_止回阀「生产厂家」-上海卡比阀门有限公司 | 高博医疗集团上海阿特蒙医院| 防水套管-柔性防水套管-刚性防水套管-上海执品管件有限公司 |