Drupal 表单上的动态选择

发布于 2024-08-24 04:53:57 字数 1027 浏览 2 评论 0原文

我确信这对于 jQuery 专家来说很简单,但我是一名后端人员,无法找到执行此操作的最佳方法。我在 Drupal 中有两个数组,一个是视图名称列表,另一个是包含每个视图的显示列表的数组。这是填充两个数组的代码:

 //load list of views in to array for select lists
$views = views_get_all_views();
$viewnames = array();
$viewdisplays = array();
foreach ($views as $view) {
  $viewnames[$view->name] = $view->name; 
  foreach ($view->display as $k) {
    $id = $k->id;
    $title = $k->display_title;
    $viewdisplays[$view->name]['id'] = $id;
    $viewdisplays[$view->name]['title'] = $title;
  }
}

这是我正在使用的表单的片段:

$form['view'] = array(
  '#type' => 'select',
  '#title' => t('Select the view to be used for display'),
  '#options' => $viewnames,
);
$form['view_display'] = array(
  '#type' => 'select',
  '#title' => t('Select the display of the gallery view to be used'),
  '#options' => array(),
);

我想要做的是用适当的值动态填充 view_display 选择框。如果用户从“视图”选择中选择“我最喜欢的视图”,我想将 $viewdisplays['My 最喜欢的视图'] 数组显示为“view_display”字段的#options。

I'm sure this is an easy one for jQuery experts, but I'm a backend guy, and can't find the best way to do this. I have two arrays in Drupal, one is a list of names of views, and the other is an array that contains a list of displays for each view. Here's the code to populate the two arrays:

 //load list of views in to array for select lists
$views = views_get_all_views();
$viewnames = array();
$viewdisplays = array();
foreach ($views as $view) {
  $viewnames[$view->name] = $view->name; 
  foreach ($view->display as $k) {
    $id = $k->id;
    $title = $k->display_title;
    $viewdisplays[$view->name]['id'] = $id;
    $viewdisplays[$view->name]['title'] = $title;
  }
}

Here's the snippet of the form that I'm working with:

$form['view'] = array(
  '#type' => 'select',
  '#title' => t('Select the view to be used for display'),
  '#options' => $viewnames,
);
$form['view_display'] = array(
  '#type' => 'select',
  '#title' => t('Select the display of the gallery view to be used'),
  '#options' => array(),
);

What I want to do, is dynamically fill the view_display select box with the appropriate values. If the user selected "My Favorite View" from the 'view' select, I want to display the $viewdisplays['My Favorite View'] array as the #options to the 'view_display' field.

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

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

发布评论

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

评论(2

咆哮 2024-08-31 04:53:57

完成你所要求的事情并不是一件困难的事情。

  1. Drupal 知道选择的表单项有哪些选项,因为它保留了表单的副本。因此,您需要使用它可以具有的所有选项来预先填充要更改的选择的选项。此外,您还需要确保它们的值是唯一的,这样您就不会得到这样的数组:

    <前><代码>数组(
    1 => '啊',
    2=> 'ab',
    3=> '交流',
    4=> '广告',
    1 => '巴',
    2=> 'bb',

  2. 现在您有两个选择
    1. 您可以使用
      将每个视图的选项发送到 JavaScript
      drupal_add_js($array, 'setting') 。这会将 $array 添加到 Drupal.settings 变量中。
    2. 您可以通过 AJAX 获取选项。在这种情况下,您必须使用 hook_menu 创建一个菜单项.
  3. 现在您只需对视图选择中的更改做出反应,并相应地添加它们的选项,无论是从 ajax 调用还是从 js 变量获取它们。我建议使用变量方法。这可能看起来像这样:

    $("#edit-view").change(function(){
        $("#edit-view_display").html(Drupal.settings.myVariable[$(this).val()]);
    })。改变();
    

    您需要将此 js 添加到您的表单中。您可以将其放入 js 文件中,将其包装在 $(document).ready 中,然后使用 drupal_add_js

    添加它。

It's not a difficult task to do what you ask.

  1. Drupal know what options a select form item has, since it keeps a copy of the form. So you will need to prepopulate the options of the select you want to change with all the options it can have. Also you need to make sure that their values are unique, so you don't get an array like this:

    array(
      1 => 'a-a',
      2 => 'a-b',
      3 => 'a-c',
      4 => 'a-d',
      1 => 'b-a',
      2 => 'b-b',
    )
    
  2. Now you have two options
    1. You send your options for each view to the javascript using
      drupal_add_js($array, 'setting'). This will add the $array to the Drupal.settings variable.
    2. You can get the options through AJAX. In that case you would have to create a menu item with hook_menu.
  3. Now you only need to react on a change in the select for the views, and add their options accordingly, either getting them from an ajax call or the js variable. I would recommend using the variable approach. That could look something like this:

    $("#edit-view").change(function(){
        $("#edit-view_display").html(Drupal.settings.myVariable[$(this).val()]);
    }).change();
    

    You would need to add this js to your form. You could put it in a js file, wrap it in $(document).ready and add it with drupal_add_js

情深缘浅 2024-08-31 04:53:57

您见过分层选择模块吗?它是 CCK 字段类型。

有关此模块的更多详细信息(来自其项目页面):

...定义了“hierarchical_select”表单元素,这是让用户在层次结构中选择项目的一种大大增强的方式。

分层选择能够保存选择的整个谱系或仅保存“最深”的选择。您可以将其配置为强制用户在树中尽可能深地进行选择,或允许用户在树中的任何位置选择项目。可以对级别进行标记,您可以配置限制可以选择的项目数量、配置保管箱的标题、选择站点范围的动画延迟等。您甚至可以通过层次选择创建新的项目和级别!

Have you seen the Hierarchical Select module? It's a CCK field type.

Some more details about this modules (from its project page):

... defines the "hierarchical_select" form element, which is a greatly enhanced way for letting the user select items in a hierarchy.

Hierarchical Select has the ability to save the entire lineage of a selection or only the "deepest" selection. You can configure it to force the user to make a selection as deep as possible in the tree, or allow the user to select an item anywhere in the tree. Levels can be labeled, you can configure limit the number of items that can be selected, configure a title for the dropbox, choose a site-wide animation delay, and so on. You can even create new items and levels through Hierarchical Select!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文