Having issues trying to return new rows in an AJAX-driven tableselect...

4 years 11 months ago #8105 by PeachyPaul
Long story short, I've created a tableselect in a custom module and the form this belongs to has a textfield that I plan on using as a means of searching a database for specific IDs. Based on the IDs found, it will then instigate an AJAX callback to update the tableselect rows.

I've managed to get the textfield to fire off an AJAX callback via both blur and enter actions but I'm having problems getting the new table (with new options) to be updated within the AJAX callback. From all the examples I'm seeing all over the internet, it seems that $form and $form_state are returned in the AJAX callback interchangeably. Which one needs to be returned? It makes sense to believe that it would be $form_state as this would have the updated values...

...but once you know which object to return, how do you update the table exactly?

Below is the AJAX callback:

function mymodule_accounts_management_ajax_callback($form, &$form_state){
drupal_set_message('mymodule_accounts_management_ajax_callback'); //Header...
$header = array(
'status' => t('Heading 1'),
'division' => t('Heading 2'),
'username' => t('Heading 3'),
'full_name' => t('Heading 4'),
'user_type' => t('Heading 5'),
'last_login' => t('Heading 6'),
'sponsor' => t('Heading 7'),
);

$options = array(
1 => array(
'status' => t('Changed 1!'),
'division' => t('Changed 1!'),
'username' => t('Changed 1!'),
'full_name' => t('Changed 1!'),
'user_type' => t('Changed 1!'),
'last_login' => t('Changed 1!'),
'sponsor' => t('Changed 1!'),
),
2 => array(
'status' => t('Changed 2!'),
'division' => t('Changed 2!'),
'username' => t('Changed 2!'),
'full_name' => t('Changed 2!'),
'user_type' => t('Changed 2!'),
'last_login' => t('Changed 2!'),
'sponsor' => t('Changed 2!'),
),
3 => array(
'status' => t('Changed 3!'),
'division' => t('Changed 3!'),
'username' => t('Changed 3!'),
'full_name' => t('Changed 3!'),
'user_type' => t('Changed 3!'),
'last_login' => t('Changed 3!'),
'sponsor' => t('Changed 3!'),
),
);


//Table Settings...
$form = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No content available.'),
'#prefix' => '<div id="mymodule_accounts">',
'#suffix' => '</div>',
//'#default_value' => array(1=>1, 2=>0, 3=>1),//Uncomment for default checkbox values...
'#empty' => 'No records found.',
);

drupal_set_message(dpm($form));//(It looks like the new options have been set...)

//$form_state = true;//Maybe I need to use $form_state and rebuild the form?

return $form;//Place TABLE MARKUP here!
}
And here's how I setup the callback to be used:

function mymodule_accounts_form($form, &$form_state){

//Search Input...
$form = array(
'#type' => 'textfield',
'#title' => t('Search:'),
'#default_value' => '',
'#size' => 30,
'#maxlength' => 11,
'#prefix' => '<div id="mymodule_search">',
'#suffix' => '</div>',
'#ajax' => array(
'callback' => 'mymodule_accounts_management_ajax_callback',
'wrapper' => 'mymodule_accounts',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('form ID', 1, 2, 3),
),
'#attributes' => array('onkeypress' => 'if(event.keyCode==13){this.form.submit();}'),
);

//Header...
$header = array(
'status' => t('Heading 1'),
'division' => t('Heading 2'),
'username' => t('Heading 3'),
'full_name' => t('Heading 4'),
'user_type' => t('Heading 5'),
'last_login' => t('Heading 6'),
'sponsor' => t('Heading 7'),
);

$options = array(
1 => array(
'status' => t('Not Changed.'),
'division' => t('Not Changed.'),
'username' => t('Not Changed.'),
'full_name' => t('Not Changed.'),
'user_type' => t('Not Changed.'),
'last_login' => t('Not Changed.'),
'sponsor' => t('Not Changed.'),
),
)

//Table Settings...
$form = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#empty' => t('No content available.'),
'#prefix' => '<div id="mymodule_accounts">',
'#suffix' => '</div>',
//'#default_value' => array(1=>1, 2=>0, 3=>1),//Uncomment for default checkbox values...
'#empty' => 'No records found.',
);

return $form;
}

The problem I'm having with the AJAX callback that gets fired after the textfield experiences blur is that the table shows "No content available." Oddly enough, if I display the contents of $form, the new fields that I set in the AJAX callback are there... It's like the table just doesn't get returned correctly from in the callback.

Do I need to use $form_state instead and rebuilt the form inside the AJAX callback?

Any insight into this would be appreciated.

Please Log in or Create an account to join the conversation.

About us

We provide high quality Joomla components created by a co-founder and original core developer of Joomla. For over a decade, our products have been used by more than 20.000 webmasters around the world.

Stay in touch