2013-02-21 44 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