第二个分类自动完成取决于第一个分类自动完成

发布于 2024-12-11 10:31:46 字数 466 浏览 0 评论 0原文

Drupal-7
第二个分类自动完成取决于第一个分类自动完成。

添加优惠:
步骤 1) 具有自动补全功能的国家/地区,城市为空
国家: U
------------美国
城市:

步骤 2)当我们选择 USA 时,我们可以使用具有自动完成功能的 City
国家: 美国
城市: 是
--------伯克利

步骤3)但我们只是插入新项目Bexxx
国家: 美国
城市: Bexxx

搜索报价:
步骤 1) 国家/地区 - 从列表中选择美国,城市为空
国家: 美国
-----------德国
城市:


步骤 2) 当我们选择 USA 时,列表中有 3 项
国家: 美国
城市: 伯克利
--------柏林
--------Bexxxxx

Drupal-7
Second taxonomy autocomplet depend on first taxonomy autocomplet.

Add offer:
step 1) Country with autocomplet , City is empty
Country: U
------------USA
City:

step 2) when we select USA then we can use City with autocomplet
Country: USA
City: Be
-------Berkeley

Step 3) but we just insert new item Bexxx
Country: USA
City: Bexxx

Search offer:
Step 1) Country - select USA from the list, City is empty
Country: USA
-----------Germany
City:

Step 2) when we select USA then we have 3 items on the list
Country: USA
City: Berkeley
-------Berlin
-------Bexxxxx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明明#如月 2024-12-18 10:31:46

我使用 Drupal 6,模块 分层选择视图。然后在视图中我使用带有暴露过滤器的字段。

I use Drupal 6, module Hierarchical select and Views. Then in Views I used field with exposed filter.

软的没边 2024-12-18 10:31:46

这是依赖字段的一个简单示例。创建一个名为“myajx”的简单模块并按原样放置此代码。现在通过 url 访问,即“http://localhost/mypage”。现在从第一个字段中选择一个值,您将看到下面的字段中仅列出了其相关值。

<?php

/**
 * Implementation of hook_menu().
 * Registers a form-based page that you can access at "http://localhost/mypage"
 */
 function myajx_menu(){
  return array(
    'mypage' => array(
        'title' => 'A page to test ajax',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('myajx_page'),
        'access arguments' => array('access content'), 
     )
   );
  }



/**
 * A form with a dropdown whose options are dependent on a
 * choice made in a previous dropdown.
 *
 * On changing the first dropdown, the options in the second are updated.
 */
 function myajx_page($form, &$form_state) {

  // Get the list of options to populate the first dropdown.
   $options_first = myajx_first_dropdown_options();

   $value_dropdown_first = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);

   $form['dropdown_first'] = array(
      '#type' => 'select',
      '#title' => 'First Dropdown',
      '#options' => $options_first,
      '#default_value' => $value_dropdown_first,

      // 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' => 'myajx_ajax_callback',
          'wrapper' => 'dropdown_second_replace',
      ),
   );
  $form['dropdown_second'] = array(
      '#type' => 'select',
      '#title' => 'Second Dropdown',
      '#prefix' => '<div id="dropdown_second_replace">',
      '#suffix' => '</div>',
      '#options' => myajx_second_dropdown_options($value_dropdown_first),
      '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );
  return $form;
 }

/**
 * Selects just the second dropdown to be returned for re-rendering
 *
 * Since the controlling logic for populating the form is in the form builder
* function, all we do here is select the element and return it to be updated.
*
* @return renderable array (the second dropdown)
*/
function myajx_ajax_callback($form, $form_state) {
         return $form['dropdown_second'];
}


/**
 * Helper function to populate the first dropdown. This would normally be
 * pulling data from the database.
 *
 * @return array of options
 */
 function myajx_first_dropdown_options() {
    return array(
        'colors' => 'Names of colors',
        'cities' => 'Names of cities',
        'animals' => 'Names of animals',
    );
 }


function myajx_second_dropdown_options($key = '') {
    $options = array(
        'colors' => array(
            'red' => 'Red',
            'green' => 'Green',
            'blue' => 'Blue'
         ),
        'cities' => array(
            'paris' => 'Paris, France',
            'tokyo' => 'Tokyo, Japan',
            'newyork' => 'New York, US'
        ),
        'animals' => array(
            'dog' => 'Dog',
            'cat' => 'Cat',
            'bird' => 'Bird'
        ),  
     );
     if (isset($options[$key])) {
        return $options[$key];
     }
    else {
        return array();
    }
 }

This is a simple example of dependent fields. Make a simple module named "myajx" and placed this code as it is. Now access through url ie "http://localhost/mypage". Now select a value from first field and you will see only its dependent values are listed in below field.

<?php

/**
 * Implementation of hook_menu().
 * Registers a form-based page that you can access at "http://localhost/mypage"
 */
 function myajx_menu(){
  return array(
    'mypage' => array(
        'title' => 'A page to test ajax',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('myajx_page'),
        'access arguments' => array('access content'), 
     )
   );
  }



/**
 * A form with a dropdown whose options are dependent on a
 * choice made in a previous dropdown.
 *
 * On changing the first dropdown, the options in the second are updated.
 */
 function myajx_page($form, &$form_state) {

  // Get the list of options to populate the first dropdown.
   $options_first = myajx_first_dropdown_options();

   $value_dropdown_first = isset($form_state['values']['dropdown_first']) ? $form_state['values']['dropdown_first'] : key($options_first);

   $form['dropdown_first'] = array(
      '#type' => 'select',
      '#title' => 'First Dropdown',
      '#options' => $options_first,
      '#default_value' => $value_dropdown_first,

      // 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' => 'myajx_ajax_callback',
          'wrapper' => 'dropdown_second_replace',
      ),
   );
  $form['dropdown_second'] = array(
      '#type' => 'select',
      '#title' => 'Second Dropdown',
      '#prefix' => '<div id="dropdown_second_replace">',
      '#suffix' => '</div>',
      '#options' => myajx_second_dropdown_options($value_dropdown_first),
      '#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
  );
  return $form;
 }

/**
 * Selects just the second dropdown to be returned for re-rendering
 *
 * Since the controlling logic for populating the form is in the form builder
* function, all we do here is select the element and return it to be updated.
*
* @return renderable array (the second dropdown)
*/
function myajx_ajax_callback($form, $form_state) {
         return $form['dropdown_second'];
}


/**
 * Helper function to populate the first dropdown. This would normally be
 * pulling data from the database.
 *
 * @return array of options
 */
 function myajx_first_dropdown_options() {
    return array(
        'colors' => 'Names of colors',
        'cities' => 'Names of cities',
        'animals' => 'Names of animals',
    );
 }


function myajx_second_dropdown_options($key = '') {
    $options = array(
        'colors' => array(
            'red' => 'Red',
            'green' => 'Green',
            'blue' => 'Blue'
         ),
        'cities' => array(
            'paris' => 'Paris, France',
            'tokyo' => 'Tokyo, Japan',
            'newyork' => 'New York, US'
        ),
        'animals' => array(
            'dog' => 'Dog',
            'cat' => 'Cat',
            'bird' => 'Bird'
        ),  
     );
     if (isset($options[$key])) {
        return $options[$key];
     }
    else {
        return array();
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文