DEV Community

Maik Michel
Maik Michel

Posted on • Originally published at micodify.de on

Scroll to top after pagination

In this article you will learn how to jump back to the beginning of the report after a new rowset / pagination event occurred.

In one of my projects there is a ClassicReport with many rows. Each of these rows contains a button that opens the respective record via modal dialog. Here the customer can edit this record and close this dialog again. So far so good, nothing special. Unfortunately, the customer will work through data cyclically by means of that report, so he will change or search those records one after the other and will switch to the next page by using pagination at the end of that page. At this moment, APEX will show correct data, i.e. will refresh that report, but unfortunately will remain at same scroll position of browser window. So, user will have to scroll up again manually.

So, in order to scroll upwards again during pagination, we highjack APEX's own method for page navigation. And overwrite it with our own logic, which then scrolls upwards after the navigation.

For this we define some variables on the page itself under "Function and Global Variable Declaration".

var originalPaginationFunction;
var ocPaginationID, ocPaginationCheckSum, ocPaginationOptions;
var ocPaginationStore = false;
Enter fullscreen mode Exit fullscreen mode

In the "Execute when Page Loads" section, we place the actual solution, that is, the actual high-jacking of the function:

// store original function
originalPaginationFunction = apex.widget.report.paginate;

apex.widget.report.paginate = function(pID, pCheckSum, pOptions, pScollTop=true) {
  // store originalValues
  ocPaginationID = pID;
  ocPaginationCheckSum = pCheckSum;
  ocPaginationOptions = pOptions;
  ocPaginationStore = true;

  // call original function
  originalPaginationFunction(ocPaginationID, ocPaginationCheckSum, JSON.parse(JSON.stringify(ocPaginationOptions)));

  if (pScollTop) {
    $('html, body').animate({scrollTop: 0}, 400);
  }
}
Enter fullscreen mode Exit fullscreen mode

That's it, actually.

By overriding this function, the pagination is discarded on refresh when closing the ModalDialog. To solve this problem, we additionally define the function refreshAndKeepPagination. (under "Function and Global Variable Declaration")

function refreshAndKeepPagination() {
  apex.widget.report.paginate(ocPaginationID, ocPaginationCheckSum, ocPaginationOptions, false);
}
Enter fullscreen mode Exit fullscreen mode

This will be called on the OnClose event whenever the ClientSideCondition: ocPaginationStore === true is met. Otherwise we call here the original Refresh per DynamikAction.

An idea for improvement would be to keep the scroll position when navigating back.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay