在最近的项目中需要将表格的表头固定,搜寻了大量的资料,发现chromatable插件还是比较方便的。但是当我在一个页面中多次使用chromatable固定对个表格的表头时问题就出现了,首先说明系统的前端架构使用了BootStrap,表格本身的头部宽度是自适应的。

使用表头固定的代码如下:

1                      //固定表头
2             $("#row1_table").chromatable({
3                 width : "718px",
4                 height : "335px",
5                 scrolling : "yes"
6             });

1                    //固定表头2
2             $("#row2_table").chromatable({
3                 width : "718px",
4                 height : "335px",
5                 scrolling : "yes"
6             });

其中,第一个表格的表头可能会有较好的固定效果,但是如果第二个表格的表头的列宽度与第一个表格的表头列宽度有区别,那问题就会显示出来了:第二个表格的表头会乱掉----Oh,my god !会很难看。

我观察了一下出现的问题,表头的列有的会挤到一起,我看了一下chromatable的源码,哦,好像该插件不能够在一个页面里适应两个不同table的头宽度,于是我就采取了如下的解决办法,既然插件无法自己分配宽度那我来给它分配,于是问题解决了,一个页面中无论你用多少次chromatable,给多少个table固定表头都会OK的,问题就是这么简单!O(∩_∩)O哈哈~

另附chromatable的js源码如下(jquery.chromatable.js):

/** File:        chromatable.js* Version:     1.3.0* CVS:         $Id$* Description: Make a "sticky" header at the top of the table, so it stays put while the table scrolls* Author:      Zachary Siswick* Created:     Thursday 19 November 2009 8:53pm * Language:    Javascript**/
(function($){$.chromatable = {// Default options
        defaults: {//specify a pixel dimension, auto, or 100%width: "900px", height: "300px",scrolling: "yes" }};$.fn.chromatable = function(options){// Extend default optionsvar options = $.extend({}, $.chromatable.defaults, options);return this.each(function(){// Add jQuery methods to the elementvar $this = $(this);var $uniqueID = $(this).attr("ID") + ("wrapper");//Add dimentsions from user or default parameters to the DOM elements$(this).css('width', options.width).addClass("_scrolling");$(this).wrap('<div class="scrolling_outer"><div id="'+$uniqueID+'" class="scrolling_inner"></div></div>');$(".scrolling_outer").css({'position':'relative'});$("#"+$uniqueID).css({'border':'1px solid #CCCCCC','overflow-x':'hidden','overflow-y':'auto','padding-right':'17px'                        });$("#"+$uniqueID).css('height', options.height);$("#"+$uniqueID).css('width', options.width);// clone an exact copy of the scrolling table and add to DOM before the original table// replace old class with new to differentiate between the two$(this).before($(this).clone().attr("id", "").addClass("_thead").css({'width' : 'auto','display' : 'block', 'position':'absolute', 'border':'none', 'border-bottom':'1px solid #CCC','top':'1px'}));// remove all children within the cloned table after the thead element$('._thead').children('tbody').remove();$(this).each(function( $this ){// if the width is auto, we need to remove padding-right on scrolling container    if (options.width == "100%" || options.width == "auto") {$("#"+$uniqueID).css({'padding-right':'0px'});}if (options.scrolling == "no") {$("#"+$uniqueID).before('<a href="#" class="expander" style="width:100%;">Expand table</a>');$("#"+$uniqueID).css({'padding-right':'0px'});$(".expander").each(function(int){$(this).attr("ID", int);$( this ).bind ("click",function(){$("#"+$uniqueID).css({'height':'auto'});$("#"+$uniqueID+" ._thead").remove();$(this).remove();});});//this is dependant on the jQuery resizable UI plugin$("#"+$uniqueID).resizable({ handles: 's' }).css("overflow-y", "hidden");}});// Get a relative reference to the "sticky header"$curr = $this.prev();// Copy the cell widths across from the original table$("thead:eq(0)>tr th",this).each( function (i) {$("thead:eq(0)>tr th:eq("+i+")", $curr).width( $(this).width());});//check to see if the width is set to auto, if not, we don't need to call the resizer functionif (options.width == "100%" || "auto"){// call the resizer function whenever the table width has been adjusted$(window).resize(function(){resizer($this);                                        });}});};// private function to temporarily hide the header when the browser is resizedfunction resizer($this) {// Need a relative reference to the "sticky header"$curr = $this.prev();$("thead:eq(0)>tr th", $this).each( function (i) {$("thead:eq(0)>tr th:eq("+i+")", $curr).width( $(this).width());});};})(jQuery);

转载于:https://www.cnblogs.com/lionelwu-qianqian/archive/2012/09/06/2673728.html

jQuery,Table表头固定插件chromatable存在的问题及解决办法相关推荐

  1. html table表头说明,HTML table表头固定

    HTML table表头固定 说说我在最近项目中碰到的css问题吧,作为问题知识集合总结笔记: table tbody { display: block; height: 200px; overflo ...

  2. table表头和首列的表格固定-JQuery、js实现的Table表头固定

    这个是最简单的方法,基于jQuery的解决方案,引入一个两个js就可以解决,当然也有使用css, position: sticky,这个我下一章节会讲它的优缺点,当然为了凸显jQuery的优点, 我就 ...

  3. JQuery实现的Table表头固定展示效果

    在线演示1 在线演示2 在线演示3 本地下载 大家可能经常需要展示数据或者报表,表头如果滚动上去后,查看数据的类别和字段是个麻烦事情,在这里我们推荐来自tympanus的一个JS实现的 table表头 ...

  4. jsp页面table表头固定-freezeheader

    该插件兼容firefox,charome,safari,ie10以上的版本. 1.导入jquery与freezeheader的包: 2.页面中的table: 3.通过ajax请求后台返回数据拼接tab ...

  5. 一眼就会table隔行换色、table表头固定、隔行色、鼠标移动行换色

    抽出时间整理笔记:table头部固定.超出宽度可横向移动,主信息隔行换色.鼠标移动变色.举例: code : <!DOCTYPE HTML> <head><meta ht ...

  6. HTML table表头固定

    实现表格表头的固定,采用纯css的方式 具体如下 <!DOCTYPE HTML> <html> <head> <meta http-equiv="C ...

  7. table表头固定,底部可滚动

    需求:实现table表头固定,表体可滚动 思路: tbody使用display:block,固定高度,并设置over-y:scroll, thead和tr,使用display:table,使用tabl ...

  8. table 表头固定

    一.table 表头固定说明.我们将表头和表内容分成两个表格就可以解决该问题. 二.代码. css代码:里面定死的宽高只是为啦方便看效果,实际中需要使用js计算出来. * {box-sizing: b ...

  9. java 表头固定_BootStrap的table表头固定tbody滚动的实例代码

    关于bootstrap table其他知识不多说,直接给大家贴代码了. 关键代码如下所示: 栏目一栏目二栏目三栏目一栏目二栏目三 星期一星期二星期三星期一星期二星期三星期一星期二 星期二 星期三星期一 ...

  10. html5手机表头设置,html Table 表头固定的实现

    本文介绍了html table 表头固定的实现,分享给大家,具体如下: 合同号签约客户发布客户合同状态选定条件的发布周期额度 看看css *{ padding:0; margin:0; } th{ b ...

最新文章

  1. 咕泡学院:(1)唐宇迪python课程作业
  2. Linux~Sh脚本一点自己的总结
  3. C语言 找数码是否存在
  4. linux中vi大括号enter缩进,格式 – 如何在vi中对齐代码(大括号,括号等)?
  5. 1536299 2013 - PPR item change - Genil mode
  6. 01 ftp上传简单示例服务端
  7. 使用Spring Task完成定时任务
  8. leetcode1442. 形成两个异或相等数组的三元组数目
  9. 前端学习(2885):如何短时间内实现v-for 搭建环境 业务模块设计
  10. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)
  11. 别让自己成为一名废弃的程序员
  12. unity零基础学习
  13. 推荐 20 款 IDEA 主题!
  14. 在mac上开启httpServer服务
  15. 什么是托管C++ (managed C++)
  16. 关于使用手机电池替换3节干电池的尝试
  17. CTF入门学习思维导图
  18. 乔治亚大学计算机科学,乔治亚大学的计算机科学排名,真得稳重考察
  19. WebRtc以Trickle ICE形式去进行pair
  20. 魔兽修改默认服务器,魔兽怎么设置默认服务器

热门文章

  1. bgsave配置与工作流程
  2. 3 矩阵运算_FlyAI小课堂:小白学PyTorch(11) 常见运算详解
  3. Java 8 Base64 编码解码
  4. rhel7.5安装mysql8.0教程_RHEL7.5下mysql 8.0.11安装教程
  5. mybatis plugins_[Mybatis]-[基础支持层]-插件-多个插件执行顺序
  6. python经典实例下载_python经典实例
  7. 无约束最优化(二) 共轭方向法与共轭梯度法
  8. TensorFlow2.0学习使用笔记
  9. 卡巴斯基6.0的授权文件
  10. 【转】每一种创伤,都是一种成熟