表格添加固定表头,js 实现 tableFixedHead.js
来源:互联网 发布:淘宝不能评价 编辑:程序博客网 时间:2024/05/21 17:59
/* * Auther:zhaohailong * Date: 2017-09-05 */ /* 主要思想: 1>将原有的TABLE中的THEAD元素复制一份放在一个新的DIV(fixedheadwrap)中 2>设置这个fixedheadwrap为绝对位于原来的TABLE的THEAD位置 */ (function ($) { $.fn.extend({ FixedHead: function (options) { var op = $.extend({ tableLayout: "auto" }, options); return this.each(function () { var $this = $(this); //指向当前的table var $thisParentDiv = $(this).parent(); //指向当前table的父级DIV,这个DIV要自己手动加上去//以防重复调用多次生成if($thisParentDiv.siblings(".fixedheadwrap").length>0){//alert($thisParentDiv[0].clientWidth+" "+$thisParentDiv.siblings(".fixedheadwrap")[0].clientWidth);//if($thisParentDiv[0].clientWidth!=$thisParentDiv.siblings(".fixedheadwrap")[0].clientWidth){//$thisParentDiv.siblings(".fixedheadwrap").css("width",$thisParentDiv[0].clientWidth);//}$thisParentDiv.siblings(".fixedheadwrap").remove();//$thisParentDiv.wrap("<div class='fixedtablewrap'></div>").parent().css({ "position": "relative" }); //在当前table的父级DIV上,再加一个DIVvar x = $thisParentDiv.position();var fixedDiv = $("<div class='fixedheadwrap' style='clear:both;overflow:hidden;z-index:2;position:absolute;' ></div>").insertBefore($thisParentDiv)//在当前table的父级DIV的前面加一个DIV,此DIV用来包装tabelr的表头.css({ "width": $thisParentDiv[0].clientWidth, "left": x.left, "top": x.top });var $thisClone = $this.clone(true);$thisClone.removeAttr("id");//去掉属性id(dom中不能存在多个相同的id)$thisClone.removeAttr("name");//去掉属性(dom中不能存在多个相同的name)$thisClone.find("tbody").remove(); //复制一份table,并将tbody中的内容删除,这样就仅余thead,所以要求表格的表头要放在thead中$thisClone.appendTo(fixedDiv); //将表头添加到fixedDiv中$this.css({ "marginTop": 0, "table-layout": op.tableLayout });//当前TABLE的父级DIV有水平滚动条,并水平滚动时,同时滚动包装thead的DIV$thisParentDiv.scroll(function () {fixedDiv[0].scrollLeft = $(this)[0].scrollLeft;});//因为固定后的表头与原来的表格分离开了,难免会有一些宽度问题//下面的代码是将原来表格中每一个TD的宽度赋给新的固定表头var $fixHeadTrs = $thisClone.find("thead tr");var $orginalHeadTrs = $this.find("thead");$fixHeadTrs.each(function (indexTr) {var $curFixTds = $(this).find("td");var $curOrgTr = $orginalHeadTrs.find("tr:eq(" + indexTr + ")");$curFixTds.each(function (indexTd) {$(this).css("width", $curOrgTr.find("td:eq(" + indexTd + ")").width());});});}else{$thisParentDiv.wrap("<div class='fixedtablewrap'></div>").parent().css({ "position": "relative" }); //在当前table的父级DIV上,再加一个DIVvar x = $thisParentDiv.position();var fixedDiv = $("<div class='fixedheadwrap' style='clear:both;overflow:hidden;z-index:2;position:absolute;' ></div>").insertBefore($thisParentDiv)//在当前table的父级DIV的前面加一个DIV,此DIV用来包装tabelr的表头.css({ "width": $thisParentDiv[0].clientWidth, "left": x.left, "top": x.top });var $thisClone = $this.clone(true);$thisClone.removeAttr("id");//去掉属性id(dom中不能存在多个相同的id)$thisClone.removeAttr("name");//去掉属性(dom中不能存在多个相同的name)$thisClone.find("tbody").remove(); //复制一份table,并将tbody中的内容删除,这样就仅余thead,所以要求表格的表头要放在thead中$thisClone.appendTo(fixedDiv); //将表头添加到fixedDiv中$this.css({ "marginTop": 0, "table-layout": op.tableLayout });//当前TABLE的父级DIV有水平滚动条,并水平滚动时,同时滚动包装thead的DIV$thisParentDiv.scroll(function () {fixedDiv[0].scrollLeft = $(this)[0].scrollLeft;});//因为固定后的表头与原来的表格分离开了,难免会有一些宽度问题//下面的代码是将原来表格中每一个TD的宽度赋给新的固定表头var $fixHeadTrs = $thisClone.find("thead tr");var $orginalHeadTrs = $this.find("thead");$fixHeadTrs.each(function (indexTr) {var $curFixTds = $(this).find("td");var $curOrgTr = $orginalHeadTrs.find("tr:eq(" + indexTr + ")");$curFixTds.each(function (indexTd) {$(this).css("width", $curOrgTr.find("td:eq(" + indexTd + ")").width());});});} }); } }); })(jQuery);
阅读全文
0 0
- 表格添加固定表头,js 实现 tableFixedHead.js
- table表头和首列的表格固定-JQuery、js实现的Table表头固定
- js 实现 table 固定表头
- JS(JQ)实现table表格固定表头且表头可以随横向滚动而滚动
- JS(JQ)实现table表格固定表头且表头可以随横向滚动而滚动
- JS实现固定表头,实现内容滚动
- js实现 固定表头,拖动部分内容
- js表头固定实现思路汇总
- Angular.js+Bootstrap实现固定表头案例
- js、css实现table表头固定
- js方式固定表头
- js固定表头补充
- js固定表头
- js 固定表头
- JS实现固定表头与固定列名方法
- jquery.tablefix.js 表头固定
- 浅谈js中如何动态添加表头/表列/表格内容
- js实现表格添加数据
- APP.Config配置文件
- 跨域请求
- Direct Buffer vs. Heap Buffer
- Android ScrollView嵌套GridView或者ListView的时候,页面自动滚动到GridView或者ListView所在位置的问题
- io流
- 表格添加固定表头,js 实现 tableFixedHead.js
- 二叉树的遍历
- Dubbo 消费者配置及启动(二)
- Django:模型类
- Android给图片加文字和图片水印
- 网页资源集合
- 排序算法之快速排序
- jstl中c标签if-else
- java线程继承Thread方法和实现Runable方法区别