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> ...
随机推荐
- 09、高级编程之基于排序机制的wordcount程序
package sparkcore.java; import java.util.Arrays; import java.util.Iterator; import org.apache.spark. ...
- Go web编程实例
1. go web编程入门 记录个web编程例子方便以后使用. 主要有: chan的使用(带缓存,不带缓存) client发起get/post请求 server解析get/post请求参数 http. ...
- jvm理论-常量池-string
字符串常量池-常量项(cp_info)结构 CONSTANT_String_info{ u1 tag=8; u2 string_index;//存放 CONSTANT_Utf8_info 指针 } C ...
- ubuntu下安装配置apache2与php
1:安装apache2 sudo apt install apache2 2:修改端口号 sudo vi /etc/apache2/ports.conf 3:修改跟目录 在 /etc/apache2/ ...
- IDEA创建多个模块MavenSpringBoot项目
最近在学习springboot,先从创建项目开始,一般项目都是一个项目下会有多个模块,这里先创建一个最简单的实例,一个项目下有一个springboot模块项目提供web服务,引用另一个java项目(相 ...
- Ubuntu 16.04 升级git
To update git on Ubuntu 12.04 just follow this line of commands: sudo apt-get install python-softwar ...
- laravel中及其常用的一些函数方法(自己看)和技巧(不断添加中)
手册:https://laravelacademy.org/ 1.中间件的定义Middleware 2.路由的定义和写法 3.控制器Controller之Request 4.控制器Controller ...
- Object C函数指针@selector
其作用相当于函数指针,现在我看到的大多说用法都是在调用某些函数需要传递一个 函数指针 参数时,使用@selector.它会在当前类里面查找selector后面所跟的函数,返回一个SEL类型的值. S ...
- svg中实现文字随曲线走向,HTML直接写和JavaScript创建对象两种方式
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...
- SQL Server 2016新特性:Live Query Statistics
SSMS可以提供可以查看正在执行的计划.live query plan可以查看一个查询的执行过程,从一个查询计划操作到另外一个查询计划操作.live query plan提供了整体的查询运行进度和操作 ...