Score:0

Scroll to view's result after applying exposed filter

ca flag

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?

unusedspoon avatar
aq flag
As an alternative, have you considered turning on the ajax setting within your views for dynamic changing of the results?
id flag
I agree - Ajax would solve this.
id flag
This seems to be the same question as https://drupal.stackexchange.com/questions/260697/scroll-to-search-results-on-views-exposed-filter-submit-button?rq=1
ThuleNB avatar
ca flag
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 avatar
ca flag
@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 avatar
ca flag
Does someone have another idea how this could be solved?
Score:0
ki flag

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;
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.