3

I'm trying to render a view using Ajax, but I can't get the ajax pagination to work. I've found the following code which works, apart from the pagination:

            $.ajax({
                url: '/views/ajax',
                type: 'post',
                data: {
                    view_name: 'stores',
                    view_display_id: 'default',
                    view_args: {},
                },
                dataType: 'json',
                success: function (response) {
                    if (response[1] !== undefined) {
                        $('.store-locator-view-wrap').html(response[1].data);
                        Drupal.attachBehaviors(); // Thought this would do it, but nope
                    }
                }
            });

The links go to /views/ajax?page=1 with the following json:

[{"command":"settings","settings":{"basePath":"\/","pathPrefix":"","ajaxPageState":{"theme":"rhm_starter","theme_token":"dRce3KwWhWs1ZpnNe6KVUu31mLX8pdVoZ5XixJo8SVI"}},"merge":true}]
::1
1
  • Trying to do the exact same thing and running into the same problem. Noticed that my Drupal.settings does not contain a views key. Have not discovered anything more past that.
    – jhoffmcd
    Commented Aug 29, 2017 at 6:20

1 Answer 1

3

The problem here is that the Drupal.settings does not have the proper views key it needs to attach the behaviors if you are loading a view via views/ajax.

There is no good documentation on this, spend alot of time trying to figure it out, but this ended up working. I had to set the views key in the settings manually:

success: function (response) {
    if (response[1] !== undefined) {
        $('.store-locator-view-wrap').html(response[1].data);

        // Set views key based on response data. 
        Drupal.settings.views = response[0].settings.views;
        Drupal.attachBehaviors();
    }
}

I also had to make sure the Views js files had been added to my page:

$views_path = drupal_get_path('module', 'views');
drupal_add_js(
    $views_path . '/js/base.js',
    array(
        'type' => 'file',
        'scope' => 'footer',
        'group' => JS_DEFAULT,
    )
);
drupal_add_js(
    $views_path . '/js/ajax_view.js',
    array(
        'type' => 'file',
        'scope' => 'footer',
        'group' => JS_DEFAULT,
    )
);
10
  • Looks good, thanks for the response! I could never get it to work this way so I built it a different way.
    – Chris
    Commented Aug 30, 2017 at 6:43
  • Even better, you could probably just do Drupal.settings = response[0].settings;
    – jhoffmcd
    Commented Aug 31, 2017 at 16:12
  • Actually I'm just trying this now, and it's not working for me. response[0].settings.views returns undefined and if I try Drupal.settings = response[0].settings I just get the same response as it my original question. Any ideas?
    – Chris
    Commented Sep 5, 2017 at 2:00
  • Actually forget that, it works! I had disabled ajax in the view :)
    – Chris
    Commented Sep 5, 2017 at 2:07
  • 1
    I figured it out myself in the end, just need to add it to the front of the ajax call, only if it's not the default language like: url: '/' + lang + '/views/ajax',
    – Chris
    Commented Sep 19, 2017 at 7:09

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