• vue购物车实战


    1.引入vue

    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js">script> 

    2.设置高亮部分的样式

    1. <style>
    2. table tr{
    3. text-align: center;
    4. }
    5. .skyblue{
    6. background-color: skyblue;
    7. }
    8. style>

    3.设置body的基本样式

    1. <div id="app" >
    2. <table border="1px solid black" style="width:500px;" >
    3. <thead>
    4. <tr >
    5. <td>选中td>
    6. <td>图片td>
    7. <td>单价td>
    8. <td>个数td>
    9. <td>小计td>
    10. <td>操作td>
    11. tr>
    12. thead>
    13. <tbody v-if="list.length>0">
    14. <tr v-for="(item,index) in list" :key="item.id" :class="{skyblue:item.isCheck}">
    15. <td><input type="checkbox" v-model="item.isCheck">td>
    16. <td>{{item.name}}td>
    17. <td>{{item.price}}td>
    18. <td>
    19. <button :disabled="item.num<=1" @click="sub(item.id)">-button>
    20. {{item.num}}
    21. <button @click="add(item.id)">+button>td>
    22. <td>{{item.price*item.num}}td>
    23. <td> <button @click="del(item.id)">delbutton> td>
    24. tr>
    25. tbody>
    26. <tbody v-else>
    27. <tr >
    28. <td colspan="6">空空如也td>
    29. tr>
    30. tbody>
    31. <tfoot>
    32. <tr>
    33. <td colspan="6" >
    34. 全选<input type="checkbox" v-model="isAll">
    35. 总价:¥ {{jiage}}
    36. <button @click="j()">结算{{zongshu}}button>
    37. td>
    38. tr>
    39. tfoot>
    40. table>
    41. div>

    4.设置js的逻辑

    完整代码如下

    1. html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <title>水果购物车title>
    6. <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js">script>
    7. <style>
    8. table tr{
    9. text-align: center;
    10. }
    11. .skyblue{
    12. background-color: skyblue;
    13. }
    14. style>
    15. head>
    16. <body>
    17. <div id="app" >
    18. <table border="1px solid black" style="width:500px;" >
    19. <thead>
    20. <tr >
    21. <td>选中td>
    22. <td>图片td>
    23. <td>单价td>
    24. <td>个数td>
    25. <td>小计td>
    26. <td>操作td>
    27. tr>
    28. thead>
    29. <tbody v-if="list.length>0">
    30. <tr v-for="(item,index) in list" :key="item.id" :class="{skyblue:item.isCheck}">
    31. <td><input type="checkbox" v-model="item.isCheck">td>
    32. <td>{{item.name}}td>
    33. <td>{{item.price}}td>
    34. <td>
    35. <button :disabled="item.num<=1" @click="sub(item.id)">-button>
    36. {{item.num}}
    37. <button @click="add(item.id)">+button>td>
    38. <td>{{item.price*item.num}}td>
    39. <td> <button @click="del(item.id)">delbutton> td>
    40. tr>
    41. tbody>
    42. <tbody v-else>
    43. <tr >
    44. <td colspan="6">空空如也td>
    45. tr>
    46. tbody>
    47. <tfoot>
    48. <tr>
    49. <td colspan="6" >
    50. 全选<input type="checkbox" v-model="isAll">
    51. 总价:¥ {{jiage}}
    52. <button @click="j()">结算{{zongshu}}button>
    53. td>
    54. tr>
    55. tfoot>
    56. table>
    57. div>
    58. <script>
    59. const ddd =[{id:1,name:"苹果",price:30,num:3,isCheck:true},
    60. {id:2,name:"火龙果",price:50,num:10,isCheck:true},
    61. {id:3,name:"香蕉",price:20,num:300,isCheck:false},
    62. {id:4,name:"西瓜",price:100,num:20,isCheck:true},
    63. ]
    64. const app=new Vue({
    65. el:'#app',
    66. data:{
    67. list:JSON.parse(localStorage.getItem("list"))||ddd
    68. },
    69. methods:{
    70. del(a){
    71. this.list = this.list.filter(item => item.id !== a )
    72. },
    73. add(a){
    74. const l=this.list.find(item=>item.id ===a)
    75. // console.log(l)
    76. l.num++
    77. },
    78. sub(a){
    79. const l=this.list.find(item=>item.id ==a)
    80. l.num--
    81. },
    82. j(){
    83. var s= this.list.reduce((sum,item)=>item.isCheck?sum+item.num:sum,0)
    84. var s1= this.list.reduce((sum,item)=>item.isCheck?sum+item.num*item.price:sum,0)
    85. alert("您共购买"+s+"个商品,花了"+s1+"元")
    86. }
    87. },
    88. computed:{
    89. isAll:{
    90. get(){ return this.list.every(a=>a.isCheck===true)},
    91. set(a){
    92. this.list.forEach(b=>b.isCheck=a)
    93. }
    94. },
    95. jiage(){
    96. return this.list.reduce((a,b)=>(b.isCheck?a+b.num*b.price:a),0)
    97. },
    98. zongshu(){
    99. return this.list.reduce((a,b)=>(b.isCheck?a+b.num:a),0)
    100. }
    101. },
    102. watch:{
    103. list:{
    104. deep:true,//深度监视
    105. handler(newValue){
    106. localStorage.setItem("list",JSON.stringify(newValue))
    107. }
    108. } ,
    109. }
    110. })
    111. script>
    112. body>
    113. html>

  • 相关阅读:
    爱摸鱼的TT~自学Java从入门到入土学习手册
    【数据结构】绪论
    SpringBoot日志基础
    vue3详解
    栈和队列经典oj面试题
    Vue+ElementUI—环境搭建
    3_springboot_shiro_jwt_多端认证鉴权_Redis缓存管理器.md
    python pygame入门 - 安装测试篇
    CrossOver2024下载安装详细图文教程
    shiro
  • 原文地址:https://blog.csdn.net/weixin_68926017/article/details/136407742