0

In Drupal 7, I load an AJAX View (in response to user clicking a link) with the following code (source: How to load and display a view with Ajax in Drupal 7?)

 jQuery.ajax({
    url: Drupal.settings.basePath + "/views/ajax",
    type: "post",
    data: {
    view_name: "books",
    view_display_id: "page", //your display id
    view_args: identifier, // your views arguments
  },
    dataType: "json",
    success: function (response) {
    if (response[1] !== undefined) {
      jQuery("#bloque-" + identifier).html(response[1].data);
    }
   }
 });

However, I cannot combine AJAX Views with Infinite scroll module, and even a basic pagination does not work, as it does not override the pager link code.

1 Answer 1

1

You can try this -

On your page template where you want to load this view use following sample code (with your view specific values such as arguments etc.) which will add all the required JS to your page -

$view = views_get_view('books');
$view->set_display('page');
$view->set_use_ajax(TRUE);
$view->set_arguments(array($tid));
$settings = array(
    'views' => array(
        'ajax_path' => url('views/ajax'),
        'ajaxViews' => array(
            array(
                'view_name' => $view->name,
                'view_display_id' => $view->current_display,
                'view_args' => check_plain(implode('/', $view->args)),
                'view_path' => check_plain($_GET['q']),
                'view_base_path' => $view->get_path(),
                'view_dom_id' => 1,
                'pager_element' => $view->pager['element'],
            ),
        ),
    ),
);
drupal_add_js($settings, 'setting');
views_add_js('ajax_view');

In your JavaScript code use Drupal.attachBehaviors() which will attach Infinite Scroll to your View's HTML -

jQuery.ajax({
    url: Drupal.settings.basePath + "/views/ajax",
    type: "post",
    data: {
        view_name: "books",
        view_display_id: "page", //your display id
        view_args: identifier, // your views arguments
    },
    dataType: "json",
    success: function (response) {
        if (response[1] !== undefined) {
            jQuery("#bloque-" + identifier).html(response[1].data);
        }
        Drupal.attachBehaviors();
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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