0

enter image description here 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 is the way to get this working ?

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',
            ),
    );




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


 $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-

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'];
          }
}
4

2 回答 2

2

我在 Drupal.org 上回复了您关于我如何处理类似问题的帖子。尝试添加

$form['mltag_new']['myselector'] =
    form_process_tableselect($form['mltag_new']['myselector']);

就在你回来之前。希望这对您的帮助比对我的帮助更大。请注意,当块从 ajax 重新加载时,#options 才会被渲染,但原始的 $form 对象似乎并不知道。

于 2012-08-10T20:20:41.853 回答
0

我知道这是几年后的事了,但我在寻找自己的解决方案时发现了这一点:

tableselect 模块以 $ 形式创建必须删除的复选框。在上面的示例中,它们$form['mltag_new']['myselector']的键与$option1原始代码中的原始键相同。如果您取消设置,请致电

 $form['mltag_new']['myselector'] = form_process_tableselect($form['mltag_new']['myselector']);

在您返回之前,它将消除虚拟行。

于 2015-12-08T14:46:10.500 回答