2014-08-29 32 views
0

我在Drupal中有一個表格,它調用Netezza中的外部數據庫。從Netezza檢索這些數據持續約10秒鐘。然後,基於這些信息,我必須構建一個選擇控件,讓用戶從類別列表中進行選擇。當用戶選擇一個類別時,我會花費另一個昂貴的電話給Netezza以檢索更多信息。Drupal Ajax表格

的問題是,用於所述第二相互作用(當用戶選擇了類別)的形式重新處理,因此這樣做2昂貴調用Netezza公司,而不是一個因爲任何人所期望的或願望。

您是否知道這種情況的解決方法?有沒有辦法在不重建整個表單的情況下使用Drupal Ajax框架進行ajax調用?

謝謝。

PD:閱讀有關Ajax框架的文檔我想一個解決方案可能會使用另一個路徑來指定#ajax ['path'],但還沒有完全測試這種行爲,並且如果您分享您的經驗將會感激。

PD2:我更喜歡基於Drupal Ajax Framework的解決方法,而不是緩存機制。

回答

0

我強烈建議你看看到Drupal Examples,特地叫ajax_example模塊。

這是一個快速的示例代碼,可能無法運行,但只給你的想​​法

function expensive_form($form, &$form_state) { 

    $form['category'] = array(
    '#title' => t('Cateogry'), 
    '#type' => 'select', 
    '#options' => first_expensive_operation(), 
    '#ajax' => array(
     'callback' => 'choose_category_callback', 
     'wrapper' => 'ajax-div', 
     // 'method' defaults to replaceWith, but valid values also include 
     // append, prepend, before and after. 
     // 'method' => 'replaceWith', 
     // 'effect' defaults to none. Other valid values are 'fade' and 'slide'. 
     'effect' => 'slide', 
     // 'speed' defaults to 'slow'. You can also use 'fast' 
     // or a number of milliseconds for the animation to last. 
     // 'speed' => 'slow', 
    ), 
); 

    $form['ajax_fieldset'] = array(
    '#title' => t("Ajax Fields"), 
    // The prefix/suffix provide the div that we're replacing, named by 
    // #ajax['wrapper'] above. 
    '#prefix' => '<div id="ajax-div">', 
    '#suffix' => '</div>', 
    '#type' => 'fieldset', 
    '#description' => t('This is where we get automatically updated something'), 
); 

    // this will only be executed on the second run of the form 
    // when the category is set. 
    if (isset($form_state['values']['category'])) { 
    $form['ajax_fieldset']['something'] = array(
     '#title' => t('Somethings'), 
     '#type' => 'select', 
     '#options' => second_expensive_operation($form_state['values']['category']), 
    ); 
    } 

    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'), 
); 

    return $form; 
} 



/** 
* Callback element needs only select the portion of the form to be updated. 
* Since #ajax['callback'] return can be HTML or a renderable 
* array, we can just return a piece of the form. 
*/ 
function choose_category_callback($form, $form_state) { 
    return $form['ajax_fieldset']; 
}