I'm currently trying to make a view, which contains images that people can insert into the body of their posts, work with the ajax pagination from the views module, but the tricky part is: the view itself is loaded in using ajax.

The Images view is loaded using jQuery's .load function, and is situated on a page (i.e. /image-gallery-view), but the .load function passes a parameter that just returns the bare content/html without styles or scripts for the purposes of AJAX. When you load this page in your browser, with styles and scripts included, pagination works perfectly.

I have replicated all the jQuery.extend behaviors that this page has in it's head section, and also all the scripts it calls, but the pagination still doesn't work, and the JS console throws no errors.

So in summary:

  • Replicated all possible JS files and behaviors from the page where it works.
  • Re-instantited/applied behaviors using Drupal.attachBehaviors(document):

I just don't have a clue. Anyone have any ideas? i need a fresh perspective.

Comments

redsd’s picture

I encountered the same issue, where I loaded views with ajax, these views contain ajax pagers.
I'm using drupal 7 with the latest views 3.

I fixed the issue by adding the javascript as drupal compiled it, with the view I loaded using ajax I combined the html output of the view and the js from drupal. this way everytime a view got added with ajax it also added the needed js to make sure the pager would work aswell

sample of my code:

  $html = my_function_to_build_and_load_view($arg);
  $html .= drupal_get_js();
  
  echo $html;

The first function will call the view for example:

function my_function_to_build_and_load_view($arg) {
$number = 10;
  $view = views_get_view($args['viewname']);
  $view->set_arguments( $query );
  $view->set_display($args['viewid']);
  $view->set_items_per_page($number);
  //$view->set_use_ajax(TRUE);
  //$view->display_handler->set_option('items_per_page', 1);
  $view->execute();
  $count = count( $view->result );
  $render = $view->preview($args['viewid'], $query);

  return $render;
}

the second function will add the needed javascript that will be included by ajax to the active page.

wMarius’s picture

Best answer , was looking for this problem solution, tried a lot of patch for view but nothing. Thanks again! :)

redsd’s picture

Your welcome, even after 2 years :P