uniapp uni-table最简单固定表头

需求:固定表头数据,在网上找了半天,啥都有,就是一直实现不了,最后更改代码实现

1.效果

2.主要代码讲解+完整代码

表格的父级一定要设置高度,不然会错位,我看网上说设置position:fixed,我觉得没必要,给父级设置position: relative;,表头设置position: absolute就可以定位到想要的位置了,但是这样会导致脱离标准文档流,所以还要在表格内容设置个margin-top:44px.44px是表头的高度,复制如下代码即可实现需求~

html 复制代码
<template>
	<view class="uni-container">
		<uni-table ref="table"  border stripe emptyText="暂无更多数据">
			<view class="tableHead">
				<uni-tr>
					<uni-th align="center">
						<view style="width:50px;">姓名</view>
					</uni-th>
					<uni-th align="center">
						<view style="width:50px;">部门</view>
					</uni-th>
					<uni-th align="center">
						<view style="width:50px;">职位</view>
					</uni-th>
					<uni-th align="center">
						<view style="width:50px;">角色</view>
					</uni-th>
				</uni-tr>
			</view>
			<view class="tableBody" >
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td>
						<view style="width:50px;">{{item.realname}}</view>
					</uni-td>
					<uni-td>
						<view style="width:50px;">{{item.name}}</view>
					</uni-td>
					<uni-td>
						<view style="width:50px;">{{item.post || ''}}</view>
					</uni-td>
					<uni-td>
						<view style="width:50px;">{{item.roleName}}</view>
					</uni-td>
				</uni-tr>
			</view>
		</uni-table>
	</view>


</template>

<script>
	export default {
		data() {
			return {
				loading: false,
				tableData: [{
					name: '382'
				}, {
					name: '382'
				}],
			}
		},
		mounted() {
			const result = [];
			for (let i = 0; i < 100; i++) {
				result.push({
					date: new Date().toISOString(),
					realname: '张三',
					name: '研发',
					post:'11'
				})
			}
			this.tableData = result;
		}
	}
</script>

<style lang="scss">
	.uni-container {
		height: 500rpx;
		
		margin-top: 50rpx;
		position: relative;
	}

	/* //表头固定样式 */
	::v-deep .tableHead {
		font-weight: bold;
		color: #333333;
		background: #F4F6FF;
		z-index: 20;
		position: absolute;
		top: 0rpx;
	}

	::v-deep .tableBody {
		height: 500px;
		overflow: scroll;
		margin-top: 44px;
		background-color: #fff;
	}
</style>

文章到处结束,希望对你有所帮助~

相关推荐
码兄科技1 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_916007471 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_916008891 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
郑州光合科技余经理2 天前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
白远山2 天前
城市电竞陪玩调度系统实战:从派单算法到多端协同的架构拆解
java·架构·uni-app·需求分析
白远山3 天前
上海24小时自助健身房系统软件开发实战指南
java·架构·uni-app·需求分析
白远山4 天前
上海24小时自助健身房系统软件开发实战指南:从需求到部署
java·架构·uni-app·需求分析
海鸥两三4 天前
小程序文档预览 · 进阶面试题
小程序·uni-app·uniapp
海鸥两三4 天前
微信小程序 PDF 预览实践(uni.downloadFile → uni.openDocument)
小程序·uni-app