2

I worked in printing out a view using php. Is it possible to fetch views using ajax and load in the page? For suppose I have three links each one will fetch a view using ajax and load in the current page. (Am I talking sense?)

2 Answers 2

3

Yes it is very much possible, but probably you can do it only by creating custom module.

  • send Ajax request to the Drupal
  • load selected view form database by views_get_view
  • execute selected display by $view->execute_display('display')
  • send HTML created on executing display back to the client.
  • on client side put this html wherever you want

Check ajax load module (which is D6 only) too, maybe it can help you.

There is a even quicker way and more robust to render a view: views_embed_view

2
  • I'm new to drupal 7, how do I can send ajax request to the Drupal? where must be the javascript file? and where musst be the server side code?
    – M a m a D
    Jan 25, 2014 at 16:00
  • @Mohammad use google Luke ;)
    – Codium
    Jan 25, 2014 at 18:55
2

Use the following javascript code to load a view via ajax.

$.post(
Drupal.settings.basePath + 'views/ajax',
{

view_name: '<your view id>',view_display_id: '<page or block ...>',view_args: 'arg1/arg2..'
},

function(response)
{
    if (response[1] !== undefined) 
    {
        var viewHtml = response[1].data;
        $("#user_gal_photos").html(viewHtml);
       /*...*/
        Drupal.attachBehaviors();
    }
});
1
  • This worked for me, however be careful with the Drupal.attachBehaviors() call at the end. It caused some problems for me. I removed it as it's not needed for my simple non-paged view.
    – kmcl
    Oct 24, 2014 at 21:29

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.