0

How can I apply a class to the <tr> of some table headers using the table theme function?

So, I get this: <tr class='my-class'><th>foo</th><th>bar</th></tr>

...rather than: <tr><th class='my-class'>foo</th><th class='my-class'>bar</th></tr>

...which is all I've managed with this:

$section = array(
  '#theme' => 'table',
  '#header' => array(
    array('data' => 'foo', 'class' => 'my-class',),
    array('data' => 'bar', 'class' => 'my-class',),
  ),
  '#rows' => $rows,
),

Thanks

1
  • Are you using Drupal 6 or 7?
    – rooby
    Commented Apr 21, 2013 at 5:05

2 Answers 2

2

You can do it using below code

 $section = array(
      //It contains the rows
      array( 
        //'data' contains cell data as numerical arrays:
        'data' => array(
          //Cell 1: array if need to add classes in cells too
          array('data' => 'Cell 1', 'class' => 'cell-class1'),
          //Cell 2:
          array('data' => 'Cell 2', 'class' => 'cell-class2'),

        ),
        //Row attributes, e.g. 'class', 'rel','data'
        'class' => array('row-class1'),
      ),
      //Contains the rows, is a numerical array (as default): 0, 1, 2 ... :
      array( 

        'data' => array(
                array('data' => 'Cell#1', 'class' => 'cell-class1'),
                array('data' => 'Cell#2', 'class' => 'cell-class1'),
        ),
        'class' => array('row-class2'),
      ),
    );

Hope it will help!

1
1

If your current example works I'm assuming you are on drupal 6.

You can add attributes to the header array like this:

$section = array(
  '#theme' => 'table',
  '#header' => array(
    array('data' => 'foo', 'class' => 'cell-class'),
    array('data' => 'bar', 'class' => 'cell-class'),
    'class' => 'row-class',
  ),
  '#rows' => $rows,
),

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.