0

I have this view, a 'block' display that has ajax enabled and have a mini pager that loads next page content through ajax.

As a requirement I have to reload the current view content through ajax along with some custom sort/filter.

After going through some forums, I did something like below.

in .module page callback :

$view = views_get_view('vdlibrary');
$view->set_display('block');
$view->set_use_ajax(TRUE);

$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 JS I am calling below ajax function on a click event to load the new views content based on the condition.

for (var key in Drupal.settings.views.ajaxViews[0]) {
  data[key] = Drupal.settings.views.ajaxViews[0][key];
}
// I add additional filters/sort params to var data.
$.ajax({
  url: Drupal.settings.views.ajax_path,
  type: 'GET',
  data: data,
  dataType: 'json',
  success: function(response) {

    $(".vdr_container").html(response[1].data);

  },
  error: function() {
    // handle error response.
  },
});

Not surprisingly, after it loads the content, the ajax pagination feature not working instead it reloads the page along with params. How do I keep the old context or make the new loaded views to rebind pager click event to work with ajax pager.

NOTE: my descriptions might not be good enough, let me know where I could explain in better way.

Thanks

1 Answer 1

0

After going through ajax_views.js and understanding view-dom-id element structure, finally I could able to get this done.

In ajax response object, there are 3 callbacks, In the last 'insert' callback, I got data with command to 'replacewith' and a DOM selector (unique dom for views wrapper with '.view-dom-id-xxxxx'). So I did replace the new content like :

$(".view-dom-id-" + vDomId).replaceWith(response[2].data);

Then I got the content with existing context, all I had to do then is re-attach the 'ViewsAjaxView' behavior as found in ajax_view.js

Drupal.attachBehaviors('ViewsAjaxView'); 

This attachBehaviors function will take care of binding ajax pagination to my newly altered views data.

I still feel, as if this can be more simpler like running all callbacks I get in response variable in success. I will post more if I get into. Hopefully someone gets some help from this.

Anybody have idea to run those commands?

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.