Drupal 7 - 如何使用AJAX更改多个下拉菜单?

时间:2012-07-05 09:23:09

标签: ajax database drupal drop-down-menu drupal-7

我是Drupal&的新手。 PHP。 我和this unanswered question有类似的问题,我自己找不到。

我需要3个依赖的下拉菜单,用户需要选择:

  • 第一 - 讲师。
  • 他的第二个相关课程。
  • 第三名 课程章节。

(数据从数据库中提取)

作为个人,他们工作正常(当我选择讲师时,课程列表会更新,当我选择课程时,章节会更新),但是当我选择讲师时,章节列表将不会更新以反映对课程下拉。

这是我到目前为止编写的代码:

function test_form($form, &$form_state)
{
  $form = array();
  $lect_options = drupal_map_assoc(get_lecturer_dropdown_options());
  $selected_lect = isset($form_state['values']['lect']) ? $form_state['values']['lect'] : key($lect_options);

  $form['lect'] = array(
    '#type' => 'select',
    '#title' => t('Lecturer Name:'),
    '#default_value' => $selected_lect,
    '#options' => $lect_options,
    '#ajax' => array(
       'callback' => 'ajax_dependent_crs_dropdown_callback',
       'wrapper' => 'dropdown-course-replace',
       ),
    );

  $course_options = drupal_map_assoc(_ajax_get_crse_dropdown_options($selected_lect));
  $selected_course = isset($form_state['values']['course']) ? $form_state['values']['course'] : key($course_options);

  $form['course'] = array(
    '#type' => 'select',
    '#title' => t('Courses:'),
    '#prefix' => '<div id="dropdown-course-replace">',
    '#suffix' => '</div>',
    '#options' => $course_options,
    '#default_value' => $selected_course,
    '#ajax' => array(
      'callback' => 'ajax_dependent_chap_dropdown_callback',
      'event' => 'change',
      'wrapper' => 'dropdown-chap-replace',
     ),
   );

  $form['chapter'] = array(
    '#type' => 'select',
    '#title' => t('Chapter:'),
    '#prefix' => '<div id="dropdown-chap-replace">',
    '#suffix' => '</div>',
    '#options' => _ajax_get_chap_dropdown_options($selected_course),
  );    

  return $form;
}

function ajax_dependent_crs_dropdown_callback($form, $form_state) {
  return $form['course'];
}
function ajax_dependent_chap_dropdown_callback($form, $form_state) {
  return $form['chapter'];
}

function get_lecturer_dropdown_options()
{
  $lect_array = array();

  $lect_result = db_query("SELECT Vod_Lectuerers.LecturerID, Vod_Lectuerers.LecFirsName, Vod_Lectuerers.LecLastName FROM Vod_Lectuerers ORDER BY Vod_Lectuerers.LecturerID");

  foreach ($lect_result as $lect_key=>$lect_row)//Get each lecturer data from the query result
  {
    $lect_array[] = $lect_row->lecturerid . '# ' . $lect_row->lecfirsname . ' ' . $lect_row->leclastname;
  }

  return $lect_array;
}

function _ajax_get_crse_dropdown_options($key) 
{
  if ($key!='')
  {   
    $course_data = array();
    $lect_id = substr($key, 0, 4);//cut the ID for the key recived by function

    $crs_result = db_query("SELECT Vod_Course.CourseID, Vod_Course.CourseName FROM Vod_Course WHERE Vod_Course.LecturerID='$lect_id' ORDER BY Vod_Course.CourseID", array($lect_id));

    foreach ($crs_result as $crs_key=>$crs_row)//Get each course data from the query result
    {
      $course_data[] = $crs_row->courseid . '# ' . $crs_row->coursename;
    }
    return $course_data;
  }
  else 
  {
    return array();
  }
}

function _ajax_get_chap_dropdown_options($key) 
{
  if ($key!='')
  {   
    $chapter_data = array();
    $course_id = substr($key, 0, 1);//cut the ID

    $cptr_result = db_query("SELECT Vod_Chapters.ChapterID, Vod_Chapters.ChapterDescription FROM Vod_Chapters WHERE Vod_Chapters.CourseID='$course_id' ORDER BY Vod_Chapters.ChapterID", array($course_id));

    foreach ($cptr_result as $cptr_key=>$cptr_row)//Get each chapter data from the query result
    {
      $chapter_data[] = $cptr_row->chapterid . '# ' . $cptr_row->chapterdescription;
    }

    return $chapter_data;
  }
  else 
  {
    return array();
  }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

您可以使用Hierarchical Select module来执行此操作。您还应该按照this tutorial开始。

希望这有帮助......穆罕默德。