从微信小程序到鸿蒙JS开发-list加载更多&回到顶部

开发
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[383590]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

1、list加载更多

如果在list中需要展示的数据非常多,那么一次性获取全部数据并显示,对于后端服务器和前段渲染的性能都是很大的负担,浪费资源且页面加载速度会很慢。

在网页端做分页普遍是用户点击“上一页”,“下一页”进行翻页,而移动端设备一般是在滑动到页面底端后加载下一页数据,并将数据接在列表底部。在list组件中,可以通过onscrollbottom属性绑定事件并处理。

视觉效果上来看数据是连续的,但其中已经触发了一次翻页。

list部分 hml视图层:

  1. <list scrollbar="auto" scrolleffect="no" onscrollbottom="loadMore" id="list"
  2.     <block for="{{ comments }}"
  3.         <list-item> 
  4.             <div> 
  5.                 <image src="/common/user.png"></image> 
  6.                 <div class="title"
  7.                     <text style="color: #333333; font-size: 32px;"
  8.                         {{ $item.user.username }} 
  9.                     </text> 
  10.                     <text style="color: #666666; font-size: 30px;"
  11.                         {{ $item.date }} 
  12.                     </text> 
  13.                 </div> 
  14.                 <rating numstars="5" rating="{{ $item.star }}" indicator="true"></rating> 
  15.             </div> 
  16.             <text class="content"
  17.                 {{ $item.content }} 
  18.             </text> 
  19.         </list-item> 
  20.     </block> 
  21. </list> 

css渲染层:

  1. list { 
  2.     width: 100%; 
  3.     height: 1400px; 
  4. list-item { 
  5.     width: 100%; 
  6.     border-bottom: 1px solid #bbbbbb; 
  7.     background-color: #fdfdfd; 
  8.     margin-bottom: 10px; 
  9.     display: flex; 
  10.     flex-direction: column
  11.     padding: 10px 0 10px 0; 
  12. list-item image { 
  13.     width: 60px; 
  14.     height: 60px; 
  15.     border-radius: 30px; 
  16.     margin-left: 20px; 
  17.     margin-top: 20px; 
  18.     object-fit: contain; 
  19. .title { 
  20.     margin-left: 20px; 
  21.     height: 100px; 
  22.     display: flex; 
  23.     flex-direction: column
  24.     width: 450px; 
  25. .title>text { 
  26.     height: 50px; 
  27.     line-height: 50px; 
  28. rating { 
  29.     width: 150px; 
  30.     height: 50px; 
  31. .content { 
  32.     margin: 10px 20px 10px 20px; 
  33.     font-size: 30px; 
  34.     color: #333333; 

js逻辑层:

  1. import fetch from '@system.fetch'
  2. import prompt from '@system.prompt'
  3.  
  4. export default { 
  5.     data: { 
  6.         ...... 
  7.         comments: [], 
  8.         page: 1, 
  9.         maxPage: 1 
  10.     }, 
  11.     onInit() { 
  12.         this.listComments(); 
  13.     }, 
  14.     // list触底加载下一页数据 
  15.     loadMore() { 
  16.         if (this.page < this.maxPage) { 
  17.             this.page++; 
  18.             this.listComments(); 
  19.         } else { 
  20.             prompt.showToast({ 
  21.                 message: "已经到底啦"
  22.                 duration: 3000 
  23.             }) 
  24.         } 
  25.     }, 
  26.     // 分页请求评论 
  27.     listComments() { 
  28.         fetch.fetch({ 
  29.             url: this.url + "/list?goodsId=" + this.id + "&pageNo=" + this.page, 
  30.             responseType: "json"
  31.             success: res => { 
  32.                 console.info(res.data); 
  33.                 let data = JSON.parse(res.data); 
  34.                 if (0 != data.code) { 
  35.                     prompt.showToast({ 
  36.                         message: "服务错误"
  37.                         duration: 3000 
  38.                     }) 
  39.                 } else { 
  40.                     data.data.list.forEach(ele => { 
  41.                         this.comments.push(ele); 
  42.                     }); 
  43.                     this.page = data.data.page; 
  44.                     this.maxPage = data.data.maxPage; 
  45.                 } 
  46.             } 
  47.         }) 
  48.     } 

在服务器端,每次请求返回十条数据,以及当前页数、总页数。

2、list回到顶部

查看了一部分评论后,如果想要回到第一条评论的位置,需有一个“回到顶部”按钮,点击后列表自动滚动到最顶部。

在官方文档list组件中,未提到如何实现这样的功能。但在js中获取组件实例后,有这么几个API可供调用:

猜测是可以使list滚动,我们使用scrollTop(),使列表滚动到最顶端。

  1. this.$element("list").scrollTop(); 

这样是不起作用的,虽然源代码注释的意思似乎是smooth默认为false。

smooth为false的效果,可以回到顶部,但比较生硬。

  1. this.$element("list").scrollTop({ 
  2.     smooth: false 
  3. }); 

smooth: true的效果,还是不错的。

按钮使用type="circle",便可指定icon,实现图标按钮。

hml视图层:

  1. <button onclick="toTop" type="circle" icon="/common/totop.png"></button> 

css渲染层:

  1. button { 
  2.     position: fixed; 
  3.     right: 20px; 
  4.     bottom: 20px; 
  5.     background-color: #eeeeee; 

js逻辑层:

  1. toTop() { 
  2.        this.$element("list").scrollTop({ 
  3.            smooth: true 
  4.        }); 
  5.    }, 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-02-20 09:52:02

鸿蒙HarmonyOS应用开发

2021-03-02 09:29:29

鸿蒙HarmonyOS应用开发

2021-02-23 12:25:26

鸿蒙HarmonyOS应用开发

2021-02-23 12:23:57

鸿蒙HarmonyOS应用开发

2021-02-21 11:09:18

鸿蒙HarmonyOS应用开发

2021-02-22 14:56:55

鸿蒙HarmonyOS应用开发

2021-02-25 10:01:19

鸿蒙HarmonyOS应用开发

2021-02-04 13:49:41

鸿蒙HarmonyOS应用开发

2021-02-23 09:52:42

鸿蒙HarmonyOS应用开发

2021-02-05 09:46:16

鸿蒙HarmonyOSjs开发

2021-02-24 09:36:03

鸿蒙CSS应用开发

2021-02-07 09:17:24

鸿蒙HarmonyOS应用开发

2017-05-08 15:03:07

微信小程序开发实战

2016-09-27 16:38:24

JavaScript微信Web

2016-11-04 10:49:48

微信小程序

2016-09-28 18:10:59

微信程序MINA

2017-01-09 10:42:56

微信小程序

2016-09-27 20:36:23

微信HttpWeb

2016-11-04 10:30:17

微信小程序

2018-09-11 10:32:07

云开发小程序开发者
点赞
收藏

51CTO技术栈公众号