HI,
What I am trying to do is display a table with checkboxes on the press of a button by ajax. The table should be initially hidden and get populated on the fly by a function call.
If initially I load $options1 with some dummy values , then after ajax call it throws in an error saying-

Notice: Undefined index: red in theme_tableselect() (line 3285 of D:\wamp\www\drupal7\includes\form.inc).

where 'red' is the index of a dummy row value and #options don't get populated with the new values.
What error am I making ??

Here is the code for the form-

$form['mltag_new']['tag'] = array(
        '#type' => 'button',
        '#value' => t("Suggest Tags"),
        '#ajax' => array(
            'callback' => 'mltag_suggest_tags_ajax',
            'wrapper' => 'mltag_suggest_tags_table_div',
            'effect' => 'slide',
            ),
    );
    
 $form['mltag_new']['myselector'] = array (
        '#type' => 'tableselect',
        '#title' => 'My Selector',
        '#header' => $header,
        '#options' => $options1,
        '#prefix' => '<div id="mltag_suggest_tags_table_div">',
        '#suffix' => '</div>',        
    );
   
    return $form;

and the Ajax callback looks something like this-

<?php
function mltag_suggest_tags_ajax($form, $form_state) {
		  //$content has some content
		  //pass the content to a function 
		  include_once 'includes/content_tag.inc';
		  $tags = mltag_content_tag($content, variable_get('algo_type'), 20);
		  
		  if (empty($tags)) {
			$output .= t('Content is insufficient to generate Tags using this algorithm. <br>Please choose other algorithm from Settings Page.');
			$form['mltag_new']['sample_text']['#markup'] = $output;
			return $form['mltag_new']['sample_text'];
		  }
		  else {
			$algo = variable_get('algo_type');
			if ($algo == 1) {
			  $header = array(
				  'tag' => t('Tag'),
				  'frequency' => t('Frequency'), 
				  );
			  
			  $options = array();      
			  foreach ($tags as $key => $value) {
				$options[$key] = array(
					'tag' => $key,
					'frequency' => $value,
					);
			  }    
			}
			
			elseif ($algo == 2) {
			  $header = array(
				  'tag' => t('Tag'),
				  'chi' => t('Chi Square Value'), 
				  );
			  
			  $options = array();
			  
			  foreach ($tags as $key => $value) {
				$options[$key] = array(
					'tag' => $key,
					'chi' => $value,
				); 
			  } 
			  
			}
		  
		  $form['mltag_new']['myselector']['#header'] = $header; 
		  $form['mltag_new']['myselector']['#options'] = $options;
		  return $form['mltag_new']['myselector'];
		  }
}
?>

Comments

tcm5025’s picture

I've been working on something similar almost all day today. I got the table to reload with my new value, by doing what you had there and then before the return calling $table = form_process_tableselect($table); on the element. This takes the #options and #header in the table and creates the proper subarrays needed to render the table properly. However, the original $form is not holding the new options. So, the next time this gets called, it doesn't see the #options that I updated last time.

Anyway, for what you have here, you can probably try adding $form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']); just before your return. That might take care of your problem. Unfortunately, it doesn't solve my trouble of having those #options persist to the next time the button gets called.

arjkap’s picture

I replied on stackoverflow and attached a screenshot of my problem there.
ya thanks a lot ! the table seems to have loaded the values now ! finally some peace to my troubled soul !! but I am still getting the "undefined index red in theme_tableselect()". The index "red" is of a dummy row that I am displaying when form loads initially.

$options1 = array();
    $options1['red']['tag'] = "A red row";
    $options1['red']['chi'] = "A red row";

And also the 1st row of the table is rendered blank. Any ideas on that ?
is there a way to unset the index ? or my main goal was to keep the table hidden initially on page load and display it only when the button is pressed, so any ideas about that ?

focal55’s picture

form_process_tableselect() was just wanted I needed. Thank you!

Joe Ybarra
www.focal55.com

2ndmile’s picture

Just wanted to say thanks for the form_process_tableselect($element) hint