2016-02-03 43 views
0

我正在使用Drupal7,因爲我有模塊在哪裏我已經使用php編寫了自己的函數,現在我的要求中有兩個下拉列表例如,當我從第一個下拉菜單中選擇「A」,然後在第二個下拉菜單中僅與「A」相關時,所有數據都需要在第二個下拉菜單中顯示, 簡而言之,如果我從第一個下拉列表中選擇城市名稱,則所有這個城市的大學應該在第二個下拉列表中顯示。我如何在php中實現這一點?我發現這個鏈接和代碼片段http://w3shaman.com/article/creating-ajax-dropdown-drupal-7但沒有用,如果有人回答這個問題,對我非常有幫助。如何通過從相應的下拉列表中選擇特定的數據在下拉列表中顯示相關數據

回答

0

如果您使用Drupal Form API創建了表單,那麼您可以使用Drupal Ajax實現此功能。 以下是來自Drupal Ajax示例模塊的代碼片段

function ajax_example_dependent_dropdown($form, &$form_state) { 
    // Get the list of options to populate the first dropdown. 
    $options_first = _ajax_example_get_first_dropdown_options(); 
    // If we have a value for the first dropdown from $form_state['values'] we use 
    // this both as the default value for the first dropdown and also as a 
    // parameter to pass to the function that retrieves the options for the 
    // second dropdown. 
    $selected = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first); 
    $form['dropdown_first'] = array(
    '#type' => 'select', 
    '#title' => 'Instrument Type', 
    '#options' => $options_first, 
    '#default_value' => $selected, 
    // Bind an ajax callback to the change event (which is the default for the 
    // select form type) of the first dropdown. It will replace the second 
    // dropdown when rebuilt. 
    '#ajax' => array(
     // When 'event' occurs, Drupal will perform an ajax request in the 
     // background. Usually the default value is sufficient (eg. change for 
     // select elements), but valid values include any jQuery event, 
     // most notably 'mousedown', 'blur', and 'submit'. 
     // 'event' => 'change', 
     'callback' => 'ajax_example_dependent_dropdown_callback', 
     'wrapper' => 'dropdown-second-replace', 
    ), 
); 

    $form['dropdown_second'] = array(
    '#type' => 'select', 
    '#title' => $options_first[$selected] . ' ' . t('Instruments'), 
    // The entire enclosing div created here gets replaced when dropdown_first 
    // is changed. 
    '#prefix' => '<div id="dropdown-second-replace">', 
    '#suffix' => '</div>', 
    // When the form is rebuilt during ajax processing, the $selected variable 
    // will now have the new value and so the options will change. 
    '#options' => _ajax_example_get_second_dropdown_options($selected), 
    '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '', 
); 
    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Submit'), 
); 
    return $form; 
}  

function ajax_example_dependent_dropdown_callback($form, $form_state) { 
    return $form['dropdown_second']; 
} 

function _ajax_example_get_second_dropdown_options($key = '') { 
    $options = array(
    t('String') => drupal_map_assoc(
    array(
     t('Violin'), 
     t('Viola'), 
     t('Cello'), 
     t('Double Bass'), 
    ) 
    ), 
    t('Woodwind') => drupal_map_assoc(
    array(
     t('Flute'), 
     t('Clarinet'), 
     t('Oboe'), 
     t('Bassoon'), 
    ) 
    ), 
    t('Brass') => drupal_map_assoc(
    array(
     t('Trumpet'), 
     t('Trombone'), 
     t('French Horn'), 
     t('Euphonium'), 
    ) 
    ), 
    t('Percussion') => drupal_map_assoc(
    array(
     t('Bass Drum'), 
     t('Timpani'), 
     t('Snare Drum'), 
     t('Tambourine'), 
    ) 
    ), 
); 
    if (isset($options[$key])) { 
    return $options[$key]; 
    } 
    else { 
    return array(); 
    } 
} 
相關問題