开发者

Drupal. Use AJAX to update view's content

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

Drupal. Use AJAX to update view's content

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 cl开发者_StackOverflow中文版earer now


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.


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


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

});


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.


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. :)


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())
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜