You are here

function theme_tableselect in Elements 5

Same name and namespace in other branches
  1. 6 elements.theme.inc \theme_tableselect()

Format a table with radio buttons or checkboxes.

@code $options = array(); $options[0]['title'] = "A red row" $options[0]['#attributes'] = array ('class' => array('red-row')); $options[1]['title'] = "A blue row" $options[1]['#attributes'] = array ('class' => array('blue-row'));

$form['myselector'] = array ( '#type' => 'tableselect', '#title' => 'My Selector' '#options' => $options, );

Parameters

$variables: An associative array containing:

  • element: An associative array containing the properties and children of the tableselect element. Each option in $variables['element']['#options'] can contain an array keyed by '#attributes' which is added to the row's HTML attributes. @see theme_table Properties used: header, options, empty, js_select.

Return value

A themed HTML string representing the table.

Example:

File

./elements.module, line 137

Code

function theme_tableselect($element) {
  $rows = array();
  $header = $element['#header'];
  if (!empty($element['#options'])) {

    // Generate a table row for each selectable item in #options.
    foreach ($element['#options'] as $key => $value) {
      $row = array();
      $row['data'] = array();
      if (isset($value['#attributes'])) {
        $row += $value['#attributes'];
      }

      // Render the checkbox / radio element.
      unset($element[$key]['#printed']);

      // NEW
      $row['data'][] = drupal_render($element[$key]);

      // As theme_table only maps header and row columns by order, create the
      // correct order by iterating over the header fields.
      foreach ($element['#header'] as $fieldname => $title) {
        $row['data'][] = $element['#options'][$key][$fieldname];
      }
      $rows[] = $row;
    }

    // Add an empty header or a "Select all" checkbox to provide room for the
    // checkboxes/radios in the first table column.
    $first_col = $element['#js_select'] ? array(
      theme('table_select_header_cell'),
    ) : array(
      '',
    );
    $header = array_merge($first_col, $element['#header']);
  }
  elseif ($element['#empty']) {

    // If there are no selectable options, display the empty text over the
    // entire width of the table.
    $rows[] = array(
      array(
        'data' => $element['#empty'],
        'colspan' => count($header),
      ),
    );
  }
  return theme('table', $header, $rows);
}