uni-app:触底事件使用

需求:需要滑到底部,加载更多数据,不想使用分页组件,实现触底加载

解决方法:使用页面滚动到底部的事件onReachBottom()

1.在项目的pages.json文件找到需要添加触底事件的页面,添加onReachBottomDistance(页面底部的触发距离)

{
"path": "pages/personal-center/wallet-detail", // 页面路径
"style": {
"navigationBarTitleText": "全部明细",
"navigationBarBackgroundColor": "#fff",
"enablePullDownRefresh": false, // 是否开启下拉刷新
"onReachBottomDistance": 50   // (滚动页面到距离底部50px时,就会触发onReachBottom事件)
         }
}

2.在页面调用onReachBottom()

// 触底事件 - 上拉加载
onReachBottom() {
// 加判断: 页码数 * 每一页获取数据的条数 >= 总条数,如果符合条件说明数据已经全部加载完毕
if (this.form.pageNum*this.form.pageSize >= this.total) {
    this.status = "noMore"
    return
} else{
  this.status = "loading"  
  this.form.pageNum++   // 每触发一次触底事件,页码加1
  this.getDetail() // 请求接口,获取数据  
}
}

完整代码如下:

<template>
	<view class="wallet-detail">
		<view class="center-center" v-if="list.length >0">
			<view class="center-item" v-for="(item,index) in list" :key="index">
				<view class="item-left">
					<view class="left-title">
					  <span>{{item.financialType == 0?'收入':'支出'}}</span>
					</view>
					<view class="left-time">
						{{item.createTime}}
					</view>
				</view>
				<view class="item-right">
					<view class="right-num">
						¥{{item.amount}}
					</view>
					<view class="right-total">
						余额:¥{{item.accountBalance}}
					</view>
				</view>
			</view>
				<uni-load-more :status="status" />
		</view>
        <!-- 无数据时展示 -->
		<view v-else class="empty">
			<image style="width: 570rpx;height: 200rpx;" 
         src="无数据的图片路径"
				mode="aspectFill"></image>
			<view class="empty-text" style="font-size: 24rpx;color: #999;">
				暂无明细
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					pageNum: 1, 
					pageSize: 20
				},
				total: 0,
				list: [],
				status: 'more' // 初始状态为"更多"
			}
		},
		created() {
			this.getDetail()
		},
		methods: {
			// 请求接口,查询明细
			getDetail() {
			this.status = this.form.pageNum*this.form.pageSize >= this.total ? 
            'noMore' : 'more'
				this.$http.post('请求接口地址', {
					pageNum: this.form.pageNum,
					pageSize: this.form.pageSize
				}).then(res => {
					if (res.code == 200) {						
						if (this.form.pageNum == 1) {
							this.list = res.data.rows
							this.total = res.data.total
						} else {
							var list1 = res.data.rows
							this.list = [...this.list, ...list1]
						}
					}
				})
			},

		},
		// 触底事件 - 上拉加载
		onReachBottom() {
			// 加判断: 页码数 * 每一页获取数据的条数 >= 总条数,如果符合条件说明数据已经全部加载完毕
			if (this.form.pageNum*this.form.pageSize >= this.total) {
			this.status = "noMore"
			 return
			} else {
				this.status = "loading" 
				this.form.pageNum++   // 每触发一次触底事件,页码加1
				this.getDetail()
			}
			
		}
	}
</script>

<style lang="less" scoped>
	
</style>

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值