I would like to load a block from views2 into a page via ajax. The block has many pictures and this slows down the site. I would like the page to load and when it is finished, load this block at the very end.

My line of thinking is that I should make an ajax call to a kind of template file that graps the right view from the database. I have no idea how to do this without loading the headers and all the rest I don't need.

The most dirty way I can imagine is to just load an entire new page with just that block in it (mysite.com/my/block/view) and extract the html I need with javascript. this would mean loading a lot of css and js and html for nothing.

Comments

dawehner’s picture

i'm not sure whether views provides something but basically

1. define menu callback
2. do a menu callback like this

<?php
// @TODO: get this via menu arguments
$view = 'yourviewname';
$display = 'displayname';
$output = views_embed_view($view, $display);
print drupal_json(array('return' => $output));
exit();
?>

and get this with your js and print it anywhere. its returns a json array

PS: there seems to be a views_ajax menu callback, but i cannot figure how to use $_REQUEST

vacilando’s picture

Issue tags: +block, +VIEWS2, +Ajax, +load

I also would like to achieve AJAX loading of a views2 block. Subscribing.

dawehner’s picture

did you tested my code?

dkruglyak’s picture

@dereine: Could you package your code in a way that is easy to apply? Perhaps make a patch?

Would be very valuable to make this available at the block level, so that it would work outside of the Views module too.

mths’s picture

Sorry for being slow to react, I had some other stuff on my mind.
I didn't test the code. I wouldn't know where to put it and how to call it. I know to little about Drupal.

esmerel’s picture

Status: Active » Closed (fixed)

This issue is a year old, it's likely either resolved or no longer relevant.

vacilando’s picture

@esmerel, if you know there is a solution, please point to it. If not, you can ask people in the thread whether they consider it irrelevant.

Such threads are very useful as meeting points for people who look for answers to particular questions.

Thanks for your consideration.

merlinofchaos’s picture

Old threads that remain open are generally bad for the queue, unfortunately. esmerel's effort is to try and trim the Views' queue down enough that the maintainers can remain effective. For posts that are more discussion than anything, there's always the forum.

BTW, Views has an AJAX method. The appropriate thing to do is to turn on AJAX, take a look at what it does when it's enabling AJAX on a view (for example, with the pager) and then try to construct something that does the same thing. This thread doesn't look like it's helping anybody doing that. Other threads may have discussed doing this, I think.

maximpodorov’s picture

You can use Ajax Blocks module.