前言

最近在做vue移动端项目,需要做一个可以固定表头首列的表格,而且由于一些原因不能使用任何UI插件,网上找了很久也没什么好方法,所以在解决了问题之后,写下了这篇文章供后来人参考,文章有什么错漏的问题欢迎评论交流。

效果

思路

要实现固定首行首列

除了使用各种UI框架插件外,那就是自己用原生写啦

首先我们理一下思路

如何固定首行首列呢?

可能每个人有不同的想法

我这里当然介绍的是我自己的想法

那就是把首列表头和表格主内容分割开来,如下图

不过这样虽然固定了表头首列

但还是不能实现我们想要的效果

因为你会发现当你滑动tbody的时候

表头和首列并不会移动

相信聪明的你已经有了解决的办法了

那就是给tbody添加一个滑动监听

滑动的时候会触发事件

引起表头和首列的移动

这就是本文固定表头首列的思路

代码实现

template:

<template>
<div class="pages" ref="pages">
<div class = "content" ref="table">
<!--首列固定-->
<div class = "left-content">
<div class = "table-head">
<table class= "full-table">
<thead>
<tr v-for = "header in tableHeader">
<th class = "firstCol"
v-for = "(b,index) in header"
v-if="index==0">
<p>{{b}}</p>
</th>
</tr>
</thead>
</table>
</div>
<div class="table-left">
<div class = "table"
ref="firstColLayer"
data-_ref="firstColLayer"
>
<table class= "full-table">
<tbody>
<tr v-for="row in dataSheet">
<td v-for="(c,index) in row" v-if="index==0">
<p>{{c}}</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class = "right-content" ref="right">
<!--首行固定-->
<div class = "table-head"
ref="firstRowLayer"
data-_ref="firstRowLayer">
<table class= "full-table">
<thead>
<tr v-for = "header in tableHeader">
<th
v-for = "(b,index) in header"
v-if="index!=0 && index!=1"
style="width:101px"
>
<p>{{b}}</p>
</th>
</tr>
</thead>
</table>
</div>
<!--正常表格内容(只有表格内容,没有表头和首列)-->
<div class="table"
style="overflow:scroll"
ref="tableContainer"
@scroll="tableDivScroll($event)"
>
<table class="content-table">
<tbody ref="tbody">
<tr v-for="row in dataSheet">
<td v-for="(c,index) in row"
v-if="index!=0 && index!=1"
>
<p>{{c}}</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</template>

JavaScript:

<script>
module.exports = (function(that) {
return {
data: function(that) {
const tableHeader = that.dataSheet;
const dataSheet = that.dataSheet;
return {
dataSheet: dataSheet,
tableHeader: tableHeader,
};
},
methods: {
tableDivScroll (event) {
const $target = this.$refs.tableContainer
// 首行固定
this.$refs.firstRowLayer.scrollLeft = $target.scrollLeft
// 首列固定
this.$refs.firstColLayer.scrollTop = $target.scrollTop
},
//定一个生命周期钩子监听变动
mounted:function () {
let maxHeight = window.screen.height
document.body.style.overflow='hidden';
this.$refs.right.style.width=""+this.$refs.table.offsetWidth-107+"px";//这里的减107是减去左侧div宽度
console.log(this.placeholderTop)
}
}
})(this);
</script>

CSS:

<style scoped>
body{
overflow:hidden
}
.pages{
height:100%;
overflow:hidden;
}
.content{
margin-top:73px;
margin-left:17px;
width:100%;
}
.left-content{
width:101px;
float:left;
}
.right-content{
float:left
}
.table-body{
width:100%;
overflow:auto;
}
.table-head{
width:100%;
overflow:hidden;
}
.left-content .table-body{
overflow:hidden;
}
.table-left .table{
height:400px;
background-color:#FFFFFF;
overflow:hidden;
margin-right:0;
padding-right:0;
}
table::-webkit-scrollbar{display:none}
.content-table th, .full-table th{
font-size:14px;
font-family:PingFangSC-Regular;
background:#EAEFF3;
font-weight:400;
color:#176BED;
height:40px;
line-height:40px;
text-align:center;
}
.content-table td, .full-table td {
line-height: 35px;
text-align: center;
word-wrap: break-word;
word-wrap: normal\0;
overflow: hidden;
-o-text-overflow: ellipsis;
text-overflow: ellipsis;
}
th,td p{
width:101px;
display: inline-block;
line-height:14px;
padding:auto 0;
margin:auto 0;
vertical-align: middle;
}
.content-table {
display:block;
background-color:#FFFFFF;
}
thead,tbody{
background-color:#FFFFFF;
}
</style>

Ps:有什么问题可以在评论区一起探讨

vue表格实现固定表头首列的更多相关文章

  1. vue 表格树 固定表头

    参考网上黄龙的表格树进行完善,并添加固定表头等的功能,目前是在iview的项目中实现,如果想在element中实现的话修改对应的元素标签及相关写法即可. <!-- @events @on-row ...

  2. jquery固定表头和列头

    1.对网上的开源方法稍作了些修改 <script type="text/javascript">// <![CDATA[ function FixTable(Ta ...

  3. html table 固定表头和列

    /**************************************************************** jQuery 插件. 功能: 固定表格标题行或列头 Version: ...

  4. Saiku如何固定查询结果table的表头和首列

    在使用saiku查询的时候,当“行”和“列”的维度内容过多时,在查看时只看到数据,不知道是什么数据,维度不清楚,得来回拖动滚动条才行,所以同事提出想要固定“表头”和“首列”. 在网上找了一些现成的插件 ...

  5. bootstrap-table固定表头固定列

    1.引入 bootstrap依赖于jquery bootstrap-table依赖于bootstrap,所以都需要引入 2. bootstrap-table有两种方式,html.js <tabl ...

  6. bootstrap 冻结表格,冻结表头

    需要的文件下载: bootstrap-table:https://github.com/wenzhixin/bootstrap-table bootstrap-table-fiex-column:ht ...

  7. asp.net table表格表头及列固定实现

    http://blog.csdn.net/zdw_wym/article/details/48630337 在开发中常会遇到table表格中列特别多,下拉后,表头就看不见了,水平滚动后,第1.2列就看 ...

  8. H5手机开发锁定表头和首列(惯性滚动)解决方案

    前端时间移动端在做表格的时候需要这个功能,由于还有实现类似原生的惯性滚动功能,于是使用了iscroll插件. iscroll插件下载地址:iscroll5 该功能demo github地址: http ...

  9. CSS样式实现表头和列固定

    效果图:第一行和第一列固定       <!DOCTYPE html> <html lang="zh"> <head> <meta cha ...

随机推荐

  1. linux下svn(subversion)服务端添加工程及配置权限

    linux下svn(subversion)服务端添加工程及配置权限 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/9010507.html 此篇我只是将所做过的 ...

  2. Docker 跨主机网络方案分析

    PS:文章首发公众号,欢迎大家关注我的公众号:aCloudDeveloper,专注技术分享,努力打造干货分享平台,二维码在文末可以扫,谢谢大家. 上篇文章介绍了容器网络的单主机网络,本文将进一步介绍多 ...

  3. 回忆一下我的运维时期 关于Impact的架构服务器集群

    Impact EDMP平台  Email Direct Marketing Platfrom   电子邮件营销平台 EDM 是 Email Direct Marketing 的缩写,即电子邮件营销,简 ...

  4. 10个Python练手小程序,学习python的很好的资料

    [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去  掉不满足条件的排列 ...

  5. SSH整合配置文件概括

    配置方式一:struts.xml, applicationContext.xml(hibernate.cfg.xml配置信息写入spring配置文件中) (版本号, struts2:2.3.15; s ...

  6. golang自定义路由控制实现(二)-流式注册接口以及支持RESTFUL

        先简单回顾一下在上一篇的文章中,上一篇我主要是结合了数组和Map完成路由映射,数组的大小为8,下标为0的代表Get方法,以此类推,而数组的值则是Map,键为URL,值则是我们编写对应的接口.但 ...

  7. Sublime 3基于python环境的使用

    .Ctrl+Shift+R 自动调整格式 打开Sublime Text 新建一个Python的编译系统 { "cmd": ["C:\\python\\python.exe ...

  8. redis与python交互

    import redis #连接 r=redis.StrictRedis(host="localhost",port=6379,password="sunck" ...

  9. Android FoldingLayout 折叠布局 原理及实现(二)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/44283093,本文出自:[张鸿洋的博客] 1.概述 在上一篇Android Fo ...

  10. Ubuntu配置SecureCRT登录

    1. 命令行切换到root用户 备注:ubuntu默认root用户没有设置密码,切换需要首先设置密码 sudo passwd root 按照提示输入当前用户密码 按照提示输入要设置的root用户密码 ...