通过 ajax 请求加载 views_accordion [英] Loading views_accordion through an ajax request

查看:19
本文介绍了通过 ajax 请求加载 views_accordion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过自定义模块中的 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);//这返回一个视图手风琴视图if(!empty($activities)) {$html.='';$html.=$活动;$html.='';}drupal_json_output(array('data'=>$html));

手风琴/可折叠功能不适用于加载的内容.任何想法是否需要通过 module_load_include 在我的自定义模块中包含任何文件?为此需要做什么?

解决方案

每当你通过ajax请求加载内容时,你必须确保需要的js设置/文件与你的内容一起加载.

在大多数情况下,在内容加载期间通过 drupal_add_js() 填充的 $javascript 静态变量不会发送到浏览器,但您可以手动执行此操作:

  • 这里是一个工作 Drupal 6 示例:
<上一页>//获取视图内容.$view = views_embed_view($name, $display_id);//获取javascript数据.$js = drupal_add_js(NULL, NULL, 'header');//准备要在客户端处理的数据.$settings = drupal_to_js(call_user_func_array('array_merge_recursive', $js['setting']));//告诉客户端使用收集的 $settings 扩展 Drupal.settings.$script = '<脚本类型="文本/javascript">jQuery.extend(true, Drupal.settings, ' . $settings . ');</脚本>';drupal_set_header('Content-Type:text/html; charset=utf-8');打印 $script .$视图;出口;

  • 在 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 = '<脚本类型="文本/javascript">jQuery.extend(true, Drupal.settings, ' . $settings . ');</脚本>';drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');打印 $script .$视图;出口;

注意:如果您的手风琴视图依赖于特定的 .js 文件,请确保该文件来源于发生 ajax 请求的每个页面.通常,如果在没有视图的情况下加载了这样的页面(全页面加载),您必须显式地获取此文件.

您可以在 hook_page_preprocess() 实现中实现这一点:

function moduleName_preprocess_page(&$variables) {//将 .js 包含到页面中,以便视图可以在未来的 ajax 请求中依赖drupal_add_library('system', 'ui.accordion');drupal_add_js(drupal_get_path('module', 'views_accordion') . '/views-accordion.js');//添加用于修复/防止手风琴问题的 css.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>'

希望这会有所帮助!

I am currently loading my view through an ajax request in my custom module:

$.getJSON('/reports/summarized-progress/get_output_activities/'+nid, null,activities);

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));

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?

解决方案

Whenever you load content through ajax request, you have to ensure that the needed js settings/files are loaded along with your content.

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 :

  • Here a working Drupal 6 example :

  // 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;

  • Same in 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;

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.

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');

  // ...
}

... 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>'

Hope this helps!

这篇关于通过 ajax 请求加载 views_accordion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆