表格和列表的无限滚动

该文章介绍了如何在Vue应用中实现表格和列表的无限滚动加载效果。通过监听滚动事件和使用`v-infinite-scroll`指令,当用户接近底部时,动态加载新数据并更新页面。文章涉及到的数据管理包括当前页数、总页数、分页大小以及数据加载状态的维护。

一、表格的无限滚动

// 表格滚动加载
function tableScroll() {
	// 获取表格对象
	nextTick(() => {
		let dom = document.querySelector('.el-table__body-wrapper') as any;
		dom.addEventListener('scroll', () => {
			const scrollDistance = dom.scrollHeight - dom.scrollTop - dom.clientHeight;
			// 判断是否到底,可以加载下一页
			if (scrollDistance <= 1) {
				// 判断是否全部加载完成
				if (state.tableData.length >= state.total) {
					console.log('加载完了');
				} else {
					//当前页数小于总页数就请求
					state.currentPage++; //当前页数自增
					let a = state.listAllData.slice((state.currentPage - 1) * state.pageSize, state.currentPage * state.pageSize);
					state.tableData = [...state.tableData, ...a];
				}
			}
		});
	});
}
onMounted(() => {
	tableScroll();
});

二、列表的无限滚动

<div 
	v-infinite-scroll="load" 
	:infinite-scroll-disabled="disabled" 
	style="height: calc(100% - 10px);overflow: auto"
>
	<div v-for="item in tableData" :key="item.elementId">
		<div class="titleElement">
			<i class="el-icon-document"></i>
			{{ item.elementName }}
		</div>
	</div>
	<p v-if="dataLoading" style="text-align: center">加载中...</p>
	<p v-if="noMore" style="text-align: center">没有更多了~</p>
</div>
// 无限滚动
const noMore: Ref = computed(() => {
	return state.tableData.length >= state.total;
});
const disabled: Ref = computed(() => {
	return state.dataLoading || noMore.value;
});
function load() {
	state.dataLoading = true;
	setTimeout(() => {
		if (state.tableData.length < state.total) {
			state.currentPage++;
			let a = state.listAllData.slice((state.currentPage - 1) * state.pageSize, state.currentPage * state.pageSize);
			state.tableData = [...state.tableData, ...a];
			state.dataLoading = false;
		}
	}, 400);
}
在 UniApp 中实现表格数据的无限滚动功能,由于无法直接操作 DOM 元素,因此不能使用传统的 DOM 操作方式来实现滚动效果。可以通过 `scroll-view` `flex` 布局结合定时器控制滚动位置,实现表格数据的垂直无限滚动效果。 ### 基本实现思路 - 使用 `flex` 布局来构建表格结构,包括表头表格内容。 - 使用 `scroll-view` 包裹表格内容区域,设置 `scroll-y` 属性以启用垂直滚动。 - 通过 `scroll-with-animation` 属性启用滚动动画,使滚动更加平滑。 - 利用 `scroll-top` 属性控制 `scroll-view` 的滚动位置。 - 使用 `setInterval` 定时器,每隔一段时间更新 `scroll-top` 值,实现自动滚动效果。 ### 示例代码 #### 模板部分 ```html <template> <view class="table-container"> <!-- 表头 --> <view class="table-header"> <view class="header-cell">列1</view> <view class="header-cell">列2</view> <view class="header-cell">列3</view> </view> <!-- 表格内容 --> <scroll-view class="table-body" scroll-y :scroll-top="scrollTop" :scroll-with-animation="true" > <view v-for="(row, index) in tableData" :key="index" class="table-row"> <view class="table-cell">{{ row.col1 }}</view> <view class="table-cell">{{ row.col2 }}</view> <view class="table-cell">{{ row.col3 }}</view> </view> </scroll-view> </view> </template> ``` #### 脚本部分 ```javascript <script> export default { data() { return { tableData: [ { col1: '数据1-1', col2: '数据1-2', col3: '数据1-3' }, { col1: '数据2-1', col2: '数据2-2', col3: '数据2-3' }, { col1: '数据3-1', col2: '数据3-2', col3: '数据3-3' }, { col1: '数据4-1', col2: '数据4-2', col3: '数据4-3' }, { col1: '数据5-1', col2: '数据5-2', col3: '数据5-3' }, { col1: '数据6-1', col2: '数据6-2', col3: '数据6-3' }, { col1: '数据7-1', col2: '数据7-2', col3: '数据7-3' }, { col1: '数据8-1', col2: '数据8-2', col3: '数据8-3' }, { col1: '数据9-1', col2: '数据9-2', col3: '数据9-3' }, { col1: '数据10-1', col2: '数据10-2', col3: '数据10-3' } ], scrollTop: 0, scrollInterval: null, rowHeight: 80 // 每行的高度(单位:rpx) }; }, mounted() { this.startAutoScroll(); }, methods: { startAutoScroll() { this.scrollInterval = setInterval(() => { const totalHeight = this.tableData.length * this.rowHeight; this.scrollTop = (this.scrollTop + this.rowHeight) % totalHeight; }, 2000); // 每隔2秒滚动一行 }, stopAutoScroll() { if (this.scrollInterval) { clearInterval(this.scrollInterval); this.scrollInterval = null; } } }, beforeDestroy() { this.stopAutoScroll(); } }; </script> ``` #### 样式部分 ```css <style scoped> .table-container { width: 100%; border: 1px solid #ccc; } .table-header { display: flex; background-color: #f5f5f5; border-bottom: 1px solid #ccc; } .header-cell { flex: 1; padding: 20rpx; font-weight: bold; text-align: center; } .table-body { height: 600rpx; /* 设置表格内容区域高度 */ overflow-y: scroll; } .table-row { display: flex; border-bottom: 1px solid #eee; } .table-cell { flex: 1; padding: 20rpx; text-align: center; } </style> ``` ### 功能说明 - **表头固定**:通过 `flex` 布局单独定义表头部分,实现固定表头效果。 - **自动滚动**:使用 `scroll-top` `setInterval` 实现表格内容的自动垂直滚动。 - **无缝滚动**:通过模运算 (`%`) 实现无缝循环滚动。 - **动画效果**:启用 `scroll-with-animation` 属性,使滚动更加平滑[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值