1

Right now I am playing with an idea where I want to change the color (maybe eventually remove/hide) a column or row of data right now my aim is the columns. So far I have it partly working but not entirely

with this:

$('#'+arry[0]+' tr td:eq('+arry[1]+')').css({"background-color":"FF0"});

I am able to effect the column but only one row, not all rows within that column, and it seems to be effecting the rows/columns within the "tbody" but not the "thead" or "tfoot" I need to effect all 3.

an example of the table I am working with looks like:

<table id="testerTable">
    <thead>
        <th>First</th>
        <th>Second</th>
        <th>Third</th>
        <th>Fourth</th>
        <th>Fifth</th>
    </thead>
    <tbody>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

I'm thinking my approach is to simple, so I need some ideas. This will ultimately be used with datatables from datatables.net but first I need to figure out this part.

****EDIT****

The arry variable is currently take from the rel attribute of something else arry[0] is the tables id.. and arry[1] is the position of the column in this case 0-4

3
  • how looks the arry ? What is the 'position of the column'? Commented Apr 25, 2012 at 21:15
  • edited to show. But in the attribute mentioned it looks like testerTable::2 for example Commented Apr 25, 2012 at 21:16
  • Ok, I think I get it. look below. Commented Apr 25, 2012 at 21:32

3 Answers 3

1

demo jsBin

var string= 'testerTable::2'; // your data // test with: 0,1,2,3,4
var arry= string.split('::'); // split to separate values

var $tr = $('#'+arry[0]+' tr');


$('#'+arry[0]+' th').eq(arry[1]).css({"background-color":"FF0"});

$tr.each(function(el){ // iterate through all 'tr' to find relative 'td' .eq()
  $(this).find('td').eq(arry[1]).css({"background-color":"FF0"});
});
Sign up to request clarification or add additional context in comments.

Comments

1

I would use <colgroup> to color the columns as it would be quick and easy to manipulate.

http://www.w3schools.com/tags/tag_col.asp

An example, you could put:

<colgroup>
  <col style="background-color:FF0" />
  <col span="4" />
</colgroup>

and edit the entire colgroup with jquery or other means.

3 Comments

unfortunately colspan isn't supported by datatables, and can't be used currently. I wish it was as easy as colspan for me :-)
@chris hmm, I just read a comment on their website that said the same thing (oh, about colgroup, not colspan), but they say nothing bad will happen if you use it. Maybe they should think about it.
most of the tables I work with are dynamically generated through datatables, so adding in the col (group/span) is a bit more difficult all around (maybe not so much on newer tables we generate, but existing ones through out the sys), overall trying to come up with a solution to cover all bases.
0

You could just give each element in column 1 the class 'c1' and then do $('.c1').css... This would save mucking about with th/tr/td distinctions. You'd do this for every column, of course.

And BTW, it's spelt 'array' :)

3 Comments

i know its spelled array but the variable name I gave is arry when I defined it through the spilt I did on the original attribute :) Ill have to see if datatables supports the notion of classing every column like that
Glad to know about the spelling :) Giving classes to table elements works fine, too, so you can do it this way, should you wish to.
You can't do it in one hit per column, though. You'd have to add the class to each td element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.