uniapp 固定首行和固定列的表格

该博客介绍了如何在uniapp中创建一个自定义样式的表格,包括使用sass进行样式编写,并展示了如何通过HbuilderX安装sass插件。示例中,表格实现了首行和末尾两列的固定效果,同时提供了代码实现行变色的逻辑,适用于移动端应用的表格展示需求。
摘要由CSDN通过智能技术生成

准备

在uniapp应用中新建一个页面;
css部分用到了sass,开发工具是HbuilderX的话安装sass插件即可。

效果

第一个一个表格是默认样式用作对照,第二个表格是固定了末尾两列和首行的自定义样式表格。
效果截图
在这里插入图片描述

代码

<template>
    <view>
		<view class="table-box">
			<table cellspacing="0px" cellpadding="0px">
				<thead>
					<tr class="table-new-line" >
						<th>第一项</th>
						<th>第二项</th>
						<th>第三项</th>
						<th>第四项</th>
						<th>第五项</th>
						<th>第六项</th>
						<th class="hold2 zd">
							固定2
						</th>
						<th class="hold1 zd">
							固定1
						</th>
					</tr>
				</thead>
				<tbody>
					<tr class="table-new-line" v-for="(item, index) in 10" :key="index">		
						<td>第一项</td>
						<td>第二项
							<checkbox style="transform:scale(0.8)" :checked="true" />
						</td>
						<td>第三项</td>
						<td>第四项</td>
						<td>第五项</td>
						<td>第六项</td>
						<td class="hold2">
							<checkbox style="transform:scale(0.8)" :checked="true" />
						</td>
						<td class="hold1">
							<button class="table-button">
								按钮
							</button>
						</td>
					</tr>
				</tbody>
			</table>
		</view>
		<view class="table-box">
			<table id="CustomTable" ref="CustomTable" cellspacing="0px" cellpadding="0px">
				<thead>
					<tr class="table-new-line" >
						<th>第一项</th>
						<th>第二项</th>
						<th>第三项</th>
						<th>第四项</th>
						<th>第五项</th>
						<th>第六项</th>				
						<th class="hold2 zd">
							固定2
						</th>
						<th class="hold1 zd">
							固定1
						</th>
					</tr>
				</thead>
				<tbody>
					<tr class="table-new-line" v-for="(item, index) in 10" :key="index">						
						<td>第一项</td>
						<td>第二项
							<checkbox style="transform:scale(0.8)" :checked="true" />
						</td>
						<td>第三项</td>
						<td>第四项</td>
						<td>第五项</td>
						<td>第六项</td>
						<td class="hold2">
							<checkbox style="transform:scale(0.8)" :checked="true" />
						</td>
						<td class="hold1">
							<button class="table-button">
								按钮
							</button>
						</td>
					</tr>
				</tbody>
			</table>
		</view>
	</view>
</template>
 
<script>
export default {
    data() {
        return {
			
        };
    },
	onReady() {
		this.altRows('CustomTable');
	},
	methods:{
		altRows(id){
			// #ifdef H5
			// 表格行变色,在不能使用document的情况下不适用这个方法
			if(document.getElementsByTagName){
				var table = document.getElementById(id); 
				var rows = table.getElementsByTagName("tr");
				// debugger
				for(let i = 0; i < rows.length; i++){         
					if(i % 2 == 0){
						rows[i].className += " evenrowcolor";
					}else{
						rows[i].className += " oddrowcolor";
					}    
				}
			}
			// #endif
			// #ifdef APP-PLUS
			
			// #endif
		},
		
	}
};
</script>

<style scoped lang="scss">
		.table-box{
			margin: auto;
            width: 98vw;
            overflow:auto;
            height:35vh;  /* 设置固定高度 */
        }
        table#CustomTable {
			/* 去除间隔 */
			border-collapse:collapse;
			/* 打开间隔 */
			/* border-collapse : separate; */
			border:0.5px solid white;
            table-layout: fixed;
			text-align:center;
			font-size: 28rpx;
			/* 固定宽度 */
            width: 180rpx;
			height:45rpx;
			thead tr th{
			     position:sticky;
			     top:0; /* 首行固定在头部  */
			 };
			td, th {
			    /* 设置td,th宽度高度 */
				border:0.5px solid white;
				/* background-color: #ffffff; */
				width: 180rpx;
			    height:45rpx;
			};
			th:not(.is-hidden):last-child, td:not(.is-hidden):last-child{
			    right:-1px;};
			th {
				z-index: 1;
			    background-color:#ecf5ff;
			}
			th:nth-last-child(1), td:nth-last-child(1){
				position:sticky;
				right:0;
				z-index:2;
				width: 180rpx;
				background-color: #c7e2d5; 
			}
			th:nth-last-child(2), td:nth-last-child(2){
				position:sticky;
				right:180rpx;
				z-index:2;
				width: 100rpx;
				background-color: #c7e2d5; 
			}
			th.zd{
				 z-index:3;
			}
        }

		.first-item{	
			display: flex;
			justify-content: center;
			/* width: 28%; */
		}
		.second-item{
			display: flex;
			justify-content: center;
			width: 80%;
		}
		.auto-new-line{
			overflow : hidden;
			text-overflow: ellipsis;
 			display: -webkit-box;
			/* -webkit-line-clamp: 2; */
			-webkit-box-orient: vertical;
			word-wrap:break-word;
			word-break:break-all;
		}
		
		.table-new-line{
			overflow : hidden;
			text-overflow: ellipsis;
			word-wrap:break-word;
			word-break:break-all;
		}
		.table-button{
			border-radius: 5rpx;
			font-size: 18rpx;
			padding: 0 0;
			margin: 10rpx 10rpx;
			background-color: #0081ff;
			color: #ffffff;
		}
		.oddrowcolor{
			background-color:#ecf5ff;
		}
		.evenrowcolor{
			background-color:#f5f7fa;
		}
</style>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值