3

If I want to divide a row into some sub-rows in Drupal 8, what should the code be?

enter image description here

The following code worked fine for simple tables. I need to do something with the $rows array to divide the rows in sub-rows. How can I achieve this?

$form['mytable'] = array(
  '#theme'  => 'table',
  '#header' => $header,
  '#rows'   => $rows,
  '#empty'  => t('Not Found.'),
);

2 Answers 2

3

Drupal 8:

$table = [
  '#type' => 'table',
  '#header' => ['ab', 'cd', 'ef'],
  [
    [
      '#plain_text' => 'rowspan',
      '#wrapper_attributes' => ['rowspan' => 2],
    ],
    ['#markup' => '1'],
    ['#markup' => '1'],
  ],
  [['#markup' => '1'], ['#markup' => '1']],
];

Result:

Table with rowspan

See Drupal Documentation for a colspan example

1

This is still similar to how it was done in Drupal 7 and Drupal 6 with the use of colspan or rowspan attributes. The documentation for template_preprocess_table states

Each cell can be either a string or an associative array with the following keys:

  • data: The string or render array 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 the top row of your example image would be like such:

$rows = [
  [
    ['data' => '', 'attributes' => ['rowspan' => 3]],
    '',
    '',
    '',
  ],
  ['', '', ''],
  ['', '', ''],
];

in order to get the following HTML:

<tr>
  <td rowspan="3"></td>
  <td></td>
  <td></td>
  <tD></td>
</tr>
<tr>
  <td></td>
  <td></td>
  <td></td>
</tr>
<tr>
  <td></td>
  <td></td>
  <td></td>
</tr>

Your Answer

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

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