在ajax回调函数中填充#options,#header用于tableselect

时间:2012-08-09 06:35:36

标签: drupal-7 drupal-modules

enter image description here 我想要做的是在ajax按下按钮时显示带有复选框的表格。该表应该最初隐藏,并通过函数调用即时填充。 如果最初我用一些虚拟值加载$ options1,那么在ajax调用之后它会抛出错误说 -

  

注意:未定义的索引:theme_tableselect()中的红色(第3285行)   d:\瓦帕\ WWW \ drupal7 \包括\ form.inc)

其中'red'是虚拟行值的索引,而#options不会填充新值。让这个有效的方法是什么?

以下是表单的代码 -

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

并且Ajax回调看起来像这样 -

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

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:0)

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

tableselect模块在$表单中创建必须删除的复选框。在上面的示例中,它们位于$form['mltag_new']['myselector']中,其密钥等于原始代码中的原始$option1。如果您取消设置,则致电

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

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