4

While viewing a views generated page, like for instance a taxonomy term page, is there a way to update the views generated content by passing it some parameters, like for instance a new taxonomy term.

The purpose of this would be to update a view page content via an ajax request.

Thanks

2 Answers 2

4

You can easily execute and render a view with arguments in a custom module.

I would:

1 - Set up some JS code to trigger an AJAX call to the server

2 - Use hook_menu() to define a custom menu callback on a specific URL, ie: /views/ajax/view_name/view_display/view_arguments

3 - Create your callback function()

4 - Load and render the view with arguments: (this is From Views 2 API):

$view = views_get_view('foobar');  // get the view named `foobar`
$view->set_display('page_1');  // Set the view display you want to use
$view->set_arguments(array($foo));  // Pass an array containing one or several variables you want to use as arguments in the view
$result = $view->render(); // Returns HTML 

5 - Return your result in JSON (practical sometime if you want to pass more information than the view itself). You can use drupal_json_output() in D7.

0
0

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

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.