How to prevent scrolling when updating a repeating panel

That almost worked out of the box.
I modified it a little because my scrollbar is not on the main window and because I want to define the decorator in a Globals module.

It doesn’t feel right to fix the unintentional scroll by scrolling back rather than by removing the unintentional scroll, but it works.

In the Globals module:

def maintain_scroll_position(selector):
  def wrap(func):
    def wrapped_func(*args, **kwargs):
      anvil.get_open_form().call_js('getScrollPosition', selector)
      res = func(*args, **kwargs)
      anvil.get_open_form().call_js('restoreScrollPosition', selector)
    return wrapped_func
  return wrap

In the form:

from .. import Globals

[...]

  @Globals.maintain_scroll_position('.content')
  def item_facility_click(self, sender, **event_args):

In Native Libraries:

<script>
var lastScrollTop = {};
function getScrollPosition(selector) {
  lastScrollTop[selector] = $(selector).scrollTop();
}
function restoreScrollPosition(selector) {
  $(selector).scrollTop(lastScrollTop[selector]);
}
</script>
3 Likes