博客列表 >vue 的购物车案例

vue 的购物车案例

default
default原创
2022年03月12日 11:37:55862浏览
  1. <template>
  2. <div>
  3. <input type="checkbox" name="" v-model="isAll" id="" @change="click" >全选
  4. <ul>
  5. <li v-for="(item,key) in list" :key="item.id" >
  6. <input type="checkbox" name="" v-model="checkList" :value="item" @change="itemClick" >
  7. <img :src="item.img" alt="">
  8. <div>
  9. <span>商品名:{{item.name}}</span>
  10. <br>
  11. <span>价格:{{item.price}}</span>
  12. </div>
  13. <div>
  14. <button @click="item.number++" :disabled="item.number===item.limit" >+</button>{{item.number}} <button @click="item.number--" :disabled="item.number === 0">-</button>
  15. </div>
  16. <div>
  17. <button @click="del(key,item.id)"> 删除</button>
  18. </div>
  19. </li>
  20. </ul>
  21. 总价格:{{sum()}}
  22. <br>
  23. {{checkList}}
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. data(){
  29. return{
  30. isAll:false,
  31. list:[
  32. {
  33. id:1,
  34. name:'ddd1',
  35. img:'https://img1.baidu.com/it/u=1583661127,1393133506&fm=26&fmt=auto',
  36. number:0,
  37. limit:5,
  38. price:20
  39. },
  40. {
  41. id:2,
  42. name:'ddd2',
  43. img:'https://img1.baidu.com/it/u=1583661127,1393133506&fm=26&fmt=auto',
  44. number:0,
  45. limit:5,
  46. price:30
  47. },
  48. {
  49. id:3,
  50. name:'ddd3',
  51. img:'https://img1.baidu.com/it/u=1583661127,1393133506&fm=26&fmt=auto',
  52. number:0,
  53. limit:5,
  54. price:40
  55. }
  56. ],
  57. checkList:[]
  58. }
  59. },
  60. methods:{
  61. sum(){
  62. var total = 0
  63. this.checkList.forEach(item=>{
  64. total += item.number * item.price
  65. })
  66. return total
  67. },
  68. del(key,id){
  69. this.list.splice(key,1)
  70. //删除原数组的同时过滤掉选中数组中的不存在的值
  71. this.checkList = this.checkList.filter(item=>item.id!==id)
  72. },
  73. click(){
  74. if(this.isAll ){
  75. this.checkList = this.list
  76. }else{
  77. this.checkList = []
  78. }
  79. },
  80. itemClick(){
  81. console.log(this.checkList.length)
  82. if(this.checkList.length === this.list.length){
  83. this.isAll = true
  84. }else{
  85. this.isAll = false
  86. }
  87. }
  88. }
  89. }
  90. </script>
  91. <style>
  92. ul{
  93. width: 500px;
  94. }
  95. li{
  96. display: flex;
  97. flex-flow: nowrap row;
  98. justify-content: space-around;
  99. margin: 10px;
  100. }
  101. img{
  102. width: 200px;
  103. }
  104. </style>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议