User login

Colorize - jQuery Table Plugin - 2.0.0

Colorize is a jQuery plugin to add background color to alternate HTML table rows, highlight a row/column on mouse over, and colorize a row/column when you click your mouse button over it. You can colorize as many table rows or columns as you want. A repeat mouse click reverts the row/column to the original background color.

How to use it?
Use it with default options (colorizes a row)
$('#yourTableId').colorize() ;

Optional parameters:

altColor : alternate row background color. The default is '#ECF6FC', 'none' can be used for no alternate background color.
bgColor : background color (The default background color is white).
hoverColor : background color when you hover a mouse over a row. The default is '#BCD4EC'.
hoverClass: style class for mouseover; takes precedence over hoverColor property
hiliteColor : row highlight color. The default color is 'yellow'. 'none' can be used to disable highlighting.
hiliteClass : style class to be used to highlight a row or a column, takes precedence over the hiliteColor setting.
columns : deprecated in the latest version. Use click and hover options instead.
hover - sets mouseover features. Available values are as follows: 'row','column', 'cross'. The default value is 'row'.
click - sets highlight options on mouse click. Available values are : 'row','column', 'cross'. When a 'row' value is set, a row is highlighted on click; when a 'column' value is set, a column is highlighted on click. The 'cross' value allows to highlight both rows and columns at the same time. The 'row' value is the default.
oneClick : true/false - Allows only one row or column to be highlighted at a time. A click on a new row/column cancels the current highlight. The default is false.
banColumns :[]- Prevents certain columns from being highlighted when clicked, and also on mouseover. Supply an array of column indices as an argument. A value of 'last' can be used to ban the last column.
banRows::[]- Prevents rows from beign highlighted. Supply an array of column indices as an argument.
banDataClick :true/false - If true, you can only click on table header rows. The default is false.
ignoreHeaders :true/false - If true, header rows are not colorized. (no striping). The default is true.
nested :true/false . The default is false. Use it for more consistent striping if a table contains nested tables.

Examples:
The order of parameters is not important.
$('#yourTableId').colorize({bgColor:'#EAF6CC', hoverColor:'green', hiliteColor:'red'});
$('#yourTableId').colorize({ oneClick:true});
$('#yourTableId').colorize({hover='column', click='column', banColumns :[0,1]});
$('#yourTableId').colorize({hover='cross', click='column'});
$('#yourTableId').colorize({ hiliteClass:'myStyle'});

No highlighting:
$('#yourTableId').colorize( { hiliteColor:'none' } );

Demo 1 - Usage with default options: $('#tblId').colorize();

Demo 2 - Highlights columns, one column at at time. Usage : $('#tblId').colorize( {columns: true, oneClick:true} );

Download Colorize 2.0.0 plugin

Download version 1.6.0

Download version 1.5.1

Download version 1.4.0

Download version 1.3.1

Download version 1.2.1

Download version 1.1

Version History

Version 2.0.0introduces 3 new options. The first new option is hover - to set mouseover features. Available values are as follows: 'row','column', 'cross'. The second option is click - to set highlight options on mouse click. Available values are the same as for the hover option: 'row','column', 'cross'. The values are self-explanatory: when a 'row' value is set, a row is highlighted on mouseover or click; when a 'column' value is set, a column is highlighted on mouseover or click. The 'cross' value allows to highlight both rows and columns at the same time. The 'row' value is the default.
The third new option is banRows. It allows you to set an array of row indices that will not be highlighted on mouseover or click when a 'column' value is selected. Usage: banRows:[1,2].
The columns option is deprecated. Use the click and hover options instead.

Version 1.6.0 introduces 3 new options, and a new feature. The new feature lets you highlight rows on mouseover and colorize columns on click. Use columns:'rowHover' option to enable this feature. New options: banDataClick - disables clicking cells, allowing only header row click; ignoreHeaders for striping; nested if you have nested tables.
Also, for better jQuery compatibility, the jQuery bind event is used to bind the onclick event handler to table cells.

Version 1.5.1 fixed a bug that caused a failure to highlight a row/column when clicking more than 2 times on the same row or column. (thanks to code from an anonymous user)

Version 1.5.0 introduced a new option, hoverClass, to indicate a style class to be used for hovering (thanks to a an idea from snobo). (Please be aware that the use of this option may degrade IE performance).
The altColor supports the value of 'none' now.
The columns option has a new value: 'hover' - changes columns background color or style on mouseover, but assigns a new style or background color to rows when they are clicked.
Includes support for multi-level headers by highlighting only the last header row. (However, the 'last' option does not work with multi-level headers)
In the banColumns option, a value of 'last' can be used to ban the last column.

Version 1.4.0 introduced a new option, hiliteClass, to indicate a style class to be used for highlighting (thanks to a tip from Skye Giordano). Also, the banColumns option now suppresses not only highlighting but also hover.

Version 1.3.0 allows to use the banColumns option even if the columns parameter is false (thanks to the code contributed by Aymeric Augustin)

Version 1.2.1 introduced a new feature : disable highlighting (thanks to a user comment):
Use 'none' value for hiliteColor option if you want to disable highlighting

Version 1.2.0 introduced new options :
columns : true/false
oneClick : true/false
banColumns :[]

Version 1.1.0 fixed a bug in the original version that highlighted all the tables on a page.

Comments

Highlight all rows

Great plugin! Is it possible to highlight all rows with a single click on a button (buttons like 'select all' and 'unselect all')?

Thanks. Jquery makes it very

Thanks. Jquery makes it very easy:

$('#myTable tr').css({ "background":"yellow});

i have a problem with

i have a problem with colorize:

i added a class for every row in html. this class is call odd or even. then i have two css-styles
#table_result tr.even td {
background-color: #FFFFFF;
}
and the same for even. this is for users who dont have JavaScript enabled to get alternating rows.

but with this style, colorize won't do anything. is this a known problem?

I am not sure I understand.

I am not sure I understand. Can you send me the page with the table and the stylesheet?

the page looks like this:

the page looks like this:

...

Col1 Col2 Col3
... ... ...
... .... ....

and the style:
tr.even td {
background-color: #FFFFFF;
}

tr.odd td {
background-color: #FFFFC8;
}

The plugin treats rows as

The plugin treats rows as single elements, and sets their styles. In your case, since you set the style for individual td elements, it overrides the plugin style-setting code. Try setting the style for rows instead.

get both row and column highlighted

What is wrong with
$('.table').colorize().colorize({columns:true});

When page is reloaded, browser dies.

How can I get both row and column highlighted?

I don't think there is

I don't think there is anything wrong with the above statement. The browser problem must be caused by something else.
Now, for your other question, the plugin does not currently hightlight both columns and rows at the same time.
However, you can highlight a row on mouseover, and colorize a column on click, or the other way round. You need to use the following options for it: columns: 'hover' or columns: 'rowHover'

ignore thead

I would like to suggest an option to ignore tr elements in the thead. I would like to apply the striping (colors) to everything except the column headers.

Thanks for the plugin.

Joel

Manually triggering the color change from code

I tried to manually trigger the color change through the jQuery trigger() method, but it didn't work. I found the issue was at the very end of the colorize module, I replaced

this.onclick = function() {
if(checkBan.call(this)) return;
handler.clickFunc.call(this, cells);
}

with

$(this).bind("click", function() {
if(checkBan.call(this)) return;
handler.clickFunc.call(this, cells);
});

By using bind() rather than attaching directly to the onclick event, I could then use trigger() on the td object and make the cell colorize.

Thanks for the tip, Anthony.

Thanks for the tip, Anthony. I wonder if this modification affects the performance.

Another small suggestion. I

Another small suggestion. I had issues using colorize when I had nested tables. Specifically with the alternating rows portion, the find functions would count nested rows in addition to the parent table.

I used nth-child to limit this to only the parent so that alternate rows are counted out correctly.

if (options.altColor!='none') {
jQuery(this).find('tr:nth-child(odd)').css('background', options.bgColor);
jQuery(this).find('tr:nth-child(even)').css('background', options.altColor);
}

Thanks for the suggestion. I

Thanks for the suggestion. I need an example of such a table with nested tables.

multi-level table headers

Thank you for this great plug-in. How can I make it to work with more than one level tr within thead. It works great if I only have one tr under thead.
Now when I click on "Total", "second level header middle" is also highlighted. I guess it's trying to match the 2nd th of the 3rd level tr to the 2nd level tr. It doesn't look right. Thanks in advance for any help.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

first level header

second level header left //colspan="4"
second level header middle //colspan="9"
second level header right //colspan="6"

//here is the last level table header I want to click on and highlight the column

Statistics by
Total
#PV
%Repl.HH
Type A
Type B
Type C
URE Int.
Par. Int.
Vac Rate
Resp. Rate
Salary of ref. per
HH Size
<14
14-19
20-29
30-39
40-49
50+

....

I am preparing a new version

I am preparing a new version of the plugin for release that will address this problem. Check back in a day or two.

Hi, i love this plugin...

Hi, i love this plugin... However when i click on a hyperlink, the whole row got colorized :p

I seriously wanted the hiliteColor function but i doesn't want it to activate when i click on a hyperlink. How can i prevent that from happening? Cheers!

You can use the banColumns

You can use the banColumns option to prevent it from happening. For example, if the hyperlink is in the first column, use it as follows: banColumns[0]. If it is in the second column, use banColumns[1], etc. Hope this helps.
Cheers!

Disable alternate rows

I have another small suggestion, to allow skipping of the row alternation. Simply to add a check for "none" on the line #143:


if (options.altColor!='none') {
jQuery(this).find('tr:odd').css('background', options.bgColor);
jQuery(this).find('tr:even').css('background', options.altColor);
}

Also, in fact, would be good to change the alternation method from applying the css color attribute to applying the class, similar to how hiliteClass has been introduced. Using classes instead of css color attributes are essential, because only then we can arrange the prioritizing of overlapped backgrounds via CSS cascading. I mean, if I have my own class applied to a particular column, that changes its background, I want to arrange that alternate background is different from a "normal" alternate. With styles, it's possible (.altClass .myOwnClass { ... }), with CSS color attribute - no.

-=snobo=-

Disable alternate rows

This is a worthy idea. I will implement it in the next version of the plugin.

Great little plugin

Great little plugin Eric..thanks so much for sharing.

I may be missing something, but is there a way to ban some colums from having a hover state?

i.e i have a 5 column table..and only 3 of the columns should have a hover state.

Thanks in advance for your help :)

ban column syntax

$("#editProfile").colorize({hiliteColor:'none', oneClick:'true', banColumn:["#last"] }); nor
$("#editProfile").colorize({hiliteColor:'none', oneClick:'true', banColumn:["last"] });

is not working to stop hi-lighting the column #last

please help!

Ban Last Column

The above syntax won't work. You will have to use a rather ugly workaround:

$("#editProfile").colorize(
{hiliteColor:'none',
oneClick:'true',
banColumn : [ $(this).find('td:last')[0].cellIndex ] }
);

Ban Hover State

The new version 1.4.0 supports banning columns from having a hover state.

Multiple Tables?

Any way to get the plugin to work with different colors in multiple tables on the same page?

Any help would be greatly appreciated.

Multiple Table

Sure, no problem.
Each table must have a unique id. For each table, you must include a call to the colorize function. Example:

$('#tbl1').colorize({bgColor:'#EAF6CC', hoverColor:'green', hiliteColor:'red'});

$('#tbl2').colorize({bgColor:'#aaa', hoverColor:'#bbb', hiliteColor:'green'});

Doesn't work at all

Jquery 1.3.1 and latest colorizer ..

I am also using tablesorter .. each table has the tablesorter class --
I want to colorize all tables :

$("table").colorize();

gives nothing , but with columns: true , it works (doesn't let me click and select but atleast highlights onmouseover)

Thanks from the non-programmer

Hi, I appreciate all of you who create plugins and widgets because it makes life so much nicer for us non-programmers. I've hacked my way through a few hard coded sites, but now I've been shown WP I don't think I could go back.

Here's a code for animating a

Here's a code for animating a paragraph using slide animation, I found this interesting.

//Code for example B
$("input.buttonBslidedown").click(function(){ $("div.contentToChange").find("p.fourthparagraph:hidden").slideDown("slow"); });
$("input.buttonBslideup").click(function(){ $("div.contentToChange").find("p.fourthparagraph:visible").slideUp("slow"); });
//show code example B
$("a.codeButtonB").click(function(){$("pre.codeB").toggle()});

how to colorize the table cells individualy ?

hi

i want to colorize only cells of a table on hover state...

how can i adapt this plugin ?

thx

I am afraid you can't. The

I am afraid you can't. The plugin is designed to colorize table rows or columns on click.
However, you don't need the plugin to colorize individual cells on hover state.
You can easily accomplish it using the following code:

var cells=$("#myTable").find('td');
$.each(cells, function(index, cell){
cell.onmouseover = function(){
this.style.backgroundColor = "yellow";
}
cell.onmouseout = function(){
this.style.backgroundColor = "white";
}
});

new feature

Thank you for colorize!

I'm using it in my project with small modification. When highlight background is to dark and text colour dark also - it's not visible. So it's my code modification with new option "altTextColor":

jQuery.fn.colorize = function(params) {
options = {
altColor: '#ECF6FC',
bgColor: '#fff',
hoverColor: '#BCD4EC',
hiliteColor: 'yellow',
oneClick: false,
columns: false,
altTextColor: 'white',
banColumns: []
};
jQuery.extend(options, params);

var colorHandler = {
checkHover: function() {
if (!this.onfire) {
this.origColor = this.style.backgroundColor;
this.style.backgroundColor= options.hoverColor;
this.origTextColor = this.style.color;
this.style.color= options.altTextColor;
}
},
checkHoverOut: function() {
if (!this.onfire) {
this.style.backgroundColor=this.origColor;
this.style.color= this.origTextColor;
}
},

Bug Report

When I use the oneClick = true in Version 1.3.0
It has problem about toggle hiliteColor on the same TR row

Bug Report

This is intentional behavior. You have to click on another row before you can toggle color on the previous row again.

debug function call leftover

Thank you & kudos for the great plugin!

Just download the 1.3.0, and wondered why I have a Javascript error when using it... Turns out there's a function call on line 59:

msg.debug(timeDiff.getDiff());

I guess that's a kinda leftover in the code... Apart from that, the plugin works excellent, thanx again!

-=snobo=-

Thanks

That's exactly what it was. I removed the offending line, and upped the version number to 1.3.1. Thank you for bringing it to my attention.

Looks good except:

Like all other jQuery alt row colorizers I've looked at so far, it can't handle rowspan properly.

Also, breaks in Firefox if setting hiliteColor:'none' or hoverColor:'none'. I'm only guessing though that this is how we would turn these features off altogether. This works in IE6 but an error is raised.

I would have assumed that the default options would be that these features are turned off, and they should only be enabled if included in the parameter list.

Maybe I'm not understanding something?

Using 'none' to disable highlighting

If you would like to use a hiliteColor value of 'none', make the following change to the script file:

1) Find the following code: "hightlight : function()".

You will see the code for this function, which will be something like this:

this.style.backgroundColor= options.hiliteColor;
this.onfire = true;

2) Change it so that it is as follows:

if(options.hiliteColor != 'none') {
this.style.backgroundColor= options.hiliteColor;
this.onfire = true;
}

That's it! Now, when you initialize the table, just set the hiliteColor value to 'none', like this:
$("#mytable").colorize( {hiliteColor: 'none'} );

Great plugin. Hope this helps.

Thanks for the tip. I've

Thanks for the tip. I've included it into the updated version of the plugin.

Default options

Sorry, the documentation was not clear on the subject of default options. I am going to update it.
The default options for parameters that accept color codes are as follows:
altColor : '#ECF6FC',
bgColor: '#FFFFFF',
hoverColor : '#BCD4EC',
hiliteColor : 'yellow'.
Also, 'none' is not a valid value. Only HTML color codes or named colors are.
There is some logic to your assumption that by default the color parameters should be turned off. However, I chose in favor of the easiest plugin initialization, and that meant making some assumptions about the default colors.
Coming back to your question about turning off hiliteColor or hoverColor parameters, you should set them equal to your table background color to make them invisible.
Hope this clears up your questions.

Default options

Yes, thank you, I figured that much out.

My much more serious issue is applying colorizer to a table with rowspans:

... ...
... ...
...
... ...

The result is a row that appears to have color applied to only part of it. Haven't seen any other similar plug-ins that resolve this problem either though.

Wonderful Plugin

I installed the plugin and had it up and running within two minutes! It's seriously great! The only question/feature request I had for the plugin was the ability to only highlight one row at a time onclick. In other words, after clicking a row, when you go to click a new row, the first row that you clicked will default back to its original color and the new row will obtain the hiliteColor.

New Feature

I will try to include this feature in the new release that I am preparing right now.

colorize leak

The plugin is great, I just cant get it to work in the manner I need, ... it does not seem to stop.
I have jQuery.js latest version, and I am currently using jquery.colorize.js for one of my tables on my page. To explain I am calling an ajax.load to a div.id ("#showPanel"). Before the ajax.load, I have a php mysql query creating my table dynamically from a db. So thru jquery I load my URL into div.id("#showpanel"). And this works like a charm. jQuery finest features. But my problem is that when I colorize this dynamic table, ... it leaks into the other html markup, .. and colorize-es my entire page and not just div.id("#showpanel"), .. how can I isolate covorize or tell colorize to stop/remove/
My markup below:
//here is my function that calls the URL where my query makes the dynamic table,...
function setAjaxQuery(urlVar) {
$("#showPanel").load("http://myURL/ajaxfunc.php"+urlVar).show('fast');
};
//here a php switch to complete my url and case for the query selection,...
switch ($_GET['ajaxfunc']){
case transDBQuery :
print transDBQuery($_GET['tablerow']);
break;
//After the switch this function queries my db and creates the table with the results,...
function transDBQuery($transponder){
?>

$('table').colorize(({bgColor:'##BCCAE5', hoverColor:'white', hiliteColor:'#BADEF6'})) ;

<?php

$DBQuery="SELECT * FROM outagedb WHERE network LIKE '%$transponder%' ORDER by id DESC";
$result=mysql_query($DBQuery);
$output = "";
$output .= "StartDateStartTimeEventDevice AffectedNetworkStopDateStopTimeTest";
while ($row=mysql_fetch_array($result))
{
$output .= "$row[startDate]$row[startTime]$row[event]$row[device_aff]$row[network]$row[stopDate]$row[stopTime]$row[outID]";
}
$output .= "";
return $output;
}

OpenTable();
echo "Query Results:";
CloseTable();

Colorize Bug

Unfortunately, a bug crept into the original version that would colorize all the tables on a page, where you wanted it or not. I have fixed the bug, and released a new version of the plugin that has a version number of 1.1.0. Please download it from this page.
Also, you need to call it as follows:

$('#yourTableId').colorize();

Otherwise, if you supply a generic 'table' tag to jQuery, the plugin will be called on all the tables on a page.

the leak has been filled !!

The new version has resolved my fault, where colorize leaked onto my entire page. Colorize is working like a charm, ... GREAT plugin, I have upgraded to 1.1.0 on my test pages and will soon incorporate into my live pages. Thanks, your reply was fast and right on the money.
jQuery Rock !!!

one of a kind must have tool

This is just one of those one of a kind tools that makes the web even more look like a fullgrown application
I certainly gonna use it

Vinny

Awesome

This is a great plugin - helpful in making tables readable. Are there any plans to expand the highlighter (say vertical instead of horizontal, and not highlight certain rows?)

Doesn't work if the table has a class though

I can't get jQuery Colorize plugin to work when my table has a CSS class attribute, as well as an ID. I'm using jQuery tablesorter, so I have to give my table a class attribute of .tablesorter in order for the various CSS values that come with tablesorter to work correctly: e.g . If I remove the class .tablesorter, colorize works, but I need this class in the markup for jQuery tablesorter. Is there any way around this?

tablesorter

There is a way around it. But if you use it, Colorize will lose most of its functionality, keeping only the striping.

Find the following piece of code in jquery.colorize.js:

return this.each(function(){
jQuery(this).find('tr:odd').css('background', options.bgColor);
jQuery(this).find('tr:even').css('background', options.altColor);

Now, change the last 2 lines to the following:

jQuery(this).find('tr:odd td').css('background', options.bgColor);
jQuery(this).find('tr:even td').css('background', options.altColor);

This will allow Colorize to overide the style settings for individual table cells set in the stylesheet.

If you want to use the sorting functionality from the tablesorter plugin, you could use something similar to the following code on (document).ready:

$(".tablesorter").tablesorter();
$('.tablesorter').colorize();
$($.tablesorter.defaults.headerList).each(function (index){
$(this).click (function (){
setTimeout(function() {
$('.tablesorter').colorize();
},10);
});
});