博客列表 >原生购物车案例使用vue改写

原生购物车案例使用vue改写

天宁
天宁原创
2022年04月21日 12:24:09345浏览

代码

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  7. <title>购物车</title>
  8. <script src="https://unpkg.com/vue@next"></script>
  9. <style>
  10. .box {
  11. width: 22em;
  12. height: 2em;
  13. }
  14. .list > li {
  15. height: 1.6em;
  16. background-color: #efefef;
  17. display: grid;
  18. grid-template-columns: repeat(5, 3em);
  19. gap: 1em;
  20. place-items: center right;
  21. border-bottom: 1px solid #ccc;
  22. }
  23. .list > li:first-of-type {
  24. background-color: lightseagreen;
  25. color: white;
  26. }
  27. .list > li:hover:not(:first-of-type) {
  28. cursor: pointer;
  29. background-color: lightcyan;
  30. }
  31. .list > li input[type='number'] {
  32. width: 3em;
  33. border: none;
  34. outline: none;
  35. text-align: center;
  36. font-size: 1em;
  37. background-color: transparent;
  38. }
  39. .list > li:last-of-type span.total-num,
  40. .list > li:last-of-type span.total-amount {
  41. grid-column: span 2;
  42. place-self: center right;
  43. color: lightseagreen;
  44. }
  45. .account {
  46. float: right;
  47. background-color: lightseagreen;
  48. color: white;
  49. border: none;
  50. outline: none;
  51. width: 4.5em;
  52. height: 1.8em;
  53. }
  54. .account:hover {
  55. background-color: coral;
  56. cursor: pointer;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div class="app">
  62. <div class="box">
  63. <div class="selectAll">
  64. <input type="checkbox" class="check-all" @change="checkedAll($event)" v-model="checked" />
  65. <label for="check-all">全选</label>
  66. </div>
  67. <ul class="list" @change="checkeds($event)">
  68. <li>
  69. <span>选择</span>
  70. <span>品名</span>
  71. <span>数量</span>
  72. <span>单价</span>
  73. <span>金额</span>
  74. </li>
  75. <li v-for="(item,index) of pricearr" :key="index">
  76. <input type="checkbox" v-model="item.checked" />
  77. <span class="content">{{item.name}}</span>
  78. <input type="number" v-model="item.num" class="num" />
  79. <span class="price">{{item.price}}</span>
  80. <span class="amount">{{item.amount}}</span>
  81. </li>
  82. <li>
  83. <span>总计:</span>
  84. <span class="total-num">{{totalNum}}</span>
  85. <span class="total-amount">{{totalAmount}}</span>
  86. </li>
  87. </ul>
  88. <button class="account">结算</button>
  89. </div>
  90. </div>
  91. <script>
  92. const app = Vue.createApp({
  93. data() {
  94. return {
  95. list: [
  96. {
  97. checked: false,
  98. name: '手机',
  99. price: 100,
  100. num: 1,
  101. amount: 0,
  102. },
  103. {
  104. checked: false,
  105. name: '电脑',
  106. price: 200,
  107. num: 2,
  108. amount: 0,
  109. },
  110. {
  111. checked: false,
  112. name: '相机',
  113. price: 300,
  114. num: 3,
  115. amount: 0,
  116. },
  117. ],
  118. checked: false,
  119. };
  120. },
  121. computed: {
  122. //计算选中的商品金额
  123. pricearr() {
  124. this.list.forEach((item, i) => {
  125. if (item.checked) {
  126. item.amount = item.num * item.price;
  127. } else {
  128. item.amount = 0;
  129. }
  130. });
  131. return this.list;
  132. },
  133. //计算选中商品数量
  134. totalNum() {
  135. let nums = this.list.map((item, i) => {
  136. return item.checked ? item.num : 0;
  137. });
  138. return nums.reduce((acc, item) => acc + item);
  139. },
  140. //计算选中商品总金额
  141. totalAmount() {
  142. let amounts = this.list.map((item, i) => {
  143. return item.checked ? item.amount : 0;
  144. });
  145. return amounts.reduce((acc, item) => acc + item);
  146. },
  147. },
  148. methods: {
  149. //设置全选
  150. checkedAll(ev) {
  151. this.checked === !ev.target.checked;
  152. if (ev.target.checked) {
  153. this.list.forEach((item, i) => {
  154. item.checked = true;
  155. });
  156. } else {
  157. this.list.forEach((item, i) => {
  158. item.checked = false;
  159. });
  160. }
  161. },
  162. //单个选项
  163. checkeds(ev) {
  164. ev.target.checked === !ev.target.checked;
  165. //若未全选则取消全选按钮
  166. let isall = this.list.map((item, i) => {
  167. return item.checked ? 1 : 0;
  168. });
  169. this.checked = isall.every(item => item === 1);
  170. },
  171. },
  172. }).mount('.app');
  173. </script>
  174. </body>
  175. </html>
上一条:变量与函数下一条:Mac安装Homebrew
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议