Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When a view is embedded inside of another view and both have ajax driven pagination, the scroll-to-top function always scrolls to the top of the parent view instead of the view directly related to the pager element. This has to do with a malformed selector in ajax_view.js
I'm going to attempt to write a patch for it, it'll be my first one O_O wish me luck!
Original code for ajax_views.js lines 113-161
$(this).click(function () {
$.extend(viewData, Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path));
$(this).addClass('views-throbbing');
$.ajax({
url: ajax_path,
type: 'GET',
data: viewData,
success: function(response) {
$(this).removeClass('views-throbbing');
// Scroll to the top of the view. This will allow users
// to browse newly loaded content after e.g. clicking a pager
// link.
var offset = $(target).offset();
// We can't guarantee that the scrollable object should be
// the body, as the view could be embedded in something
// more complex such as a modal popup. Recurse up the DOM
// and scroll the first element that has a non-zero top.
var scrollTarget = target;
while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
scrollTarget = $(scrollTarget).parent()
}
// Only scroll upward
if (offset.top - 10 < $(scrollTarget).scrollTop()) {
$(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
}
// Call all callbacks.
if (response.__callbacks) {
$.each(response.__callbacks, function(i, callback) {
eval(callback)(target, response);
});
}
Modified Version
$(this).click(function () {
parent_view = $(this).parents(".view");
$.extend(viewData, Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path));
$(this).addClass('views-throbbing');
$.ajax({
url: ajax_path,
type: 'GET',
data: viewData,
success: function(response) {
$(this).removeClass('views-throbbing');
// Scroll to the top of the view. This will allow users
// to browse newly loaded content after e.g. clicking a pager
// link.
var offset = parent_view.offset();
// We can't guarantee that the scrollable object should be
// the body, as the view could be embedded in something
// more complex such as a modal popup. Recurse up the DOM
// and scroll the first element that has a non-zero top.
var scrollTarget = parent_view;
while ($(scrollTarget).scrollTop() == 0 && $(scrollTarget).parent()) {
scrollTarget = $(scrollTarget).parent()
}
// Only scroll upward
if (offset.top - 10 < $(scrollTarget).scrollTop()) {
$(scrollTarget).animate({scrollTop: (offset.top - 10)}, 500);
}
// Call all callbacks.
if (response.__callbacks) {
$.each(response.__callbacks, function(i, callback) {
eval(callback)(target, response);
});
}
Comment | File | Size | Author |
---|---|---|---|
#1 | views_ajax_pager-1452400-0.patch | 1.59 KB | terbs |
Comments
Comment #1
terbs CreditAttribution: terbs commentedHere's the attached patch file yay!
Comment #2
MustangGB CreditAttribution: MustangGB commented