Does somebody already have an easy way expanding Drupal's theme_table function to give each table an id or class so I can do some theming with css?
Adding classes like "col1" and "col2" would also be pretty usefull ...

Comments

occupant’s picture

Not sure about the theme_table function, but if you're using views to build your tables you can add css classes easily enough.

Rakward’s picture

Not talking about themes, just drupal generated tables with the functions in theme.inc.

Nikki Aiuto’s picture

I know this thread is a year old, but since I went looking for the same answer and didn't find it here, I went into the source in includes/theme.inc. The answer is in the comments for the theme_table function (which I imagine you found by now, but I put here for others to find):

* @param $rows
* An array of table rows. Every row is an array of cells, or an associative
* array with the following keys:
* - "data": an array of cells
* - Any HTML attributes, such as "class", to apply to the table row.
*
* Each cell can be either a string or an associative array with the following keys:
* - "data": The string to display in the table cell.
* - "header": Indicates this cell is a header.
* - Any HTML attributes, such as "colspan", to apply to the table cell.

So, this should give you the col1, col2, etc attributes you are looking for (plus I also gave each row a distinct class):

 $attrs = array('class' => 'my-table-class');  // optional table level attributes        
  $headers = array('col one', 'col two', 'col three');
  $rows = array();
  $data = array
    (array(1,2,3),
     array(4,5,6),
     array(7,8,9));

  $i = 1;
  foreach ($data as $cols) {
    $rows[] = array('data' => array
                    (array('data' => $cols[0], 'class'=> 'col1'),
                     array('data' => $cols[1], 'class'=> 'col2'),
                     array('data' => $cols[2], 'class'=> 'col3')),
                    'class' => "row$i");
    $i++;
  }
  $tbl = theme('table', $headers, $rows, $attrs);

The code above produces this HTML:

<table class="my-table-class sticky-enabled">
 <thead><tr><th>col one</th><th>col two</th><th>col three</th> </tr></thead>
<tbody>
 <tr class="row1 odd"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td> </tr>
 <tr class="row2 even"><td class="col1">4</td><td class="col2">5</td><td class="col3">6</td> </tr>
 <tr class="row3 odd"><td class="col1">7</td><td class="col2">8</td><td class="col3">9</td> </tr>
</tbody>
</table>