# 6.3 上拉加载(Loadmore)

基于 Weex 原生 <loading> 的上拉加载示例。滚动到底部时触发 loading 事件,调用方负责请求下一页数据并控制加载状态。

# 实例

# 基础用法

<template>
  <div class="dof-demo">
    <dof-minibar title="上拉加载" :left-button="leftButton"></dof-minibar>
    <scroller class="scroller">
      <div class="cell" v-for="num in lists" :key="num">
        <div class="panel">
          <text class="text">{{ num }}</text>
        </div>
      </div>
      <loading class="loading" @loading="onLoading" :display="loading ? 'show' : 'hide'" v-if="hasNextPage">
        <midea-apng-view :loop="true" :auto="true" :src="loadingIcon" class="indicator"></midea-apng-view>
      </loading>
      <text class="tips" v-if="!hasNextPage">- 没有更多内容了 -</text>
    </scroller>
  </div>
</template>

<script>
import { DofMinibar } from 'dolphin-intl-ui'

export default {
  components: { DofMinibar },
  data: () => ({
    loading: false,
    maxPage: 3,
    currentPage: 1,
    pageSize: 5,
    lists: [0, 1, 2, 3, 4],
    loadingIcon: 'http://dolphin-weex-dev.msmartlife.cn/cdn/images/common/loading_gray.png',
    hasNextPage: true,
  }),
  methods: {
    onLoading() {
      this.loading = true
      setTimeout(() => {
        this.loadNextPage()
        this.loading = false
      }, 2000)
    },
    loadNextPage() {
      if (this.currentPage >= this.maxPage) {
        this.hasNextPage = false
        return
      }
      const start = this.currentPage * this.pageSize
      for (let index = start; index < start + this.pageSize; index += 1) {
        this.lists.push(index)
      }
      this.currentPage += 1
    },
  },
}
</script>

<style scoped>
.dof-demo {
  background-color: #f9f9f9;
}
.loading {
  width: 750px;
  align-items: center;
  padding-bottom: 40px;
}
.indicator {
  margin-top: 16px;
  height: 40px;
  width: 40px;
}
.panel {
  width: 686px;
  height: 344px;
  margin: 35px 32px;
  flex-direction: column;
  justify-content: center;
  border-radius: 32px;
  background-color: #ffffff;
}
.text {
  font-size: 50px;
  text-align: center;
  color: #41b883;
}
.tips {
  color: #8a8a8f;
  font-size: 24px;
  margin-top: 80px;
  padding-bottom: 80px;
  text-align: center;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

# Events

事件名 说明 回调参数
loading 滚动到底部且加载器可见时触发 原生事件对象