Matteo Scandolo | 8b2370c | 2017-02-02 17:19:07 -0800 | [diff] [blame] | 1 | class XosPaginationCtrl { |
| 2 | $inject = ['$onInit', '$scope']; |
| 3 | |
| 4 | public pageSize: number; |
| 5 | public totalElements: number; |
| 6 | public change: any; // fn |
| 7 | public currentPage: number; |
| 8 | public pages: number; |
| 9 | public pageList: number[]; |
| 10 | |
| 11 | constructor ( |
| 12 | private $scope: ng.IScope |
| 13 | ) { |
| 14 | } |
| 15 | |
| 16 | $onInit() { |
| 17 | this.currentPage = 0; |
| 18 | |
| 19 | // watch for data changes |
| 20 | this.$scope.$watch(() => this.totalElements, () => { |
| 21 | if (this.totalElements) { |
| 22 | this.pages = Math.ceil(this.totalElements / this.pageSize); |
| 23 | this.pageList = this.createPages(this.pages); |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | public goToPage = (n) => { |
| 29 | if (n < 0 || n === this.pages) { |
| 30 | return; |
| 31 | } |
| 32 | this.currentPage = n; |
| 33 | this.change(n); |
| 34 | }; |
| 35 | |
| 36 | private createPages = (pages) => { |
| 37 | let arr = []; |
| 38 | for (let i = 0; i < pages; i++) { |
| 39 | arr.push(i); |
| 40 | } |
| 41 | return arr; |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | export const xosPagination: angular.IComponentOptions = { |
| 46 | template: require('./pagination.html'), |
| 47 | controllerAs: 'vm', |
| 48 | controller: XosPaginationCtrl, |
| 49 | bindings: { |
| 50 | pageSize: '=', |
| 51 | totalElements: '=', |
| 52 | change: '=' |
| 53 | } |
| 54 | }; |