博客列表 >购物车全选/全不选功能 2. 库存管理功能

购物车全选/全不选功能 2. 库存管理功能

P粉751989631
P粉751989631原创
2022年11月18日 22:59:19560浏览

1.实例演示购物车

  1. <style>
  2. .cart {
  3. width: 460px;
  4. display: grid;
  5. gap: 10px;
  6. }
  7. table {
  8. border-collapse: collapse;
  9. text-align: center;
  10. }
  11. table caption {
  12. font-size: 20px;
  13. margin-bottom: 10px;
  14. }
  15. table input[type="number"] {
  16. width: 40px;
  17. }
  18. table th,
  19. table td {
  20. border-bottom: thin solid #888;
  21. padding: 5px;
  22. }
  23. table thead {
  24. border-top: thin solid #888;
  25. background-color: lightcyan;
  26. }
  27. table tfoot {
  28. background-color: lightcyan;
  29. }
  30. table tbody tr:nth-child(odd):hover {
  31. background-color: #eee;
  32. cursor: pointer;
  33. }
  34. table tr td:first-child {
  35. padding-left: 20px;
  36. }
  37. table tr td:last-child {
  38. padding-right: 20px;
  39. }
  40. .cart .pay {
  41. display: grid;
  42. grid: 1fr / auto-flow;
  43. place-content: end;
  44. gap: 10px;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="cart">
  50. <table>
  51. <caption>
  52. 我的购物车
  53. </caption>
  54. <thead>
  55. <tr>
  56. <td><input type="checkbox" name="" class="check-all" checked /></td>
  57. <td>编号</td>
  58. <td>品名</td>
  59. <td>单位</td>
  60. <td>单价</td>
  61. <td>数量</td>
  62. <td>金额(元)</td>
  63. </tr>
  64. </thead>
  65. </table>
  66. </div>
  67. <script type="module">
  68. // (一) 商品信息
  69. const items = [
  70. { id: 286, name: "酸奶", units: "箱", price: 50, num: 1 },
  71. { id: 870, name: "苹果", units: "千克", price: 10, num: 1 },
  72. { id: 633, name: "外套", units: "件", price: 300, num: 1 },
  73. { id: 153, name: "皮鞋", units: "双", price: 400, num: 1 },
  74. { id: 109, name: "手机", units: "台", price: 5000, num: 1 },
  75. ];
  76. // (二)导入购物车模块
  77. import Cart from "./modules/cart.js";
  78. //(三)实例化购物车类
  79. const cart = new Cart(items);
  80. // (四) 获取购物车(表格)
  81. const table = document.querySelector("table");
  82. // (五) 将商品渲染到购物车元素中 tbody
  83. // 1 创建 tbody: 商品容器
  84. const tbody = table.createTBody();
  85. // 2. 创建tbody的内容,商品列表
  86. items.forEach(function (item, key) {
  87. // 1. 创建商品模板字符串
  88. const tr = `
  89. <tr>
  90. <td><input type="checkbox" name="" class="check" checked /></td>
  91. <td>${item.id}</td>
  92. <td>${item.name}</td>
  93. <td>${item.units}</td>
  94. <td>${item.price}</td>
  95. <td>
  96. <input type="number" name="" value="${item.num}"
  97. </td>
  98. <td class="money">${cart.money[key]}</td>
  99. </tr>
  100. `;
  101. // 2. 将内容填充到tbody
  102. tbody.insertAdjacentHTML("beforeend", tr);
  103. // tbody.innerHTML = tr;
  104. });
  105. // (六) 将相关统计数据(总数量,总金额),填充到tfoot中
  106. const tfoot = table.createTFoot();
  107. // 创建tfoot内容
  108. let tr = `
  109. <tr>
  110. <td colspan="5">总计:</td>
  111. <td class="total">${cart.total}</td>
  112. <td class="total-money">${cart.totalMoney}</td>
  113. </tr>
  114. `;
  115. tfoot.insertAdjacentHTML("beforeend", tr);
  116. // (七) 更新数量,实时计算出结果并显示出来
  117. // 1. 拿到所有的数量控件
  118. const nums = table.querySelectorAll("input[type=number]");
  119. // 2. 为每一个数量控件添加事件监听: input
  120. nums.forEach(function (num, key) {
  121. num.oninput = function () {
  122. // 1. 计算总数量
  123. items[key].num = num.value * 1;
  124. cart.total = cart.getTotal(items);
  125. // 2. 计算每个商品金额
  126. cart.money[key] = num.value * 1 * items[key].price;
  127. // 3. 计算商品总金额
  128. cart.totalMoney = cart.money.reduce(function (acc, cur) {
  129. return acc + cur;
  130. });
  131. // 4. 将数据渲染到指定元素上
  132. table.querySelector(".total").textContent = cart.total;
  133. table.querySelectorAll(".money")[key].textContent = cart.money[key];
  134. table.querySelector(".total-money").textContent = cart.totalMoney;
  135. };
  136. });
  137. // 满减促销活动表
  138. const promotion = [
  139. { key: 1, money: 500, reduce: 50 },
  140. { key: 2, money: 800, reduce: 60 },
  141. { key: 3, money: 1000, reduce: 100 },
  142. { key: 4, money: 2000, reduce: 300 },
  143. { key: 5, money: 5000, reduce: 500 },
  144. ];
  145. </script>
  146. </body>
  147. </html>
  1. // 默认导出
  2. // 购物车模块
  3. export default class {
  4. // 构造器
  5. constructor(items) {
  6. // 1. 商品总数量
  7. this.total = this.getTotal(items);
  8. // 2. 每个商品金额(数组)
  9. this.money = this.getMoney(items);
  10. // 3. 商品总金额
  11. this.totalMoney = this.getTotalMoney();
  12. }
  13. // (一) 计算商品总数量
  14. getTotal(items) {
  15. // 1. 数量数组: 每个商品的数量num字段组成的数组
  16. let numArr = items.map(function (item) {
  17. return item.num;
  18. });
  19. // 2. 计算总数量
  20. return numArr.reduce(function (acc, cur) {
  21. return acc + cur;
  22. });
  23. }
  24. // (二) 计算每个商品的金额
  25. getMoney(items) {
  26. // 金额 = 数量 * 单价
  27. return items.map(function (item) {
  28. return item.num * item.price;
  29. });
  30. }
  31. // (三) 计算商品总金额
  32. getTotalMoney() {
  33. return this.money.reduce(function (acc, cur) {
  34. return acc + cur;
  35. });
  36. }
  37. }

运行结果

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议