2013-02-21 57 views
0

我正在使用drupal 7模块,我希望使用ajax过滤器在页面上打印信息(MENU_LOCAL_TASK节点/%node/something)。 我创建了一个表单并添加了2个复选框,其中1个默认其他不是。我想根据所选的复选框向用户显示信息。 1在表格行1上显示,2在表格2上显示。如果其中一部分关闭,则表格行关闭。我有没有提到,我想解决它没有提交和重新加载,只有ajax。 我添加到两个'复选框'以下'ajax' => array('callback' => 'my_module_callback') 。这是其余的代码,简单。drupal ajax表单更改

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

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

感谢和遗憾的简短描述:)

回答

1

你真的不应该在回调做的处理,应该在形式建筑功能来完成。回调通常只返回已更改表单的部分。另外,我不认为在这种情况下不需要设置命令[],因为表单的返回部分将自动替换'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']; 
} 

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