【uniApp】实现列表下拉触底加载更多功能

html 复制代码
<scroll-view :style="'height:' + scrollViewH + 'px;'" 
	scroll-y="true" 
	@scrolltolower="onReachLower">
	<uni-collapse ref="collapse" v-model="value" v-for="el in listData" :key="el.id">
		<uni-collapse-item :title="el.stationname" titleBorder="none">
			<view class="info-content" @click="showInfo(el)">
				<p>{{el.warntime}} &nbsp;&nbsp;{{el.warninfo}}</p>
			</view>			
		</uni-collapse-item>
	</uni-collapse>
	<uni-load-more v-if="showLoadMore" :status="loadStatus" :content-text="contentText"></uni-load-more>
</scroll-view>
javascript 复制代码
export default {
	data() {
		return {
			params: {
				pageNum: 1, // 页码
				pageSize: 10 // 条数
			},
			listData: [], // 当前页列表数据
			// 下拉加载
			allListCount: 0, // 列表总数
			allListData: [], // 列表所有数据
			scrollViewH: 0, // 列表滚动区域
			showLoadMore: false,
			loadStatus: "more",
			contentText: {
				contentdown: '查看更多',
				contentrefresh: '加载中......',
				contentnomore: '已全部加载'
			}
		}
	},
	onLoad() {
		this.getScrollHeight()
		this.getList()
	},
	methods: {
		// 获取屏幕高度
		getScrollHeight() {
			let self = this;
				uni.getSystemInfo({
					success(res) {
						self.scrollViewH = res.windowHeight - 120; // 120为页面中我查询条件高度,根据实现情况调整
					}
				});
		},
		// 滚动到底部显示加载状态
		onReachLower(){
			this.showLoadMore = true
			if(this.listData.length >= this.allListCount){
				this.loadStatus = "noMore"
			}else{
				this.loadStatus = "loading"
				setTimeout(()=>{
					this.params.pageNum ++;
					this.loadMore();
				}, 1000)	
			}		
		},
		// 根据接口获取列表数据,此处为后端分页,可根据实际情况改为前端分页
		getList() {
			this.$api.getDataByAjax(url, 'GET', this.params).then((res)=>{
				if (res.code === 200 && res.data) {
					this.allListCount = res.data.count
					this.listData = res.data
				}
			})
		},
		// 加载更多
		loadMore(){
			this.$api.getDataByAjax(url, 'GET', this.params).then((res)=>{
				if (res.code === 200 && res.data) {
					this.listData = this.listData.concat(res.data); // 合并下一页数据
				}
			})
		},
	}
}
相关推荐
bug总结12 小时前
uniapp 微信小程序分包规范
微信小程序·小程序·uni-app
天府云创13 小时前
uni-app X 蒸汽模式发布!性能比肩原生~
前端框架·uni-app·移动开发·原生应用·蒸汽模式·app研发·多端适配
堕落年代16 小时前
uni-app x 蒸汽模式:实时流式语音识别(ASR)三大疑难杂症全记录
人工智能·uni-app·语音识别
RisunJan21 小时前
uni-app 高频面试题与解答
uni-app
梦曦i1 天前
@meng-xi/create-uni-app vs unibest:轻量脚手架与重型框架的定位之争
前端·uni-app
2501_915106322 天前
iOS数据采集技术详解:从性能监控到崩溃分析的全链路实践
android·ios·小程序·https·uni-app·iphone·webview
我爱写代码i3 天前
AI对话绘画数字人源码 - uniapp前端
前端·人工智能·uni-app
ModyQyW3 天前
vite-plugin-uni-pages 更新了什么
前端·uni-app
2501_915918413 天前
Flutter项目配置iOS混淆的详细步骤与工具推荐
android·flutter·ios·小程序·uni-app·cocoa·iphone
梦曦i3 天前
create-uni-app v1.2.0:交互体验优化
前端·uni-app