vue實現(xiàn)購物車列表
更新時間:2020年06月30日 16:32:08 作者:--新芽-
這篇文章主要為大家詳細介紹了vue實現(xiàn)購物車列表,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)購物車列表的具體代碼,供大家參考,具體內(nèi)容如下
功能:
- 刪除
- 單選 全選
- 增加數(shù)量 減少數(shù)量
- 計算總價 計算數(shù)量
- 搜索
代碼:
<!DOCTYPE html> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> 篩選:<input type="text" v-model="key"> <table border="1" cellspacing="0" cellpadding="10"> <tr> <th> <input type="checkbox" v-model="all" @change="checkAll()" > </th> <th>id</th> <th>書籍名稱</th> <th>出版日期</th> <th>購買價格</th> <th>數(shù)量</th> <th>操作</th> </tr> <tr v-for="(item,index) in flist" :key="item.id"> <td style="text-align: center;"><input type="checkbox" v-model="item.sel" ></td> <td>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.time}}</td> <td>{{item.price|prices}}</td> <td><button @click="item.num--" :disabled="item.num==1">-</button>{{item.num}}<button @click="item.num++">+</button></td> <td><button @click="delItem(item.id)">移除</button></td> </tr> <tr><td colspan="7">總價格:{{total.price|prices}} 選擇數(shù)量:{{total.num}}</td></tr> </table> </div> <script> var vm = new Vue({ el:"#app", data:{ key:"", all:true, list:[ {id:1,name:"小紅書",time:"2018-8",price:188.99,num:1,sel:true}, {id:2,name:"小爛熟",time:"2019-8",price:88.9,num:1,sel:true}, {id:3,name:"小綠樹",time:"2017-5",price:133.00,num:1,sel:true}, {id:4,name:"發(fā)生的樹",time:"2020-1",price:68.80,num:1,sel:true}, {id:5,name:"奧古",time:"2015-4",price:555.50,num:1,sel:true }, ] }, methods:{ delItem(item){ var falg=window.confirm("確定要刪除嗎?"); if(falg){ this.list.splice(item-1,1) } }, checkAll(){ this.list.forEach(item=>item.sel=this.all) } }, watch:{ list:{ handler:function(){ this.all=this.list.every(item=>item.sel) }, deep:true } }, computed:{ total:function(){ var price=0; var num=0; this.list.forEach(item=>{ if(item.sel){ price+=item.num*item.price num+=item.num*1 } }) return ({price,num}) }, flist:function(){ if(this.key===''){return this.list} return this.list.filter(item=>item.name.includes(this.key)) } }, filters:{ prices:function(val,fix=2){ val=val.toFixed(fix) val=""+val return "¥"+val } }, }) </script> </body> </html>
關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學習教程進行學習。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue動態(tài)權(quán)限登錄實現(xiàn)(基于路由與角色)
很多應用都會需要對不同的用戶進行權(quán)限控制,本文主要介紹了Vue動態(tài)權(quán)限登錄實現(xiàn)(基于路由與角色),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05Vue-router 中hash模式和history模式的區(qū)別
這篇文章主要介紹了Vue-router 中hash模式和history模式的區(qū)別,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Vue3使用el-table組件實現(xiàn)分頁、多選以及回顯功能
這篇文章主要介紹了Vue3使用el-table組件實現(xiàn)分頁、多選以及回顯功能,文中通過代碼示例介紹的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-09-09