我目前正通过自定义模块中的ajax请求加载我的视图:
$.getJSON('/reports/summarized-progress/get_output_activities/'+nid, null,activities);
上述请求的drupal页面返回以下内容:
$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包含我的自定义模块中的任何文件?需要做些什么才能发挥作用?
答案 0 :(得分:1)
每当您通过ajax请求加载内容时,您必须确保所需的js设置/文件与您的内容一起加载。
在大多数情况下,通过drupal_add_js()在内容加载期间填充的$ javascript静态变量不会发送到浏览器,但您可以手动执行此操作:
// 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;
$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请求的每个页面。通常,如果在没有视图的情况下加载这样的页面(整页加载),您必须显式地获取此文件。
您可以在hook_page_preprocess()实现中实现此目的:
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变量中添加一个脚本标记:
$script .= '<script type="text/javascript" src="sourcefile.js"></script>'
希望这有帮助!