Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

This relates to a bug which is as of yet unresolved in Drupal 7. It is documented here. Simply, tableselect when used with ajax is horribly broken and until the fix gets implemented in Drupal 7 I was wondering if there was anyway to achieve the same functionality that actually worked? That is, I need a form with a table, that is refreshed via ajax as a user modifies search criteria, of checkboxes that works with pagination (my query extends PagerDefault). Is there another way of achieving this in Drupal 7 that actually works? Below is some sample code that demonstrates this bug. f you have the form recreated by typing something in the textfield and hitting enter and then click "next" you will get the white screen of death. If you click back Drupal informs you of the error Notice: Undefined index: form_build_id in ajax_get_form() (line 320 of /var/www/html/includes/ajax.inc). I notice that clicking "next" the URL changes to something which is clearly not right: ?q=system/ajax&page=1

function temp_module_menu() {
$items['temp_module'] = array(
    'title' => 'Drupal 7 test pager',
    'type' => MENU_CALLBACK,
    'page callback' => 'temp_module_test_pager',
    'access callback' => TRUE,
return $items;

function temp_module_test_pager() {
    return drupal_get_form('temp_form');

function list_refresh($form, &$form_state) {
return $form;

function temp_form($form, &$form_state) {
$form['#prefix'] = '<div id="search_list">';
$form['#suffix'] = '</div>';

$form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Course Title'),
    '#size' => 30,
    '#maxlength' => 30,
    '#required' => FALSE,
    '#prefix' => '<td>',
    '#suffix' => '</td></tr>',
    '#ajax' => array(
        'callback' => 'list_refresh',
        'wrapper' => 'search_list',
        'keypress' => TRUE,
        'method' => 'replace',
        'effect' => 'fade'
//Create a list of headers for your Html table (see Drupal 7 docs for theme_table here
$header = array(
    'nid' => t('title'),
    'title' => t('nid'),
    'type' => t('type'),
    'created' => t('created'),
    'status' => t('status'),

//Create the Sql query. This uses various parts of Drupal 7's new DBTNG database abstraction layer.

$query = db_select('node', 'n')
        ->condition('status', 1) //Only published nodes, change condition as it suits you
        ->extend('PagerDefault')  //Pager Extender
        ->limit(2)    //2 results per page
        ->extend('TableSort')  //Sorting Extender
        ->orderByHeader($header)//Field to sort on is picked from $header
        ->fields('n', array(

$results = $query

$rows = array();
foreach ($results as $node) {
    $rows[$node->nid] = array(
        'title' => l($node->title, 'node/' . $node->nid),
        'nid' => $node->nid,
        'type' => $node->type,
        'created' => format_date($node->created),
        'status' => $node->status
$form['registration_table'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $rows,
    '#js_select' => FALSE,
    '#multiple' => TRUE,
    '#prefix' => '<table><tr><td>',
    '#suffix' => '</td></tr>',
$form['submit'] = array('#type' => 'submit',
    '#value' => t('Register'),
    '#disabled' => FALSE,
    '#prefix' => '<tr><td>',
    '#suffix' => '</td></tr></table>');
$form['pager'] = array('#markup' => theme('pager'));
return $form;
share|improve this question
I found the solution of this problem. Do you want the solution? –  user17281 May 7 '13 at 13:59
Sure! I would be interested to see your approach! –  adamcrussell May 13 '13 at 16:02
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.