问题描述
我目前正在自定义模块中通过ajax请求加载我的视图:
I am currently loading my view through an ajax request in my custom module:
$.getJSON('/reports/summarized-progress/get_output_activities/'+nid, null,activities);
上述请求的drupal页面返回以下内容:
The drupal page for the above request returns the following:
$output_arg=arg(3);
$html="";
$activities=views_embed_view('activities','block_activities',$output_arg); //this returns a view accordion view
if(!empty($activities)) {
$html.='';
$html.=$activities;
$html.='';
}
drupal_json_output(array('data'=>$html));
手风琴/可折叠功能在加载的内容上不起作用。任何想法,无论我需要通过module_load_include包含我的自定义模块中的任何文件吗?需要做哪些工作?
The accordion/collapsible functionality is not working on the loaded content. Any ideas whether I need to include through module_load_include any files in my custom module? What needs to be done for this to work?
推荐答案
无论何时通过ajax请求加载内容,您必须确保所需的js设置/文件与您的内容一起加载。
Whenever you load content through ajax request, you have to ensure that the needed js settings/files are loaded along with your content.
在大多数情况下,通过drupal_add_js()在内容加载期间填充的$ javascript静态变量不会发送到浏览器,但您可以手动执行此操作:
In most cases, the $javascript static variable that is filled during a content load through drupal_add_js() is not sent to the browser, but you can do this manually :
- 这里有一个正在工作的Drupal 6示例:
// Get the view content.
$view = views_embed_view($name, $display_id);
// Get javascript data.
$js = drupal_add_js(NULL, NULL, 'header');
// Prepare data to be processed clientside.
$settings = drupal_to_js(call_user_func_array('array_merge_recursive', $js['setting']));
// Tell the client to extend Drupal.settings with collected $settings.
$script = '<script type="text/javascript">
jQuery.extend(true, Drupal.settings, ' . $settings . ');
</script>';
drupal_set_header('Content-Type:text/html; charset=utf-8');
print $script . $view;
exit;
- 在Drupal 7中相同:
$view = views_embed_view($name, $display_id);
$js = drupal_add_js(NULL, array('type' => 'setting'));
$settings = drupal_json_encode(call_user_func_array('array_merge_recursive', $js['settings']['data']));
$script = '<script type="text/javascript">
jQuery.extend(true, Drupal.settings, ' . $settings . ');
</script>';
drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');
print $script . $view;
exit;
注意:
如果您的手风琴视图依赖于特定的.js文件,请确保此文件是在每一个需要进行ajax请求的页面中提供的。通常,如果没有视图,这样一个页面被加载(全页加载),你必须明确地提供这个文件。
Note :If your accordion view rely on a specific .js file, ensure this file is sourced inside every page where ajax requests take place. Usually, you'll have to explicitly source this file if such a page is loaded (full page load) without the view.
您可以在hook_page_preprocess()实现中实现此目的:
You can achieve this in a hook_page_preprocess() implementation :
function moduleName_preprocess_page(&$variables) {
// Include .js to the page so views can rely on in future ajax request
drupal_add_library('system', 'ui.accordion');
drupal_add_js(drupal_get_path('module', 'views_accordion') . '/views-accordion.js');
// Add the css for fixing/preventing accordion issues.
drupal_add_css(drupal_get_path('module', 'views_accordion') . '/views-accordion.css');
// ...
}
...或者通过在请求内容时包含该文件(就像我们对Drupal.settings一样),只需在ajax回调的$ script变量中添加脚本标签:
... or by including the file when the content is requested (as we do for Drupal.settings), just add a script tag in the $script variable in the ajax callback :
$script .= '<script type="text/javascript" src="sourcefile.js"></script>'
希望这有帮助!
这篇关于通过ajax请求加载views_accordion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!