7 form.inc theme_tableselect($variables)

Returns HTML for a table with radio buttons or checkboxes.

Parameters

$variables: An associative array containing:

  • element: An associative array containing the properties and children of the tableselect element. Properties used: #header, #options, #empty, and #js_select. The #options property is an array of selection options; each array element of #options is an array of properties. These properties can include #attributes, which is added to the table row's HTML attributes; see theme_table(). An example of per-row options:
    $options = array(
      array(
        'title' => 'How to Learn Drupal',
        'content_type' => 'Article',
        'status' => 'published',
        '#attributes' => array('class' => array('article-row')),
      ),
      array(
        'title' => 'Privacy Policy',
        'content_type' => 'Page',
        'status' => 'published',
        '#attributes' => array('class' => array('page-row')),
      ),
    );
    $header = array(
      'title' => t('Title'),
      'content_type' => t('Content type'),
      'status' => t('Status'),
    );
    $form['table'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('No content available.'),
    );
    

Related topics

14 string references to the theme hook from theme_tableselect()

Note: this list is generated by looking for the string for this theme hook, so it may include some references that are not actually using this theme hook.

comment_admin_overview in modules/comment/comment.admin.inc
Form builder for the comment overview administration form.
database_test_theme_tablesort in modules/simpletest/tests/database_test.module
Output a form without setting a header sort.
drupal_common_theme in includes/common.inc
Provides theme registration for themes across .inc files.
FormsElementsTableSelectFunctionalTest::testMultipleFalseOptionchecker in modules/simpletest/tests/form.test
Test the whether the option checker gives an error on invalid tableselect values for radios.
FormsElementsTableSelectFunctionalTest::testMultipleFalseSubmit in modules/simpletest/tests/form.test
Test submission of values when #multiple is FALSE.

... See full list

File

includes/form.inc, line 3459
Functions for form and batch generation and processing.

Code

function theme_tableselect($variables) {
  $element = $variables ['element'];
  $rows = array();
  $header = $element ['#header'];
  if (!empty($element ['#options'])) {
    // Generate a table row for each selectable item in #options.
    foreach (element_children($element) as $key) {
      $row = array();

      $row ['data'] = array();
      if (isset($element ['#options'][$key]['#attributes'])) {
        $row += $element ['#options'][$key]['#attributes'];
      }
      // Render the checkbox / radio element.
      $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.
    if ($element ['#js_select']) {
      // Add a "Select all" checkbox.
      drupal_add_js('misc/tableselect.js');
      array_unshift($header, array('class' => array('select-all')));
    }
    else {
      // Add an empty header when radio buttons are displayed or a "Select all"
      // checkbox is not desired.
      array_unshift($header, '');
    }
  }
  return theme('table', array('header' => $header, 'rows' => $rows, 'empty' => $element ['#empty'], 'attributes' => $element ['#attributes']));
}

Comments

The tableselect template:

<?php
$header
= array();
$options = array();
$default_value = array();

$IsCheckbox = true;

$header = array(
 
'title' => t('Column title')
);
$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'));

if(
$IsCheckbox)
{
/* Checkbox only */
$default_value[0] = true;
$default_value[1] = false;
}
else
{
/* Radiobox only */
$default_value = '0';
}

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

Disable rows:

<?php
/* Using foreach: */
foreach( $options as $key => $value )
{
 
$form['myselector'][$key]['#disabled'] = true;
}
/* Individual: */
 
$form['myselector'][0]['#disabled'] = true;
 
$form['myselector'][1]['#disabled'] = true;
?>

* Disable block must go after $form['myselector'] = aray(...) initialization block;
** Please note, that it is checkbox/radio element is actually being disabled;
*** Drupal 7 disable rows tableselect.

I needed to disable the checkbox for users that have opted out of email. I used the code above and it turned off the correct checkboxes, but it always puts the deselected items above the other items so the list is out of order.

Here is my code.

$optout = array();
foreach ($result as $row){
if ($row->optout) $optout[$row->c_id] = TRUE;
$rows[$row->c_id] = array(
'name' =>  $row->firstname . ' ' .  $row->lastname,
'email' => $row->emailaddress,
'optout' => iif($row->optout,'Yes',''),
'mobilephone' =>  $row->mobilephone,
     );
}

  $form['table'] = array
  (
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#empty' => t('No users found'),
  );

  //disable checkboxes
    foreach( $optout as $key => $value )
{
  $form['table'][$key]['#disabled'] = true;
}

Easy way to solve this?

... tablesort issues? @see #97293 'Tablesort cleanup' !!!

I override theme tableselect and stuck this first thing:

$element = $variables['element'];

foreach ($element['#options'] as $key => $value) {
$o = $element[$key];

unset($element[$key]);

$element[$key] = $o;
}

then the rest...

To make it clearer, when you disable rows:

You can't do it while you assemble the rows. You have to iterate through your tableselect after setting it up and set ['table'][$key]['#disabled'] for each row you want to disable then.

Don't use ['#disable'] = FALSE, any setting will disable the row, even FALSE. Just don't specify it at all if the row should be selectable. The checkbox/radio button will be removed rather than disabled, actually.

In theme_table there is an variable that you can pass to disable sticky headers like so:

<?php
$form
['table'] = array(
   
'#type' => 'table',
   
'#rows' => $rows,
   
'#header' => $header,
   
'#sticky' => FALSE,
);
?>

Unfortunately there's no way of passing that sticky variable through theme_tableselect so the least hacky solution in my opinion is to add the class tableheader-processed to the table select (which will trick the sticky header javascript into thinking that the table already has a sticky header).

Resulting code would be:

<?php
$form
['table'] = array(
   
'#type' => 'tableselect',
   
'#header' => $header,
   
'#options' => $options,
   
'#default_value' => $value,
   
'#attributes' => array(
     
'class' => array(
       
'tableheader-processed'
     
),
    ),
  );
?>

Hopefully this helps someone in the future :)

Thanks for sharing your solution. I've written a patch to fix this in core.
See http://drupal.org/node/1788278

just implement this in your theme template.php:

/**
* Overrides theme_tableselect.
*
* Changes:
*   Add ability to disable sticky header.
*/
function MYTHEME_theme_tableselect($variables) {
  $element = $variables['element'];
  $rows = array();
  $header = $element['#header'];
  if (!empty($element['#options'])) {
    // Generate a table row for each selectable item in #options.
    foreach (element_children($element) as $key) {
      $row = array();

      $row['data'] = array();
      if (isset($element['#options'][$key]['#attributes'])) {
        $row += $element['#options'][$key]['#attributes'];
      }
      // Render the checkbox / radio element.
      $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.
    if ($element['#js_select']) {
      // Add a "Select all" checkbox.
      drupal_add_js('misc/tableselect.js');
      array_unshift($header, array('class' => array('select-all')));
    }
    else {
      // Add an empty header when radio buttons are displayed or a "Select all"
      // checkbox is not desired.
      array_unshift($header, '');
    }
  }
  return theme('table', array(
    'header'     => $header,
    'rows'       => $rows,
    'empty'      => $element['#empty'],
    'sticky'     => isset($element['#sticky']) ? $element['#sticky'] : TRUE,
    'attributes' => $element['#attributes'],
  ));

}

Can someone tell me how to use tableselect with views? I am trying to add checkboxes next to every row.

Thanks!

If I'm not mistaken there's no way to do that using Views alone. You'd have to use Views Bulk Operations (plus custom actions if needed). Module here: http://drupal.org/project/views_bulk_operations

I'm having an issue with getting my database result set to display in the tableselect form even though the query has been successfully ran according to devel and there are records in the table.

function location_admin_view_form( $form, $form_state ) {
//Select * database query for location_manager
$query = db_select( 'location_manager', 'lm' );

$query->fields( 'lm' );

$results = $query->execute();

$options = array();

$header = array( t( 'Location' ), t( 'Minimum IP Range' ), t( 'Maximum IP Range' ), t( 'Category' ) );

$result = array();

foreach ( $result as $row ) {
$options[$row->location_manager_id]= array(
'Location' => $row->location_name,
'Minimum IP Range' => $row->low_ip_range_formatted,
'Maximum IP Range' => $row->high_ip_range_formatted,
'Category' => $row->location_category,
);
}

// table for result set input.
$form['location_results'] = array(
'#theme' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No Location Information Available'),
'#js_select' => FALSE,

);

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
return $form;
}