2014-08-29 69 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']; 
}