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

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


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

其中,第一个表格的表头可能会有较好的固定效果,但是如果第二个表格的表头的列宽度与第一个表格的表头列宽度有区别,那问题就会显示出来了:第二个表格的表头会乱掉----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 options
var options = $.extend({}, $.chromatable.defaults, options); return this.each(function(){ // Add jQuery methods to the element
var $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 function
if (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 resized function 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);

jQuery,Table表头固定插件chromatable存在的问题及解决办法的更多相关文章
- table表头固定问题
table表头固定问题 原生的table表头在表格滚动时候无法固定,可以使用以下的方法进行模拟 1. 双table法 表头和表体各用一个table,这样会产生表格列对不齐的问题,可以使用colgrou ...
- HTML table表头固定
HTML table表头固定 说说我在最近项目中碰到的css问题吧,作为问题知识集合总结笔记: <!DOCTYPE html> <html> <head> < ...
- es6 Object.assign ECMAScript 6 笔记(六) ECMAScript 6 笔记(一) react入门——慕课网笔记 jquery中动态新增的元素节点无法触发事件解决办法 响应式图像 弹窗细节 微信浏览器——返回操作 Float 的那些事 Flex布局 HTML5 data-* 自定义属性 参数传递的四种形式
es6 Object.assign 目录 一.基本用法 二.用途 1. 为对象添加属性 2. 为对象添加方法 3. 克隆对象 4. 合并多个对象 5. 为属性指定默认值 三.浏览器支持 ES6 O ...
- jQuery绑定和解绑点击事件及重复绑定解决办法
原文地址:http://www.111cn.net/wy/jquery/47597.htm 绑点击事件这个是jquery一个常用的功能,如click,unbind等等这些事件绑定事情,但还有很多朋友不 ...
- table 表头固定 thead固定. 1) 使用jquery.freezeheader.js
方法一: 使用jquery.freezeheader.js 固定表头: 1-: 初始化: <!DOCTYPE html> <html lang="en"> ...
- 纯CSS实现table表头固定(自创备忘)
因为之前约定时候产品没说要表头固定,这次迭代测试突然提出这个需求,而且不知道因为什么未知原因非要这样不可--因为之前用了table标签做表单,而且也没用插件,这下就难了点,找另外一个前端前辈妹子商量了 ...
- jquery.min.map 404 (Not Found)出错的原因及解决办法
Chrome 更新后出现了 jquery.min.map 404 (Not Found) 的信息 这个到底是什么东西?查询了一下,得到了以下资料 JQuery 官方解释 摘录一下內容 从 jQuer ...
- Bootstrap Table表格一直加载(load)不了数据-解决办法
bootstrap-table是一个基于Bootstrap风格的强大的表格插件神器,官网:http://bootstrap-table.wenzhixin.net.cn/zh-cn/ 这里列出遇到的一 ...
- jquery中动态新增的元素节点无法触发事件解决办法
在使用jquery中动态新增的元素节点时会发现添加的事件是无法触发的,我们下面就为各位来详细的介绍此问题的解决办法. 比如做一个ajax读取留言列表的时候,每条留言后面有个回复按钮,class为“re ...
随机推荐
- Python实践练习:生成随机的测验试卷文件
题目 假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验.不妙的是,班里有几个坏蛋,你无法确信学生不会作弊.你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人 ...
- ARP数据包伪造
一台网络中的计算机,其传递到网络中的数据包的内容是完全由其软硬件逻辑决定的,软件可以操控硬件,硬件亦是一种特殊的软件,所以,接收者只根据数据包的内容,绝不可能判定此数据包的真正来源,一切都是可以伪 ...
- MySQL修改redo_log_size
MySQL5.5 步骤如下: 1. set global innodb_fast_shutdown = 0; 2. mysqladmin shutdown 3. 修改my.cnf innodb_log ...
- Linux大牛分享的7道经典面试题和秒收 offer 的技巧
笔者其实没有想到去面试,只是在智联上更新了一下简历,就陆陆续续接到很多猎头的邮件和电话,闲话少说,下面就分享给大家Linuxer的面试经历: 首先,猎头或者公司人资会把公司的介绍及岗位要求发到你邮箱( ...
- Java中异常体系
Java中的异常体系 Throwable,是一个类,表示可抛出的,它是所有的异常都继承的类. Throwable的子类,又分为两种,一种是Error,另一种是Exception. Error一般不是应 ...
- git服务器搭建问题
CentOS6.5本地搭建. 对于图形化的系统,可以联网,然后CTRL+ALT+F2可以切换到命令行,CTRL+ALT+F1可以切换回桌面图形化. 可以用SSH和FTP来连接服务器和传文件. (1 ...
- ubuntu 重启显卡报错 nvidia
1.我装玩显卡以后重启报错了 解决了5个小时才解决,先贴个当时报错的图 第一个图是没有加nomodeset 出先的 当你出现第二个图片的时候证明你离成功不远了 从头开始: 1.开机,出现 ubuntu ...
- c语言和设计模式
在网上看到一个博客专门写了关于设计模式的文章,感觉很有用.其实,我感觉数据结构 算法 设计模式 这三样同等重要啊. 数据结构 算法相对而言接触的比较多,但设计模式这个东西真的一头雾水,有时候觉得别人写 ...
- Git blame
一.简介 git blame可以将文件中的每一行的作者.最新的变更提交和提交时间展示出来. 二.实例 http://blog.csdn.net/hudashi/article/details/76 ...
- JAVA的StringBuffer类[转]
StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存 ...