1.引入vue
html<script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
2.设置高亮部分的样式
html<style> table tr{ text-align: center; } .skyblue{ background-color: skyblue; } </style>
3.设置body的基本样式
html<div id="app" > <table border="1px solid black" style="width:500px;" > <thead> <tr > <td>选中</td> <td>图片</td> <td>单价</td> <td>个数</td> <td>小计</td> <td>操作</td> </tr> </thead> <tbody v-if="list.length>0"> <tr v-for="(item,index) in list" :key="item.id" :class="{skyblue:item.isCheck}"> <td><input type="checkbox" v-model="item.isCheck"></td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td> <button :disabled="item.num<=1" @click="sub(item.id)">-</button> {{item.num}} <button @click="add(item.id)">+</button></td> <td>{{item.price*item.num}}</td> <td> <button @click="del(item.id)">del</button> </td> </tr> </tbody> <tbody v-else> <tr > <td colspan="6">空空如也</td> </tr> </tbody> <tfoot> <tr> <td colspan="6" > 全选<input type="checkbox" v-model="isAll"> 总价:¥ {{jiage}} <button @click="j()">结算{{zongshu}}</button> </td> </tr> </tfoot> </table> </div>
4.设置js的逻辑
javascript<script> const ddd =[{id:1,name:"苹果",price:30,num:3,isCheck:true}, {id:2,name:"火龙果",price:50,num:10,isCheck:true}, {id:3,name:"香蕉",price:20,num:300,isCheck:false}, {id:4,name:"西瓜",price:100,num:20,isCheck:true}, ] const app=new Vue({ el:'#app', data:{ list:JSON.parse(localStorage.getItem("list"))||ddd }, methods:{ del(a){ this.list = this.list.filter(item => item.id !== a ) }, add(a){ const l=this.list.find(item=>item.id ===a) // console.log(l) l.num++ }, sub(a){ const l=this.list.find(item=>item.id ==a) l.num-- }, j(){ var s= this.list.reduce((sum,item)=>item.isCheck?sum+item.num:sum,0) var s1= this.list.reduce((sum,item)=>item.isCheck?sum+item.num*item.price:sum,0) alert("您共购买"+s+"个商品,花了"+s1+"元") } }, computed:{ isAll:{ get(){ return this.list.every(a=>a.isCheck===true)}, set(a){ this.list.forEach(b=>b.isCheck=a) } }, jiage(){ return this.list.reduce((a,b)=>(b.isCheck?a+b.num*b.price:a),0) }, zongshu(){ return this.list.reduce((a,b)=>(b.isCheck?a+b.num:a),0) } }, watch:{ list:{ deep:true,//深度监视 handler(newValue){ localStorage.setItem("list",JSON.stringify(newValue)) } } , } }) </script>
完整代码如下
html<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>水果购物车</title> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script> <style> table tr{ text-align: center; } .skyblue{ background-color: skyblue; } </style> </head> <body> <div id="app" > <table border="1px solid black" style="width:500px;" > <thead> <tr > <td>选中</td> <td>图片</td> <td>单价</td> <td>个数</td> <td>小计</td> <td>操作</td> </tr> </thead> <tbody v-if="list.length>0"> <tr v-for="(item,index) in list" :key="item.id" :class="{skyblue:item.isCheck}"> <td><input type="checkbox" v-model="item.isCheck"></td> <td>{{item.name}}</td> <td>{{item.price}}</td> <td> <button :disabled="item.num<=1" @click="sub(item.id)">-</button> {{item.num}} <button @click="add(item.id)">+</button></td> <td>{{item.price*item.num}}</td> <td> <button @click="del(item.id)">del</button> </td> </tr> </tbody> <tbody v-else> <tr > <td colspan="6">空空如也</td> </tr> </tbody> <tfoot> <tr> <td colspan="6" > 全选<input type="checkbox" v-model="isAll"> 总价:¥ {{jiage}} <button @click="j()">结算{{zongshu}}</button> </td> </tr> </tfoot> </table> </div> <script> const ddd =[{id:1,name:"苹果",price:30,num:3,isCheck:true}, {id:2,name:"火龙果",price:50,num:10,isCheck:true}, {id:3,name:"香蕉",price:20,num:300,isCheck:false}, {id:4,name:"西瓜",price:100,num:20,isCheck:true}, ] const app=new Vue({ el:'#app', data:{ list:JSON.parse(localStorage.getItem("list"))||ddd }, methods:{ del(a){ this.list = this.list.filter(item => item.id !== a ) }, add(a){ const l=this.list.find(item=>item.id ===a) // console.log(l) l.num++ }, sub(a){ const l=this.list.find(item=>item.id ==a) l.num-- }, j(){ var s= this.list.reduce((sum,item)=>item.isCheck?sum+item.num:sum,0) var s1= this.list.reduce((sum,item)=>item.isCheck?sum+item.num*item.price:sum,0) alert("您共购买"+s+"个商品,花了"+s1+"元") } }, computed:{ isAll:{ get(){ return this.list.every(a=>a.isCheck===true)}, set(a){ this.list.forEach(b=>b.isCheck=a) } }, jiage(){ return this.list.reduce((a,b)=>(b.isCheck?a+b.num*b.price:a),0) }, zongshu(){ return this.list.reduce((a,b)=>(b.isCheck?a+b.num:a),0) } }, watch:{ list:{ deep:true,//深度监视 handler(newValue){ localStorage.setItem("list",JSON.stringify(newValue)) } } , } }) </script> </body> </html>
vue购物车实战
流殇꧂2024-03-02 14:52
相关推荐
计算机-秋大田7 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解Yaml417 小时前
Spring Boot 与 Vue 共筑二手书籍交易卓越平台清灵xmf21 小时前
在 Vue 中实现与优化轮询技术琴~~2 天前
前端根据后端返回的文本流逐个展示文本内容程序员徐师兄2 天前
基于 JavaWeb 的宠物商城系统(附源码,文档)shareloke2 天前
让Erupt框架支持.vue文件做自定义页面模版你白勺男孩TT3 天前
Vue项目中点击按钮后浏览器屏幕变黑,再次点击恢复的解决方法虞泽3 天前
鸢尾博客项目开源工业互联网专业3 天前
Python毕业设计选题:基于django+vue的4S店客户管理系统麦麦大数据3 天前
vue+django+neo4j航班智能问答知识图谱可视化系统