views:

1090

answers:

3

Hey all,

I see this post on highlighting even columns but can I highlight only selected columns?

Here is the code they use:

$("table.Table22 > tbody > tr > td:nth-child(even)").css("background","blue");

But I would like: NOTE: the class="highlight" will be on the selected columns, so if I selected column 3 the class="highlight" would be removed from column 2 and added to column 3. jQuery needs to add the class based on selected column.

<table class="tbl">
    <tr>
        <th class="firstColumn">
            Cell 1:Heading
        </th>
        <th class="highlight">
            Selected column so this should be highlighted
        </th>
        <th>
            Cell 3:Heading
        </th>
        <th>
            Cell 4:Heading
        </th>
        <th>
            Cell 5:Heading
        </th>
    </tr>
    <tr>
        <td>
            Cell 1:Row 1
        </td>
        <td class="highlight">
            Selected column so this should be highlighted
        </td>
        <td>
            Cell 3:Row 1
        </td>
        <td>
            Cell 4:Row 1
        </td>
        <td>
            Cell 5:Row 1
        </td>
    </tr>
    <tr>
        <td>
            Cell 1:Row 2
        </td>
        <td class="highlight">
            Selected column so this should be highlighted
        </td>
        <td>
            Cell 3:Row 2
        </td>
        <td>
            Cell 4:Row 2
        </td>
        <td>
            Cell 5:Row 2
        </td>
    </tr>
</table>
+4  A: 

You might want to take a look at jQuery tableHover plugin to achieve this. Then use something like this

$('table.tbl').tableHover({
                           colClass: 'hover', 
                           clickClass: 'click', 
                           headCols: true, 
                           footCols: true 
                          });

EDIT:

Something like this?

Working Demo - Click on any cell, to highlight the column

Code from demo -

$(function() {

  $('table.tbl tr').children().click(function() {

    $('table.tbl tr').children().removeClass('highlight'); 
    var index = $(this).parent().children().index(this); 
    $('table.tbl tr').each(function() {
      $(':nth-child(' + (index + 1) + ')' ,this).addClass('highlight');
    });

  });

});
Russ Cam
I did like the tableHover plugin but actually I'm looking to change the CSS in the selected columns of a table, but I though for the example I would just use highlight. But the plugin will go into my workspace very soon, Thanks :)
Phill Pafford
Thanks this is what I needed. Any chance you could help out on my other question? I have added your code to it as well: http://stackoverflow.com/questions/1126489/jquery-examples-horizontal-accordion-table-instead-of-un-ordered-lists-upda
Phill Pafford
A: 

If you create a link in your table headers, you can do something like this:

$("table.tbl th a").click(function() {
   var colnum = $(this).closest("th").prevAll("th").length;

   $(this).closest("table").find("tr td").removeClass("highlight");
   $(this).closest("table").find("tr td:eq(" + colnum + ")").addClass("highlight");
}

That will set all cells below the clicked link to class "highlight".

Of course, you should still set the correct style in your CSS file:

table.tbl tr .highlight {  background-color: blue; }
Philippe Leybaert
+1  A: 

have you concidered using colgroups instead of adding classes to every cell?

i only recently started to see the power of colgroups myself, and they work like this:

 <table id="myTable">

           <colgroup class="highlight"></colgroup>
           <colgroup></colgroup>
           <colgroup></colgroup>
           <colgroup></colgroup>
           <colgroup></colgroup>

        <thead>
           <tr>
               <th></th>
               <th></th>
               <th></th>
               <th></th>
               <th></th>
           </tr>
        </thead>
        <tbody>
           <tr>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
               <td></td>
           </tr>
        <tbody>
 <table>

this would render a table with 5 columns, where 1 column has css class to highlight the first col. so actually the only thing you have to do then, add a function to the hover of each cell, to just add the highlighting class to the corresponding colgroup.

there is a complete videoguide you can find right here:table fix header, and row + column highlighting.

*EDITED the answer because it was irrelevant, i read the question wrong, and answered to a totally different matter. (added a correct reply now)

Sander
I did like the video and had thought about colgroups but couldn't get them to work in my earlier attempts. Will add that site to my bookmarks, thanks ;)
Phill Pafford
Any chance you could look at my other question? http://stackoverflow.com/questions/1126489/jquery-examples-horizontal-accordion-table-instead-of-un-ordered-lists-upda
Phill Pafford