html table 固定表头和列
/**************************************************************** jQuery 插件. 功能: 固定表格标题行或列头 Version: 1.0 调用方法:
$('#myTable').fixTable(
pRow, //可滚动区域第一行的行号
pCol, //可滚动区域第一列的列号
splitColor, //(可选)固定区域与滚动区域的分隔线颜色
); ****************************************************************/ jQuery.fn.extend({
fixTable: function (pRow, pCol, splitColor) {
//滚动条宽度
var scrW = 16; //设置分隔线颜色
if (!splitColor) {
splitColor = '#333';
} //得到表格本身
var t = $(this);
var pid = 'fixbox_' + t.attr('id'); t.show(); //得到表格实际大小
var tw = t.outerWidth(true);
var th = t.outerHeight(true); //在外部包一个DIV,用来获取允许显示区域大小
t.wrap("<div id='" + pid + "' ></div>");
var p = $('#' + pid);
p.css({
width: '100%',
height: '100%',
border: '0px',
margin: '0 0 0 0',
padding: '0 0 0 0'
}); //允许显示区域大小
t.hide();
var cw = p.outerWidth(true);
var ch = p.outerHeight(true);
t.show(); //拿到表格的HTML代码
var thtml = p.html(); //判断是否需要固定行列头
if (tw <= cw && th <= ch) {
return;
}
//判断需要固定行/列/行列
var fixType = 4; //全固定
if (tw <= cw - scrW) { //宽度够, 高度不够
fixType = 1; //行固定
cw = tw + scrW;
} else if (th <= ch - scrW) { //高度够, 宽度不够
fixType = 2; //列固定
ch = th + scrW;
}
//固定单元格的位置
var w1 = 0;
var h1 = 0; var post = t.offset(); var p1, p2, p3, p4;
if (fixType == 4) { //行头列头都需固定
//取出指定行列单元格左上角的位置,单位px
var pos = t.find('tr').eq(pRow).find('td').eq(pCol).offset(); w1 = pos.left - post.left;
h1 = pos.top - post.top; var tmp = '<table style="background: #ECE9D8;" ';
tmp += 'border="0" cellspacing="0" cellpadding="0">';
tmp += '<tr><td style="border-right: 1px solid ' + splitColor +
';border-bottom: 1px solid ' + splitColor + '">';
tmp += '<div id="' + pid + '1"></div></td>';
tmp += '<td style="border-bottom: 1px solid ' + splitColor +
';"><div id="' + pid + '2"></div></td></tr>';
tmp += '<tr><td valign="top" style="border-right: 1px solid ' +
splitColor + ';"><div id="' + pid + '3"></div></td>';
tmp += '<td><div id="' + pid + '4"></div></td></tr>';
tmp += '</table>'; p.before(tmp); $('div[id^=' + pid + ']').each(function () {
$(this).css({
background: 'white',
overflow: 'hidden',
margin: '0 0 0 0',
padding: '0 0 0 0',
border: '0'
});
});
p1 = $('#' + pid + '1');
p2 = $('#' + pid + '2');
p3 = $('#' + pid + '3');
p4 = $('#' + pid + '4'); //左上角方块
p1.html(thtml).css({ width: w1 - 1, height: h1 - 1 });
p1.find('table:first').attr('id', undefined); //右上方块
p2.html(thtml).css({ width: cw - w1 - scrW, height: h1 - 1 });
p2.find('table:first').css({
position: 'relative',
left: -w1
}).attr('id', undefined); //左下方块
p3.html(thtml).css({ width: w1 - 1, height: ch - h1 - scrW });
p3.find('table:first').css({
position: 'relative',
top: -h1
}).attr('id', undefined); //主方块
p4.append(p).css({
width: cw - w1,
height: ch - h1,
overflow: 'auto'
}); t.css({
position: 'relative',
top: -h1,
left: -w1
}); p.css({ width: tw - w1, height: th - h1, overflow: 'hidden' }); p4.scroll(function () {
p2.scrollLeft($(this).scrollLeft());
p3.scrollTop($(this).scrollTop());
});
} else if (fixType == 1) { //只需固定行头
var pos = t.find('tr').eq(pRow).find('td').first().offset();
h1 = pos.top - post.top; var tmp = '<table style="background: #ECE9D8;" ';
tmp += 'border="0" cellspacing="0" cellpadding="0">';
tmp += '<tr><td style="border-bottom: 1px solid ' + splitColor + '">';
tmp += '<div id="' + pid + '1"></div></td></tr>';
tmp += '<tr><td><div id="' + pid + '2"></div></td></tr>';
tmp += '</table>'; p.before(tmp); $('div[id^=' + pid + ']').each(function () {
$(this).css({
background: 'white',
overflow: 'hidden',
margin: '0 0 0 0',
padding: '0 0 0 0',
border: '0'
});
});
p1 = $('#' + pid + '1');
p2 = $('#' + pid + '2');
//上方方块
p1.html(thtml).css({ width: tw, height: h1 - 1 });
p1.find('table:first').attr('id', undefined); //主方块
p2.append(p).css({
width: cw + 1,
height: ch - h1,
overflow: 'auto'
}); t.css({
position: 'relative',
top: -h1,
left: 0
}); p.css({ width: tw, height: th - h1, overflow: 'hidden' });
} else if (fixType == 2) { //只需固定列头
var pos = t.find('tr').first().find('td').eq(pCol).offset();
w1 = pos.left - post.left; var tmp = '<table style="background: #ECE9D8;" ';
tmp += 'border="0" cellspacing="0" cellpadding="0">';
tmp += '<tr><td valign="top" style="border-right: 1px solid ' + splitColor + '">';
tmp += '<div id="' + pid + '1"></div></td>';
tmp += '<td><div id="' + pid + '2"></div></td></tr>';
tmp += '</table>'; p.before(tmp); $('div[id^=' + pid + ']').each(function () {
$(this).css({
background: 'white',
overflow: 'hidden',
margin: '0 0 0 0',
padding: '0 0 0 0',
border: '0'
});
});
p1 = $('#' + pid + '1');
p2 = $('#' + pid + '2');
//上方方块
p1.html(thtml).css({ width: w1 - 1, height: th });
p1.find('table:first').attr('id', undefined); //主方块
p2.append(p).css({
width: cw - w1,
height: ch + 1,
overflow: 'auto'
}); t.css({
position: 'relative',
top: 0,
left: -w1
}); p.css({ width: tw - w1, height: th, overflow: 'hidden' });
}
}
});
转自:http://bbs.csdn.net/topics/330147945
html table 固定表头和列的更多相关文章
- asp.net table表格表头及列固定实现
http://blog.csdn.net/zdw_wym/article/details/48630337 在开发中常会遇到table表格中列特别多,下拉后,表头就看不见了,水平滚动后,第1.2列就看 ...
- vue表格实现固定表头首列
前言 最近在做vue移动端项目,需要做一个可以固定表头首列的表格,而且由于一些原因不能使用任何UI插件,网上找了很久也没什么好方法,所以在解决了问题之后,写下了这篇文章供后来人参考,文章有什么错漏的问 ...
- HTML table固定表头
最近尝试了几种HTML的table固定表头的方法..额...各有利弊,但很尴尬..... 1.thead和tbody的display设置为block; 这种可以实现,但是需要提前设置好每个th和td的 ...
- table固定前两列和最后一列,其他滑动显示
网上搜的基本都是4个table做的,数据处理比较麻烦,写了个一个table的,此示例只固定了前两列和最后一列,和网上的不太一样. 网上搜的基本都是4个table做的,数据处理比较麻烦,写了个一个tab ...
- jquery固定表头和列头
1.对网上的开源方法稍作了些修改 <script type="text/javascript">// <![CDATA[ function FixTable(Ta ...
- Table 固定表头的几种方法
<style type="text/css"> /*所有内容都在这个DIV内*/ div.all { border: 3px solid #FF00FF; width: ...
- table 固定表头
1 .table { border-collapse: collapse; } .table th { display: table-cell; } .fixedThead {//thead disp ...
- element table固定表头,表的高度自适应解决方法
主要是通过在mounted生命周期中,改变tableHeight的值,来让表格的高度自适应. 标签: <el-table ref="table" :data="ta ...
- (转)supertable像excel那样固定table的表头和第一列
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...
随机推荐
- RbbitMQ消息队列及python实现
1.简介 RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的 ...
- SSE图像算法优化系列六:OpenCv关于灰度积分图的SSE代码学习和改进。
最近一直沉迷于SSE方面的优化,实在找不到想学习的参考资料了,就拿个笔记本放在腿上翻翻OpenCv的源代码,无意中看到了OpenCv中关于积分图的代码,仔细研习了一番,觉得OpenCv对SSE的灵活运 ...
- Docker CE 镜像源站
sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-proper ...
- NOIP2010提高组 机器翻译
OJ提交地址:https://www.luogu.org/problemnew/show/P1540 http://noi.openjudge.cn/ch0112/07/ 总时间限制: 1000ms ...
- 使用 TRESTClient 与 TRESTRequest 作为 HTTP Client(转)
使用 TRESTClient 与 TRESTRequest 作为 HTTP Client 转自:http://www.cnblogs.com/dennieschang/p/6966403.html ...
- Effective Java 第三版——88. 防御性地编写READOBJECT方法
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- pandas.Dataframe复杂条件过滤
https://stackoverflow.com/questions/11418192/pandas-complex-filter-on-rows-of-dataframe mask = df.ap ...
- nginx实现限速
项目中有一个需求,需要限制每个容器的网速,避免某些容器占用太多资源,导致其他容器无法使用,但是docker对于网速的限制支持的有点弱,由于容器中的所有进程和APP的交互都是通过nginx的,所以就想到 ...
- android手机测试中如何查看内存泄露
(一) 生成.hprof文件生成.hprof 文件的方法有很多,而且Android 的不同版本中生成.hprof 的方式也稍有差别,我使用的版本的是2.1,各个版本中生成.prof 文件的方法请参考: ...
- 第四百零三节,python网站在线支付,支付宝接口集成与远程调试,
第四百零三节,python网站在线支付,支付宝接口集成与远程调试, windows系统安装Python虚拟环境 首先保证你的系统已经安装好了Python 安装virtualenv C:\WINDOWS ...