images.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. Component({
  2. // 组件的属性列表
  3. properties: {
  4. // 判断是否还有更多数据
  5. hasMore: {
  6. type: Boolean,
  7. value: false
  8. },
  9. // 加载中的显示文本
  10. loadingText: {
  11. type: String,
  12. value: '加载中...'
  13. },
  14. // 加载失败的显示文本
  15. failText: {
  16. type: String,
  17. value: '加载失败, 请点击重试!'
  18. },
  19. // 没有更多后的显示文本, 默认没有则隐藏加载更多控件
  20. finishText: {
  21. type: String,
  22. value: ''
  23. },
  24. // 列表渲染延时, 默认为 500 ms, 我在开发工具中测试列表渲染速度时快时慢, 可根据实际使用中界面复杂度自行调整
  25. // ps 如果能监听setData() 渲染结束的话则可以不需要延时
  26. listRenderingDelay: {
  27. type: Number,
  28. value: 500
  29. }
  30. },
  31. /**
  32. * 组件的初始数据
  33. */
  34. data: {
  35. showThis: false,
  36. text: '',
  37. showIcon: false,
  38. isLoading: false
  39. },
  40. /**
  41. * 组件的方法列表
  42. */
  43. methods: {
  44. //加载更多的入口方法, 直接在page中使用时请在onReachBottom() 方法中调用这个方法, 并实现loadMoreListener方法去获取数据
  45. loadMore: function () {
  46. console.log('properties', this.properties)
  47. if (!this.properties.hasMore) {
  48. console.log('load more finish')
  49. return
  50. }
  51. if (this.data.isLoading) {
  52. console.log('loading ...')
  53. return
  54. }
  55. this.setData({
  56. isLoading: true
  57. })
  58. this.triggerEvent('loadMoreListener')
  59. },
  60. //加载完成, 传入hasMore
  61. loadMoreComplete: function (data) {
  62. var hasMore = data.curPage < data.pageCount && data.pageCount != 1
  63. var text = '', showThis = false, showIcon = false
  64. if (hasMore) {
  65. showIcon = true
  66. showThis = true
  67. text = this.properties.loadingText
  68. } else if (this.properties.finishText.length > 0) {
  69. text = this.properties.finishText
  70. showThis = true
  71. }
  72. this.setData({
  73. hasMore: hasMore,
  74. text: text,
  75. showIcon: showIcon,
  76. showThis: showThis
  77. })
  78. //界面渲染延迟, 避免列表还未渲染完成就再次触发 loadMore 方法
  79. setTimeout(function () {
  80. this.setData({
  81. isLoading: false
  82. })
  83. }.bind(this), this.properties.listRenderingDelay)
  84. },
  85. // 加载失败
  86. loadMoreFail: function () {
  87. this.setData({
  88. showIcon: false,
  89. text: this.properties.failText
  90. })
  91. //界面渲染延迟, 避免列表还未渲染完成就再次触发 loadMore 方法
  92. setTimeout(function () {
  93. this.setData({
  94. isLoading: false
  95. })
  96. }.bind(this), this.properties.listRenderingDelay)
  97. },
  98. //点击 loadmore 控件时触发, 只有加载失败时才会进入页面回调方法
  99. clickLoadMore: function () {
  100. if (this.data.text != this.properties.failText) return
  101. this.setData({
  102. showIcon: true,
  103. text: this.properties.loadingText,
  104. isLoading: true
  105. })
  106. this.triggerEvent('clickLoadMore')
  107. }
  108. }
  109. })