使用 AJAX 嵌入视图 [英] Embed a View using AJAX

查看:17
本文介绍了使用 AJAX 嵌入视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一个参数的视图和一组公开的过滤器.当用户过滤视图时,表单使用 Ajax 提交,过滤器使用 location.hash 附加到 url.

I have a view with one argument, and a set of exposed filters. When the user filters the view, the form is submitted using Ajax, and the filters are appended to the url using location.hash.

我的目标是在初始页面加载时过滤视图,如果 location.hash 中存在过滤器.

My goal is to filter the view upon initial page load, if the filters are present in the location.hash.

目前,我正在通过 Ajax 回调加载视图,效果非常好.但最大的问题是视图的 Ajax 不起作用.

Currently, I'm loading the view through an Ajax callback, which works perfectly fine. But the big problem is that Ajax for the view doesn't work.

这是加载视图的回调.

// Load the view object.
$view = views_get_view('taxonomy_term');
$view->set_display('page');
$view->set_use_ajax(TRUE);

// Pass the current tid as the argument.
$view->set_arguments(array($tid));
// Set the current page.
$view->set_current_page($page);
// Set the exposed filters.
$view->get_exposed_input();

// Execute.
return $view->execute_display();

当我直接导航到该回调时,一切正常.但是当我通过 Ajax 加载它时不会.

When I navigate directly to that callback, everything works. But not when I load it through Ajax.

有什么想法吗?

更新:Drupal.behaviors.ViewsAjaxView() 似乎由于某种原因没有执行.如果我手动执行它,一切正常.

Update: It seems that Drupal.behaviors.ViewsAjaxView() doesn't execute for some reason. If I execute it manually, everything works.

推荐答案

好的,我找到了答案.

我现在不是从我自己的回调中加载视图,而是从常规的 ajax 回调中加载视图.

Instead of loading the View from my own callback, I'm now loading the View from the regular ajax callback.

在我的页面上,我创建了视图对象,并将配置添加到 Drupal.settings.

On my page, I create the view object, and add the configuration to Drupal.settings.

$view = views_get_view('taxonomy_term');
$view->set_display('page');
$view->set_use_ajax(TRUE);
$view->set_arguments(array($tid));
$settings = array(
  'views' => array(
    'ajax_path' => url('views/ajax'),
    'ajaxViews' => array(
      array(
        'view_name' => $view->name,
        'view_display_id' => $view->current_display,
        'view_args' => check_plain(implode('/', $view->args)),
        'view_path' => check_plain($_GET['q']),
        'view_base_path' => $view->get_path(),
        'view_dom_id' => 1,
        'pager_element' => $view->pager['element'],
      ),
    ),
  ),
);
drupal_add_js($settings, 'setting');
views_add_js('ajax_view');

然后我加载我的 js,它将 location.hash 中的当前过滤器添加到设置中.最后,加载视图.

Then I load my js, which adds the current filter from the location.hash to the settings. And finally, loads the View.

var data = {};
// Add view settings to the data.
for (var key in Drupal.settings.views.ajaxViews[0]) {
  data[key] = Drupal.settings.views.ajaxViews[0][key];
}
// Get the params from the hash.
if (location.hash) {
  var q = decodeURIComponent(location.hash.substr(1));
  var o = {'f':function(v){return unescape(v).replace(/+/g,' ');}};
  $.each(q.match(/^??(.*)$/)[1].split('&'), function(i,p) {
    p = p.split('=');
    p[1] = o.f(p[1]);
    data[p[0]] = data[p[0]]?((data[p[0]] instanceof Array)?(data[p[0]].push(p[1]),data[p[0]]):[data[p[0]],p[1]]):p[1];
  });
}
$.ajax({
  url: Drupal.settings.views.ajax_path,
  type: 'GET',
  data: data,
  success: function(response) {
    var viewDiv = '.view-dom-id-' + data.view_dom_id;
    $('#content > div.limiter').html(response.display);
    // Call all callbacks.
    if (response.__callbacks) {
      $.each(response.__callbacks, function(i, callback) {
        eval(callback)(viewDiv, response);
      });
    }
  },
  error: function(xhr) {
    $('#content > div.limiter').html('<p id="artist-load-error">Error text.</p>');
    $('#block-request-0').hide();
  },
  dataType: 'json'
});

这样,视图通过常规流程加载,一切都按预期工作 =)

This way, the view loads through the regular flow, and everything works as expected =)

这篇关于使用 AJAX 嵌入视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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