lorenzofox3 / Smart-Table

Code source of Smart Table module: a table/grid for Angularjs
http://lorenzofox3.github.io/smart-table-website/
1.8k stars 513 forks source link

scroll retains same position using custom pagination #824

Closed zhyuxicc closed 6 years ago

zhyuxicc commented 6 years ago

Hi,

I create my pagination following your custom pagination example on the bottom of the documentation page. I have a small question about the scroll. When I jump to other page, the scroll always remain at the same position. For example, I drag the scroll to the bottom on page 1, then i jump to page 2, the scroll retains it's position and show the bottom of my table. My problem is how to make scroll always got to top of the table when jumping to another page. It may be silly question(I'm new to angular, new to javascript), but I really couldn't find a solution to it. Thanks!

ghost commented 6 years ago

I encountered this yesterday, the fix is to add: $location + $anchorScroll to allow scroll to top.

//view -> table state
        scope.selectPage = function (page) {
          if (page > 0 && page <= scope.numPages) {
            ctrl.slice((page - 1) * scope.stItemsByPage, scope.stItemsByPage);
          }
            $location.hash('top'); // add this 
            $anchorScroll(); // and add this 
        };
        if (!ctrl.tableState().pagination.number) {
          ctrl.slice(0, scope.stItemsByPage);
          $location.hash('top'); // add this 
          $anchorScroll(); // and add this
        }
      }

Remember to add services to the Directive:

.directive('stPagination', ['stConfig', '$location', '$anchorScroll', function (stConfig, $location, $anchorScroll) {

Then in your template add: id="top"