Blogger Information
Blog 40
fans 0
comment 0
visits 16124
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
仿写课堂中的购物车案例
飞天001
Original
310 people have browsed it

仿写课堂中的购物车案例

  1. // (一).导入购物车模块,获取data,total,totalMoney等数据
  2. import carts from './carts.js'
  3. // console.log(carts.total,carts.totalMoney);
  4. //(二).获取dom元素
  5. const total = document.querySelector('.total')
  6. const totalMoney = document.querySelector('.totalMoney')
  7. const items = document.querySelectorAll('.carts-body > .item')
  8. // console.log(items);
  9. // console.log(nums);
  10. //(三).渲染
  11. total.textContent = carts.total
  12. totalMoney.textContent = carts.totalMoney
  13. // 循环渲染购物车的每个元素
  14. items.forEach(function(item,index,arr){
  15. item.querySelector('.id').textContent = carts.data[index].id
  16. item.querySelector('.name').textContent = carts.data[index].name
  17. item.querySelector('.unit').textContent = carts.data[index].unit
  18. item.querySelector('.price').textContent = carts.data[index].price
  19. item.querySelector('.num').value = carts.data[index].num
  20. item.querySelector('.money').textContent = carts.data[index].money
  21. })
  22. // (四) 获取购物车中的单价,数量,金额分别组成数组,为更新购物车做准备
  23. const prices = document.querySelectorAll('.price')
  24. const nums = document.querySelectorAll('.num')
  25. const moneys = document.querySelectorAll('.money')
  26. const checks = document.querySelectorAll('.check')
  27. console.log(checks);
  28. /**
  29. * 分析:
  30. * 购物车中需要更新的数据:商品金额money,总数量total,总金额totalMoney
  31. * 要更新上面的数据,主要是依赖于更新单个商品的 数量num
  32. */
  33. //(五) 给商品数量的按钮添加change事件
  34. nums.forEach(function(num,index,arr){
  35. num.onchange = function(){
  36. // 判断该商品是否被选中,如果选中才计算(js秉承错误优先原则)
  37. if(!checks[index].checked) return false
  38. //1. 将每个数量取出来组成数组,再数组求和
  39. const numArr = [...arr].map(item=>parseInt(item.value))
  40. total.textContent = numArr.reduce((pre,cur)=>pre + cur)
  41. //2. 计算总金额=获取数组,再数组求和
  42. const moneyArr = [...arr].map((price,index)=>prices[index].textContent * nums[index].value)
  43. totalMoney.textContent = moneyArr.reduce((pre,cur)=>pre + cur)
  44. //3.单个商品价格=数量*单个价格
  45. moneys[index].textContent = num.value * prices[index].textContent
  46. }
  47. })
  48. //(六) 全选/全不选
  49. // 获取全选按钮
  50. const checkAll = document.querySelector('.check-all')
  51. let totalTmp = total.textContent
  52. let totalMoneyTmp = totalMoney.textContent
  53. // console.log(checkAll);
  54. //为全选按钮添加事件
  55. checkAll.onchange = function(){
  56. // 遍历当前所有选中按钮的状态,并将全选按钮的值复制给它
  57. checks.forEach(item=>item.checked=checkAll.checked)
  58. if (checkAll.checked==false){
  59. let checkStatus = [...checks].every(function(check){
  60. return check.checked
  61. })
  62. if(checkStatus ==false) {
  63. total.textContent = 0
  64. totalMoney.textContent = 0
  65. }
  66. }else{
  67. total.textContent = totalTmp
  68. totalMoney.textContent = totalMoneyTmp
  69. }
  70. }
  71. // //遍历每个商品的复选框,并添加change事件
  72. checks.forEach(function(check,index){
  73. check.onchange = function(){
  74. //根据单个复选框状态来设置 全选按钮
  75. checkAll.checked = [...checks].every(item=>item.checked) //every只要有一个选中,就返回true,从而checkall.checked就是true
  76. ////如果商品未选中,应该减去相对应的总数量和总金额
  77. if(check.checked == false) {
  78. total.textContent = total.textContent*1 - nums[index].value*1
  79. totalMoney.textContent = totalMoney.textContent*1 - moneys[index].textContent*1
  80. }else{
  81. total.textContent = total.textContent*1 + nums[index].value*1
  82. totalMoney.textContent = totalMoney.textContent*1 + moneys[index].textContent*1
  83. }
  84. }
  85. })

自己写了一遍,感觉还不是很通透.

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