I wrote a patch to enable adding attributes, such as "colspan", "rowspan" or "class" to the <td> element, via the use of a new form attribute #td_attributes

Example usage:

$form['my_table'] = array(
  '#type' => 'tableform',
  '#header' => array(
    t('ID'),
    t('Title'),
  ),
  '#options' => array(
    array(
      '1',
      t('One'),
    ),
    array(
      '2',
      t('Two'),
    ),
    array(
      array(
        '#markup' => l(t('Add new'), 'node/add'), // <-----
        '#td_attributes' => array(                // <-----
          'colspan' => 2,                         // <-----
        ),                                        // <-----
      ),
    ),
  ),
);

This results in the following markup:

<table class="sticky-enabled">
  <thead>
    <tr>
      <th>ID</th>
      <th>Title</th>
    </tr>
  </thead>
  <tbody>
    <tr class="odd">
      <td>1</td>
      <td>One</td>
    </tr>
    <tr class="even">
      <td>2</td>
      <td>Two</td>
    </tr>
    <tr class="odd">
      <td colspan="2"> <!-- SEE HERE -->
        <a href="/en/node/add">Add new</a>
      </td>
    </tr>
  </tbody>
</table>

Comments

khuongkd’s picture

StatusFileSize
new1.51 KB

I wrote for class header but element form '#tree' not work
after i change to #prefix -> worked.

$header = array(   
    'stt' => array(      
      '#prefix' => t('STT'),
      '#head_attributes' => array(
        'class' => 'testing',        
      ),
    ),
    t('Tên bé'),
    t('Điểm danh'),
    t('Giờ ăn'),
    t('Giờ ngủ'),
    t('Ý kiến khác của cô giáo'),
  );
neovinhtru’s picture

oh nice!
thank you so much.

linhdevil10107’s picture

good job!
Vote for khuongkd!

nikita_tt’s picture

Issue summary: View changes
StatusFileSize
new529 bytes

Here is my version of patch. You just need to add the '#tf_attributes' (array) element to the array of row columns.
Example:

  $row[] = array(
    'column #1 content',
    'column #2 content',
    'column #3 content',
    '#tf_attributes' => array(
      'class' => array('example-class'),
    ),
  );