0

when a user submit the view's exposed form the site is reloaded and the page's focus is not on the view's results but on the very top of the page. I use quite a few exposed filters above a table view. Locating the filters somewhere else is not possible (e.g. sidebar). I am trying to solve this using JS. My attempts:

(function ($) {

  'use strict';

  $(document).ready(function() {
    if( $('.views-table').length ){
      $("html, body").animate({ scrollTop: $(".views-table").offset().top }, "slow");        
    }
  });

}(jQuery));

This works when I apply a filter but it also works when I used load the view page without filters applied. Unfortunately, I don't have any style classes that are only applied to the filtered view table.

(function ($) {

Drupal.behaviors.form_submit_processor = {
  attach: function (context, settings) {
    $("form#views-exposed-form-standortanalyse-page-1").submit(function(e) {
      $("html, body").animate({ scrollTop: $(".views-table").offset().top }, "slow");  
    });
  }
}

}(jQuery));

When using this approach the scrolling works when hitting the submit button but after page reload the scroll setting is lost again.

Does someone has any idea how this can be achieved?

6
  • 1
    As an alternative, have you considered turning on the ajax setting within your views for dynamic changing of the results? Nov 28, 2022 at 16:26
  • I agree - Ajax would solve this.
    – cilefen
    Nov 28, 2022 at 16:49
  • This seems to be the same question as drupal.stackexchange.com/questions/260697/…
    – cilefen
    Nov 28, 2022 at 16:51
  • Turning on Ajax in the view's setting doesn't lead to the expected behavior. When a filter is being applied the focus doesn't automatically scroll to the results table.
    – ThuleNB
    Nov 29, 2022 at 7:29
  • @cilefen: Yes, I already saw this question. The first code in my question is from the post you mentioned. Unfortunately, there's no solution for my topic. The problem with this code - as mentioned in my question - is that it's also applied when initially opening the view page, not only when filters are applied.
    – ThuleNB
    Nov 29, 2022 at 7:32

1 Answer 1

0

Putting this here in case anyone runs into this issue in the future — you can disable the autoscroll functionality by placing this in your code to override:

Drupal.AjaxCommands.prototype.viewsScrollTop = null;

Your Answer

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

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