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

<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>
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); // 合并下一页数据
				}
			})
		},
	}
}

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 10:22:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 10:22:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 10:22:03       58 阅读
  4. Python语言-面向对象

    2024-07-13 10:22:03       69 阅读

热门阅读

  1. 【第33章】MyBatis-Plus之预防安全漏洞

    2024-07-13 10:22:03       28 阅读
  2. 【安全设备】上网行为管理

    2024-07-13 10:22:03       26 阅读
  3. 智能小车——底层配置

    2024-07-13 10:22:03       27 阅读
  4. PCIe总线的序

    2024-07-13 10:22:03       26 阅读
  5. 怎么知道服务器100M带宽可以支持多少人访问?

    2024-07-13 10:22:03       27 阅读
  6. [AI 大模型] Meta LLaMA-2

    2024-07-13 10:22:03       28 阅读
  7. Oracle逻辑备份

    2024-07-13 10:22:03       23 阅读
  8. c#视觉应用开发中如何在C#中处理图像噪声?

    2024-07-13 10:22:03       28 阅读
  9. 【ceph】ceph-mon重新选举的情况

    2024-07-13 10:22:03       27 阅读
  10. SpringBoot配置Swagger开启页面访问限制

    2024-07-13 10:22:03       26 阅读