Drupal 表单上的动态选择
我确信这对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成你所要求的事情并不是一件困难的事情。
Drupal 知道选择的表单项有哪些选项,因为它保留了表单的副本。因此,您需要使用它可以具有的所有选项来预先填充要更改的选择的选项。此外,您还需要确保它们的值是唯一的,这样您就不会得到这样的数组:
<前><代码>数组(
1 => '啊',
2=> 'ab',
3=> '交流',
4=> '广告',
1 => '巴',
2=> 'bb',
)
将每个视图的选项发送到 JavaScript
drupal_add_js($array, 'setting')
。这会将$array
添加到Drupal.settings
变量中。hook_menu
创建一个菜单项.现在您只需对视图选择中的更改做出反应,并相应地添加它们的选项,无论是从 ajax 调用还是从 js 变量获取它们。我建议使用变量方法。这可能看起来像这样:
您需要将此 js 添加到您的表单中。您可以将其放入 js 文件中,将其包装在
$(document).ready
中,然后使用drupal_add_js
添加它。
It's not a difficult task to do what you ask.
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:
drupal_add_js($array, 'setting')
. This will add the$array
to theDrupal.settings
variable.hook_menu
.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:
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 withdrupal_add_js
您见过分层选择模块吗?它是 CCK 字段类型。
有关此模块的更多详细信息(来自其项目页面):
Have you seen the Hierarchical Select module? It's a CCK field type.
Some more details about this modules (from its project page):