views:

191

answers:

3

I have a problem setting the CSS class for each cell in a given table row. Initially I thought setting the parent row CSS would affect the style properties of cells, but that doesn't work. Instead I have to loop through all the cells in a given row to updated the CSS class.

However this is not efficient. And it took a lot of time. Consider my situation: I have around 230 rows in which each row has 23 cells (totally 5290 cells).

Note: I don't use any frameworks. so please can you suggest an approach in native JS?

UPDATE :

its working fine using the Paolo's recommendation..

Initially my custom css class is been like this

.Grid_RowItalicsBold { PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }

And i changed this to

tr.Grid_RowItalicsBold td{ PADDING-RIGHT: 5px; PADDING-LEFT: 8px; FONT-WEIGHT: bold; FONT-SIZE: 8pt; OVERFLOW: hidden; COLOR: Black; LINE-HEIGHT: 15pt; FONT-STYLE: normal; FONT-FAMILY: Verdana, Arial, Sans-Serif; WHITE-SPACE: nowrap; BACKGROUND-COLOR: yellow; TEXT-DECORATION: none }

And i assigned this class to my specific rows using javascript. :)

+14  A: 

Why can't you set the class of the row and adjust your css accordingly?

<tr class="myclass">
   <td>...</td>
   <td>...</td>
</tr>

Then in CSS:

tr.myclass td {
    ...
}

In either case, assuming the table has an id of "mytable" you could give all the table rows the class you want like so:

var rows = document.getElementById('mytable').getElementsByTagName('tr');
for(var x = 0; x < rows.length; x++) {
    rows[x].className = rows[x].className + " myclass";
}

If you're doing this to the whole table, though, you might as well just give a class to the table tag itself then do:

table.myclass tr td {
    ...
}
Paolo Bergantino
I was just about to submit a very similar answer. Here's an upvote for you instead. :-)
No Surprises
@Paolo, i forgot to mention that.. the ids are auto generated.. i dont have the control over that.. (Its actually a intrnal Grid component)
Ramesh Vel
sorry for the misunderstanding, your approach is working fine... I have updated my question... :)
Ramesh Vel
+2  A: 

@kris, the classes used in cells are common for entire cells.. i need to update only the selective rows.. so i cant just update that...

It still seems like you should be using CSS selectors to do what you want. Maybe something like this:


  #mytable td {
    // regular td style
  }
  #mytable .special td {
    // special cell style
  }

<table id="mytable">
  <tbody>
    <tr>
      <td>Data</td>
      <td>Data</td>
    </tr>
    <tr class="special">
      <td>Data</td>
      <td>Data</td>
    </tr>
  </tbody>
</table>
No Surprises
A: 

Initially I thought setting the parent row CSS would affect the style properties of cells, but that doesn't work.

I think that if you set some of your cell's properties to "inherit" they will inherit those properties from the table row. Have you tried that?

Soska