!
也想出现在这里? 联系我们
广告区块

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

本案例源码链接(非VIP可私聊获取):https://download.csdn.net/download/weixin_52212950/86286910【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例https://download.csdn.net/download/weixin_52212950/86286910

文章导读:

这篇文章实现一个小案例:在购物平台选商品时我们经常会输价格区间,然后筛选出在这个区间内的商品,其实有JavaScript基础我们就已经能实现了,利用循环判断等知识。但是这篇文章是新方法 forEach,filter,some 的使用实现,可以让我们的实现更轻松。

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

文章目录:

一:效果展示

二:功能实现分析

2.1 页面渲染数据

2.2 根据价格区间查找

2.3 根据输入结果准确查找

三:完整代码


一:效果展示

页面基本效果

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

根据价格区间搜索

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

根据车辆名称搜索

【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例


二:功能实现分析

2.1 页面渲染数据

我们的数据存放格式为数组放对象,在写入大量数据后要将其渲染到页面的主表格中,我们可以使用 forEach 方法来实现,每次迭代都会执行一次基础的创建行,添加行的操作


实现过程:

  • 遍历开始,首先轮到的是数组第一个元素,即第一个车辆的数据,然后创建行tr,给行tr添加内容,内容中直接将车辆信息的编号id,名称name,价格price写入,然后再在tbody内加上创建好的行
  • 再轮到下一个车辆的数据,在创建行等操作,直到所有车辆信息全部添加进去结束
  1. car.forEach(function(value,index,arr){
  2. var tr=document.createElement('tr');
  3. tr.innerHTML=''+value.id+''+value.name+''+value.price+''
  4. tbody.appendChild(tr)
  5. })

2.2 根据价格区间查找

既然是筛选数据,那自然是选择我们的 filter 方法了,为了不让表格数据每次搜索完后上一次的数据还保留,所以每次点击后先清空右侧表格内的数据再去执行筛选。filter 返回的是一个新数组,所以需要一个新定义的数组去接收。筛选过后再使用 forEach 将新数组渲染到右侧的搜错结果表格即可

  1. select1.addEventListener('click',function(){
  2. rtbody.innerHTML=''
  3. var newcar=car.filter(function(value,index,arr){
  4. return value.price>=start.value&&value.pricevalue;
  5. })
  6. // console.log(newcar);
  7. newcar.forEach(function(value,index,arr){
  8. var tr=document.createElement('tr');
  9. tr.innerHTML=''+value.name+' :'+value.price+'万'+''
  10. rtbody.appendChild(tr)
  11. })
  12. })

  13. 2.3 根据输入结果准确查找

    在大批数据中要准确查找某个信息时,使用 some 方法比较合适,因为 some 一旦查找到就会停止遍历,数据量大时可以节省效率,很明显我们这个功能也要让其返回一个符合条件的新数组,但是some返回的结果是一个布尔值,所以此处我们需要自定义数组,然后去用 push 方法将复合查找要求的数据添加到新数组里,并且一旦查找到就让其 返回 ture 终止循环遍历

    1. select2.addEventListener('click',function(){
    2. rtbody.innerHTML=''
    3. newarr=[]
    4. car.some(function(value,index,arr){
    5. if(value.name==name1.value){
    6. newarr.push(value)
    7. return true;
    8. }
    9. })
    10. newarr.forEach(function(value,index,arr){
    11. var tr=document.createElement('tr');
    12. tr.innerHTML=''+value.name+' :'+value.price+'万'+''
    13. rtbody.appendChild(tr)
    14. })
    15. })
    16. 【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例

      三:完整代码

      1. DOCTYPE html>
      2. Document
      3. *{
      4. margin: 0;
      5. padding: 0;
      6. }
      7. .outbox{
      8. position: absolute;
      9. width: 1200px;
      10. left: 50%;
      11. top: 50%;
      12. transform: translate(-50%,-50%);
      13. border-top: 1px solid black;
      14. border-left: 1px solid black;
      15. border-right: 2.2px solid black;
      16. border-bottom: 2.2px solid black;
      17. }
      18. .leftbox{
      19. float: left;
      20. width: 900px;
      21. /* height: 600px; */
      22. box-sizing: border-box;
      23. border-top: 1.4px solid black;
      24. border-left: 1.4px solid black;
      25. border-right: 5px solid black;
      26. }
      27. .rightbox{
      28. float: left;
      29. width: 298px;
      30. border-top: 1.5px solid black;
      31. }
      32. .top{
      33. position: relative;
      34. width: 898px;
      35. height: 120px;
      36. border-bottom: 1.4px solid black;
      37. background-color: rgb(219, 219, 219);
      38. box-sizing: border-box;
      39. }
      40. .bottom{
      41. padding-top: 28px;
      42. width: 898px;
      43. padding-bottom: 28px;
      44. background-color: rgb(244, 244, 244);
      45. }
      46. table{
      47. margin: 0 auto;
      48. width: 835px;
      49. border: 1.4px solid black;
      50. }
      51. thead tr{
      52. width: 835px;
      53. height: 40px;
      54. border: 1.4px solid black;
      55. background-color: rgb(208, 241, 255);
      56. }
      57. tbody tr{
      58. width: 835px;
      59. height: 50px;
      60. border: 1.4px solid black;
      61. background-color: rgb(255, 239, 239);
      62. }
      63. td{
      64. width: 278px;
      65. border: 1.4px solid black;
      66. text-align: center;
      67. font-weight: bold;
      68. font-size: 17px;
      69. letter-spacing: 3px;
      70. }
      71. .price{
      72. position: absolute;
      73. top: 22px;
      74. left: 37px;
      75. letter-spacing: 2px;
      76. font-size: 18px;
      77. font-weight: 500;
      78. }
      79. .start,.end{
      80. padding-left: 6px;
      81. width: 100px;
      82. height: 30px;
      83. outline: none;
      84. font-size: 20px;
      85. }
      86. .pricebox{
      87. position: absolute;
      88. top: 19px;
      89. left: 325px;
      90. letter-spacing: 2px;
      91. font-size: 18px;
      92. font-weight: 800;
      93. }
      94. .select1{
      95. position: absolute;
      96. width: 170px;
      97. height: 37px;
      98. top: 17px;
      99. right: 115px;
      100. font-size: 14.5px;
      101. letter-spacing: 2px;
      102. background-color: rgb(255, 226, 154);
      103. border: 1px solid black;
      104. }
      105. .name-p{
      106. position: absolute;
      107. bottom: 22px;
      108. left: 94px;
      109. letter-spacing: 2px;
      110. font-size: 18px;
      111. font-weight: 500;
      112. }
      113. .name{
      114. position: absolute;
      115. bottom: 16px;
      116. left: 325px;
      117. letter-spacing: 2px;
      118. font-size: 18px;
      119. width: 235px;
      120. height: 30px;
      121. outline: none;
      122. padding-left: 6px;
      123. }
      124. .select2{
      125. position: absolute;
      126. width: 170px;
      127. height: 37px;
      128. bottom: 15px;
      129. right: 115px;
      130. font-size: 14.5px;
      131. letter-spacing: 2px;
      132. background-color: rgb(255, 226, 154);
      133. border: 1px solid black;
      134. }
      135. .select1:hover{
      136. background-color: rgb(255, 210, 95);
      137. }
      138. .select2:hover{
      139. background-color: rgb(255, 214, 110);
      140. }
      141. .rtable{
      142. margin: 0 auto;
      143. width: 240px;
      144. margin-top: 40px;
      145. margin-bottom: 40px;
      146. }
      147. .rthead .rtr{
      148. width: 240px;
      149. height: 40px;
      150. border: 1.4px solid black;
      151. background-color: rgb(208, 241, 255);
      152. }
      153. .rtbody .rtr{
      154. width: 240px;
      155. height: 40px;
      156. border: 1.4px solid black;
      157. background-color: rgb(255, 201, 114);
      158. }
      159. .rtd{
      160. width: 240px;
      161. border: 1.4px solid black;
      162. text-align: center;
      163. font-weight: bold;
      164. font-size: 17px;
      165. letter-spacing: 3px;
      166. }
      167. .righttd{
      168. width: 240px;
      169. border: 1.4px solid black;
      170. text-align: center;
      171. font-weight: bold;
      172. font-size: 17px;
      173. letter-spacing: 3px;
      174. background-color: rgb(255, 234, 206);
      175. }
      176. .clear{
      177. width: 85px;
      178. height: 85px;
      179. position: absolute;
      180. top: 16px;
      181. right: 13px;
      182. border: 1px solid black;
      183. font-weight: 400;
      184. font-size: 15px;
      185. background-color: #fff;
      186. }
      187. .clear:hover{
      188. background-color: rgb(247, 247, 247);
      189. }
      190. 您要查询的价格区间为(万元):

      191. ~
  • 您要查询的车辆名称为:

  • 汽车编号汽车名称汽车售价(万元)
  • 查询结果
  • var car=[
  • {
  • 'id':1000001,
  • 'name':'奥迪A6 Avent',
  • 'price':46
  • },
  • {
  • 'id':1000002,
  • 'name':'奥迪A6 allroad',
  • 'price':52
  • },
  • {
  • 'id':1000003,
  • 'name':'奥迪RS6',
  • 'price':145
  • },{
  • 'id':1000004,
  • 'name':'奥迪RS7',
  • 'price':154
  • },
  • {
  • 'id':1000005,
  • 'name':'奥迪A7L',
  • 'price':82
  • },
  • {
  • 'id':1000006,
  • 'name':'GTR R32',
  • 'price':140
  • },
  • {
  • 'id':1000007,
  • 'name':'GTR R33',
  • 'price':101
  • },
  • {
  • 'id':1000008,
  • 'name':'GTR R34',
  • 'price':180
  • },
  • {
  • 'id':1000009,
  • 'name':'GTR R35',
  • 'price':160
  • }
  • ]
  • var tbody=document.querySelector('tbody');
  • var rtbody=document.querySelector('.rtbody');
  • var start=document.querySelector('.start');
  • var end=document.querySelector('.end');
  • var select1=document.querySelector('.select1');
  • var clear=document.querySelector('.clear');
  • var select2=document.querySelector('.select2');
  • var name1=document.querySelector('.name');
  • clear.addEventListener('click',function(){
  • window.location.reload()
  • })
  • car.forEach(function(value,index,arr){
  • var tr=document.createElement('tr');
  • tr.innerHTML=''+value.id+''+value.name+''+value.price+''
  • tbody.appendChild(tr)
  • })
  • select1.addEventListener('click',function(){
  • rtbody.innerHTML=''
  • var newcar=car.filter(function(value,index,arr){
  • return value.price>=start.value&&value.price
  • })
  • // console.log(newcar);
  • newcar.forEach(function(value,index,arr){
  • var tr=document.createElement('tr');
  • tr.innerHTML=''+value.name+' :'+value.price+'万'+''
  • rtbody.appendChild(tr)
  • })
  • })
  • select2.addEventListener('click',function(){
  • rtbody.innerHTML=''
  • newarr=[]
  • car.some(function(value,index,arr){
  • if(value.name==name1.value){
  • newarr.push(value)
  • return true;
  • }
  • })
  • newarr.forEach(function(value,index,arr){
  • var tr=document.createElement('tr');
  • tr.innerHTML=''+value.name+' :'+value.price+'万'+''
  • rtbody.appendChild(tr)
  • })
  • })
  • 【JavaScript 进阶教程】汽车商城根据价格区间筛选车辆案例
    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
    本文链接:https://blog.csdn.net/weixin_52212950/article/details/126139222
    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
    0 条回复 A文章作者 M管理员
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    有新私信 私信列表
    搜索
    幸运之星正在降临...
    点击领取今天的签到奖励!
    恭喜!您今天获得了{{mission.data.mission.credit}}积分