vue实现变态表格表头和列固定

复杂表格表头和列固定

对于这个问题,相信网上也会有很多答案,很多插件或者是类似的案例,一下是我参考别人所写的案例自己改造的一个变态表格
1.实现的效果是,左右滚动,左边列固定,上下滚动,上边头部固定
2.要实现这个效果,具体方法是分为四个表格,用到了同步滚动的方法
3.反正很复杂,直接上代码吧
template:

<template>
	<div class="table-scroll-mod">
		<div class="table-scroll-box" ref="tableScrollBox">
			<!-- 头部 -->
			<div class="fixed-head" ref="tableFixedHead">
				<table>
					<thead>
						<tr>
							<div class="top-btn">
								<div class="top-before-table">
									<div class="pagination-btn">
										<el-button type="primary" style="margin-right:0.05rem;">自动量化打分</el-button>
										<el-button type="primary" style="margin-right:0;">复制打分值</el-button>
									</div>
									<div class="count-description">
										<p class="counter">合计(满分70分):</p>
										<p class="descrip">评分描述</p>
									</div>
								</div>
								<div class="score-table">
									<ul class="ul-companyList">
										<li v-for="(com,index) in companLsit" :key="index">
											<span>{
  {com.score}}</span>
											<div class="com-name">
												<span>{
  {com.comName}}</span>
												<a class="submitScore">提交</a>
											</div>
										</li>
									</ul>
								</div>
							</div>
						</tr>
					</thead>
				</table>
			</div>
			<!-- 左边 -->
			<div class="fixed-left" ref="tableFixedLeft">
				<table>
					<thead>
						<tr>
							<div class="top-btn">
								<div class="pagination-btn">
									<el-button type="primary" style="margin-right:0.05rem;">自动量化打分</el-button>
									<el-button type="primary" style="margin-right:0;">复制打分值</el-button>
									<v-complex :pageList="groupPageList" @fnPage="loadCompanyInfo"></v-complex>
								</div>
								<div class="count-description">
									<p class="counter">合计(满分70分):</p>
									<p class="descrip">评分描述</p>
								</div>
							</div>
						</tr>
					</thead>
					<tbody>
						<div class="fz-table">
							<div class="before-table">
								<ul class="sx-title">
									<li>属性名称</li>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现表格左侧固定的方法有很多种,其中一种比较简单的方式是使用CSS的position属性。具体实现步骤如下: 1. 将表格的整体布局设置为position:relative; 2. 将表格左侧需要固定使用position:absolute;定位到左侧; 3. 将固定的宽度设置为固定值; 4. 给固定设置z-index属性,使其在表格中处于最上层; 5. 设置表格容器的overflow属性为auto或scroll,使其可以滚动; 6. 在滚动时,通过JavaScript获取表格容器的scrollTop值,将固定的top值设置为scrollTop值减去表格容器顶部距离文档顶部的距离。 同时,实现头部固定上下左右滑动也可以使用position属性,只需将表格容器和表格头部分别设置为position:fixed;即可。在滚动时,通过JavaScript获取表格容器的scrollTop和scrollLeft值,设置表格头部的top和left值即可实现固定上下左右滑动。 下面是一份示例代码,供参考: HTML部分: ``` <div class="table-container"> <table> <thead> <tr> <th>固定</th> <th>表头1</th> <th>表头2</th> <th>表头3</th> <th>表头4</th> <th>表头5</th> </tr> </thead> <tbody> <tr> <td class="fixed-col">固定1</td> <td>内容1</td> <td>内容2</td> <td>内容3</td> <td>内容4</td> <td>内容5</td> </tr> <tr> <td class="fixed-col">固定2</td> <td>内容1</td> <td>内容2</td> <td>内容3</td> <td>内容4</td> <td>内容5</td> </tr> <!-- ... --> </tbody> </table> </div> ``` CSS部分: ``` .table-container { position: relative; width: 100%; height: 400px; overflow: auto; } table { width: 100%; border-collapse: collapse; } thead th { background-color: #eee; } tbody td { border: 1px solid #ccc; } .fixed-col { position: absolute; left: 0; top: 0; z-index: 1; width: 100px; } .fixed-col + td { padding-left: 100px; } thead { position: fixed; top: 0; left: 0; z-index: 2; } tbody { margin-top: 40px; /* 表头高度 */ } tbody tr:first-child td { border-top: none; } tbody tr td:first-child { border-left: none; } ``` JavaScript部分: ``` var tableContainer = document.querySelector('.table-container'); var fixedCol = document.querySelector('.fixed-col'); var tableHead = document.querySelector('thead'); tableContainer.addEventListener('scroll', function() { fixedCol.style.top = this.scrollTop + 'px'; tableHead.style.left = -this.scrollLeft + 'px'; }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值