德鲁帕尔。使用 AJAX 更新视图内容

发布于 2024-09-05 07:43:31 字数 435 浏览 7 评论 0原文

我在 Drupal 中创建了一个视图,用于检索节点列表。该视图的显示是一个页面,并且效果非常好。它甚至允许我通过参数过滤其内容。

查看当前的视图配置(点击放大):

alt text

我想使用 AJAX 来使用过滤器 (通过参数)功能,无需重新加载页面。我已启用“使用 AJAX”选项,但我不确定如何继续。 关于如何做到这一点有什么想法吗?

顺便说一句,我正在构建自己的主题(以防改变任何内容)。


更新: 基本上,当我浏览section/parameter1、section/parameter2...时,此视图有效,但我想对 AJAX 执行相同的操作,而无需重新加载页面。我希望现在更清楚了

I've created a view in Drupal that retrieves a list of nodes. The display of this view is a page and it works perfectly well. It does even allow me to filter its content by argument.

See the current's view configuration (click to enlarge):

alt text

I want to use AJAX to use the filter (by parameter) functionality without reloading the page. I've enabled the "Use AJAX" option but I am not sure on how to continue.
Any ideas on how to do that?

By the way, I'm building my own theme (in case that changes anything).


Update:
Basically, this view works when I browse through section/parameter1, section/parameter2... but I want to do the same with AJAX, without reloading the page. I hope is clearer now

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清欢 2024-09-12 07:43:31

尽管这个问题可能已经解决了,但最好的“Drupalistic 方式”在这个截屏视频中进行了解释:

http://seanbuscay.com/ajax-reader-demo

这并不难,并且可以用于视图或渲染完整节点。

根据截屏视频的作者(我可以向您保证这是正确的),以下是要记住的步骤:

  1. 编写菜单回调。
  2. 在页面回调函数中返回 Ajax 格式的内容。
  3. 设置链接格式(将“no-js”和“user-ajax”添加到指向
    菜单回调。
  4. 确保“jquery.form.js”和“drupal.ajax”库是
    已加载到页面中。

Even though this probably has been solved by this time, the best "Drupalistic way" to do it is explained in this screencast:

http://seanbuscay.com/ajax-reader-demo

It is not that hard, and works either with views or rendering full nodes.

According to the author of the screencast (and I can ensure you it is all right), this are the steps to remember:

  1. Write a menu callback.
  2. Return Ajax formatted content in a page callback function.
  3. Format the links (add "no-js" & "user-ajax" to the that point to the
    menu callback.
  4. Make sure the "jquery.form.js" the "drupal.ajax" libraries are
    loaded in the page.
眼泪也成诗 2024-09-12 07:43:31
//select required div area to refresh
Drupal.behaviors.share_status = {
            attach: function (context) {
                var initialLoad = false;
                // Drupal passes document as context on page load.
                if (context == document) {
                    initialLoad = true;
                }
                // Make sure we can run context.find().
                var ctxt = $(context);
                   $('.empty-share-status').load( 'form-content',function(context) {
                    $(".view-share-status").load("share_status/get/ajax"); //call menu action
                });
            }
        }

//get action in module page using MENU_CALLBACK

function share_status_menu() {
    $items['share_status/get/ajax'] = array(
    'page callback' => 'share_status_get_ajax', // Render HTML
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );
    return $items;
}
// render HTML content
function share_status_get_ajax() {
  $block = module_invoke('views','block_view','share_status-block_1');
  print render($block);
}
//select required div area to refresh
Drupal.behaviors.share_status = {
            attach: function (context) {
                var initialLoad = false;
                // Drupal passes document as context on page load.
                if (context == document) {
                    initialLoad = true;
                }
                // Make sure we can run context.find().
                var ctxt = $(context);
                   $('.empty-share-status').load( 'form-content',function(context) {
                    $(".view-share-status").load("share_status/get/ajax"); //call menu action
                });
            }
        }

//get action in module page using MENU_CALLBACK

function share_status_menu() {
    $items['share_status/get/ajax'] = array(
    'page callback' => 'share_status_get_ajax', // Render HTML
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  );
    return $items;
}
// render HTML content
function share_status_get_ajax() {
  $block = module_invoke('views','block_view','share_status-block_1');
  print render($block);
}
岁月静好 2024-09-12 07:43:31

这是我最终使用 ajax 加载视图块并从我在页面上创建的链接列表的 href 值传递上下文过滤器的 javascript。希望这对某人有帮助!

function getInfo(args) {

  $.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'post',
    data: {
      view_name: 'agent_lookup',
      view_display_id: 'agent_lookup_block', //your display id
      view_args: args,
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
        var viewHtml = response[1].data;
        $('#ajax-target').html(viewHtml);
        //Drupal.attachBehaviors(); //check if you need this.
      }

    },
        error: function(data) {
         alert('An error occured!');
        }
  });
}



$('.ajax_button').once().click(function(e){
    e.preventDefault();
    var the_id = $(this).attr('href');
    noSlashes = the_id.replace(/\//g,'');
    getInfo(noSlashes);

});

Here is the javascript I ended up using to load the views block with ajax and pass in the contextual filter from the href value of a list of links I created on page. Hope this helps someone!

function getInfo(args) {

  $.ajax({
    url: Drupal.settings.basePath + 'views/ajax',
    type: 'post',
    data: {
      view_name: 'agent_lookup',
      view_display_id: 'agent_lookup_block', //your display id
      view_args: args,
    },
    dataType: 'json',
    success: function (response) {
        if (response[1] !== undefined) {
        var viewHtml = response[1].data;
        $('#ajax-target').html(viewHtml);
        //Drupal.attachBehaviors(); //check if you need this.
      }

    },
        error: function(data) {
         alert('An error occured!');
        }
  });
}



$('.ajax_button').once().click(function(e){
    e.preventDefault();
    var the_id = $(this).attr('href');
    noSlashes = the_id.replace(/\//g,'');
    getInfo(noSlashes);

});
荒人说梦 2024-09-12 07:43:31

我不清楚“使用过滤器(按参数)”是什么意思。当给定页面上的选择标准发生更改时,AJAX 视图会更新结果。您似乎没有可能更改的选择标准,因此无需使用 AJAX 进行更新。可能更改的选择标准包括页面(您已关闭分页)和公开的过滤器(您的过滤器未公开)。它不包括参数(您拥有),因为这些参数不会在单个页面上更改,而是在页面之间更改。

It's not clear to me what you mean by "to use the filter (by parameter)." AJAX views update the results when selection criteria changes on a given page. You appear to have no selection criteria that might change, so there's nothing to update with AJAX. Selection criteria that might change includes page (you have paging off) and exposed filters (your filter is not exposed). It does not include arguments (which you have), since those don't change on a single page but rather between pages.

执着的年纪 2024-09-12 07:43:31

好吧,我找到了一个快速但肮脏的解决方案。您可以构建一个呈现视图的 php 文件,然后使用 ajax.load 在 div 中打印它。

ajax 调用:

$('#output_div').load('path/ajax_all_projects.php?type=art');

ajax 文件 (ajax_all_projects.php):

<?php
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$get_param = $_GET["type"];
$arguments = Array();
if(!empty($get_param)){
    array_push($arguments, $get_param);
}

$view = views_get_view('all_projects');
print $view->execute_display('Block', $arguments);
?>

我认为这可以解决问题。我希望它对其他人有帮助。 :)

Ok, I found a quick'n'dirty solution. You can build a php file that renders the view and then use ajax.load to print it in a div.

The ajax call:

$('#output_div').load('path/ajax_all_projects.php?type=art');

The ajax file (ajax_all_projects.php):

<?php
include_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$get_param = $_GET["type"];
$arguments = Array();
if(!empty($get_param)){
    array_push($arguments, $get_param);
}

$view = views_get_view('all_projects');
print $view->execute_display('Block', $arguments);
?>

I think that does the trick. I hope it helps someone else. :)

小巷里的女流氓 2024-09-12 07:43:31

您还可以使用以下内容
从 hook_menu 创建一个页面,并在菜单函数中,调用

 views_embed_view($name, $display_id = 'default')

do 例如,如果您有一个参数

print views_embed_view('my_test_view', 'block_1', $my_arg)

,而不是

view::execute_display($display_id = NULL, $args = array())

您也可以使用

view::preview($display_id = NULL, $args = array())

you can also use the following
create a page from hook_menu and in the menu function, call

 views_embed_view($name, $display_id = 'default')

do for example if you have a parameter

print views_embed_view('my_test_view', 'block_1', $my_arg)

instead of

view::execute_display($display_id = NULL, $args = array())

you could also use

view::preview($display_id = NULL, $args = array())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文