欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

theme_table 增加class 到列

shiping1 的头像

I have a table in a form such as the 'user_admin_account' form. In one of the TD's I want to add a class, how do I do this the most convenient way?

This is what I currently have, it doesn't seem to work

$row = array(
  'title' => array(
    'data' => array(
      '#title' => $name . ' ' . $surname,
      '#attributes' => array(
        'class' => array(
          'myClass',
        ),
      ),        
    ),
  ),
  'date' => date('Y-m-d', $account->created),
  'name' => $name . ' ' . $surname,
);
shareimprove this question
 add comment

这是正确答案  The variables for theme_table() are structured differently to a standard render array, so you need to format your array slightly differently:

$row = array(
  'title' => array(
    'data' => $name . ' ' . $surname,
    'class' => array('myClass')
  ),
  'date' => date('Y-m-d', $account->created),
  'name' => $name . ' ' . $surname,
);

The above produces:

<table>
  <tbody>
    <tr class="odd">
      <td class="myClass"> </td>
      <td>1970-01-01</td>
      <td> </td> 
    </tr>
  </tbody>
</table>

The first and last column are only empty as my test environment doesn't have the same variables defined as in your code.

shareimprove this answer
 add comment

The documentation for theme_table() describes what the function is expecting in $variables['rows'], and it says:

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.
  • "no_striping": a boolean indicating that the row should receive no 'even / odd' styling. Defaults to FALSE.

The example present in that page is the following one:

$rows = array(
  // Simple row
    array('Cell 1', 'Cell 2', 'Cell 3'),
    // Row with attributes on the row and some of its cells.
    array(
      'data' => array('Cell 1', array('data' => 'Cell 2', 'colspan' => 2)), 'class' => array('funky'))
    );

In that example, "funky" is the CSS class applied to the second row, which contains two cells; the second cell takes the room of two cells (because the 'colspan' => 2 part).



来自 http://drupal.stackexchange.com/questions/39195/add-class-to-column-in-a-table

普通分类: