【问题标题】:drupal ajax form changedrupal ajax 表单更改
【发布时间】:2013-02-21 18:35:10
【问题描述】:

我正在开发一个 drupal 7 模块,我希望使用 ajax 过滤器在页面 (MENU_LOCAL_TASK node/%node/something) 上打印信息。 我创建了一个表单并添加了 2 个复选框,默认情况下有 1 个,其他没有。我想根据选中的复选框向用户显示信息。 1 在表格第 1 行显示,2 在表格第 2 行显示。如果其中一些已关闭,则该表行已关闭。我是否提到过,我想在不提交和重新加载的情况下解决它,只需要 ajax。 我在两个“复选框”中添加了以下'ajax' => array('callback' => 'my_module_callback') .这是剩下的代码,简单化了。

function my_module_callback($form, $form_state) {
    $data = array();
    $nid = 1;
    if ($form_state['values']['checkbox1']) { 
        $data += load_data($nid, "checkbox1");
    }
    if ($form_state['values']['checkbox1']) { 
        $data += load_data($nid, "checkbox2");
    }
    $commands[] = ajax_command_html("#here", my_module_table($data));
    return array('#type' => 'ajax', '#commands' => $commands);
}


function my_module_table($data){
    //do some stuff with the data in a foreach
    return theme("my_module_fancy_table",array("data" => $data));
}

function theme_my_module_fancy_table($data){ //registered with my_module_theme()
    // putting html into $output in a foreach
    return $output;
}

function my_module_page_callback_from_menu_function($nid){
    $output = drupal_render(drupal_get_form('my_module_custom_ajax_form'));
    $output .= "adding other stuffs including div#here";
    return $output;
}

首先这是做到这一点的“好方法”,因为我有点失去信心:) 第二个问题,如何在页面加载时显示数据,现在需要更改一个复选框才能看到一些信息。

感谢和抱歉的简短描述:)

【问题讨论】:

    标签: drupal drupal-7 drupal-ajax


    【解决方案1】:

    你不应该真的在回调中进行处理,它应该在表单构建函数中完成。回调通常只返回表单中已更改的部分。另外,我认为在这种情况下不需要设置 commands[],因为返回的部分表单会自动替换 'wrapper' 设置的内容。

    function my_module_form($form, $form_state){
      $data = array();
      $nid = 1;
      if ($form_state['values']['checkbox1']) { 
        $data += load_data($nid, "checkbox1");
      }
      if ($form_state['values']['checkbox2']) { 
        $data += load_data($nid, "checkbox2");
      }
    
      $form = array();
      $form['checkbox1'] = array(
        '#type' => 'checkbox',
        '#ajax' => array(
          'callback' => 'my_module_callback'
          'wrapper' => 'mydata',
          'event' => 'change',
        ),
      );
      $form['checkbox2'] = array(
        '#type' => 'checkbox',
        '#ajax' => array(
          'callback' => 'my_module_callback'
          'wrapper' => 'mydata',
          'event' => 'change',
        ),
      );
      $form['mydata'] = array(
        '#prefix' => '<div id="mydata">',
        '#suffix' => '</div>',
        '#markup' => my_module_table($data),
      );
      return $form;
    }
    
    function my_module_callback($form, $form_state){
      // $form_state['rebuild'] = true; may have to be set because the form has not been submitted and wont be rebuilt...I think, I cant remember for sure.
      return $form['mydata'];
    }
    

    要在页面加载时显示数据,您只需在表单构建函数中更改设置数据的逻辑即可。 另外,仅供参考,有一个专门针对 drupal 的堆栈站点:http://drupal.stackexchange.com

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多