Blogger Information
Blog 30
fans 0
comment 0
visits 13896
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
原生购物车案例使用vue改写
天宁
Original
346 people have browsed it

代码

  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>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post