|
| 1 | +@ngdoc overview |
| 2 | +@name Tutorial: 406 Custom Pagination |
| 3 | +@description |
| 4 | + |
| 5 | +When pagination is enabled, the data is displayed in pages that can be browsed using the built in |
| 6 | +pagination selector. However, you don't always have pages with the same number of rows. |
| 7 | + |
| 8 | +For custom pagination, set the `pageSizes` option and the `useCustomPagination`. |
| 9 | +~~ and implement the `gridApi.pagination.on.paginationChanged` callback function. The callback |
| 10 | +may contain code to update any pagination state variables your application may utilize, e.g. variables containing |
| 11 | +the `pageNumber`, `pageSize`, and `pageSizeList`. The REST call used to fetch the data from the server should be |
| 12 | +called from within this callback. The URL of the call should contain query parameters that will allow the server-side |
| 13 | +code to have sufficient information to be able to retrieve the specific subset of data that the client requires from |
| 14 | +the entire set.~~ |
| 15 | + |
| 16 | +It should also update the `$scope.gridOptions.totalItems` variable with the total count of rows that exist (but |
| 17 | +were not all fetched in the REST call mentioned above since they exist in other pages of data). |
| 18 | + |
| 19 | +This will allow ui-grid to calculate the correct number of pages on the client-side. |
| 20 | + |
| 21 | +@example |
| 22 | +This shows custom pagination. |
| 23 | +<example module="app"> |
| 24 | + <file name="app.js"> |
| 25 | + var app = angular.module('app', ['ngTouch', 'ui.grid', 'ui.grid.pagination']); |
| 26 | + |
| 27 | + app.controller('MainCtrl', [ |
| 28 | + '$scope', '$http', 'uiGridConstants', function($scope, $http, uiGridConstants) { |
| 29 | + |
| 30 | + $scope.gridOptions1 = { |
| 31 | + paginationPageSizes: null, |
| 32 | + useCustomPagination: true, |
| 33 | + columnDefs: [ |
| 34 | + { name: 'name', enableSorting: false }, |
| 35 | + { name: 'gender', enableSorting: false }, |
| 36 | + { name: 'company', enableSorting: false } |
| 37 | + ] |
| 38 | + }; |
| 39 | + |
| 40 | + $scope.gridOptions2 = { |
| 41 | + paginationPageSizes: null, |
| 42 | + useCustomPagination: true, |
| 43 | + useExternalPagination : true, |
| 44 | + columnDefs: [ |
| 45 | + { name: 'name', enableSorting: false }, |
| 46 | + { name: 'gender', enableSorting: false }, |
| 47 | + { name: 'company', enableSorting: false } |
| 48 | + ], |
| 49 | + onRegisterApi: function(gridApi) { |
| 50 | + gridApi.pagination.on.paginationChanged($scope, function (pageNumber, pageSize) { |
| 51 | + $scope.gridOptions2.data = getPage($scope.grid2data, pageNumber); |
| 52 | + }); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + $http.get('/data/100_ASC.json') |
| 57 | + .success(function (data) { |
| 58 | + $scope.gridOptions1.data = data; |
| 59 | + $scope.gridOptions1.paginationPageSizes = calculatePageSizes(data); |
| 60 | + }); |
| 61 | + |
| 62 | + $http.get('/data/100.json') |
| 63 | + .success(function (data) { |
| 64 | + $scope.grid2data = data; |
| 65 | + $scope.gridOptions2.totalItems = 0;//data.length; |
| 66 | + $scope.gridOptions2.paginationPageSizes = calculatePageSizes(data); |
| 67 | + $scope.gridOptions2.data = getPage($scope.grid2data, 1); |
| 68 | + }); |
| 69 | + |
| 70 | + |
| 71 | + function calculatePageSizes(data) { |
| 72 | + var initials = []; |
| 73 | + return data.reduce(function(pageSizes, row) { |
| 74 | + var initial = row.name.charAt(0); |
| 75 | + var index = initials.indexOf(initial); |
| 76 | + if(index < 0) |
| 77 | + { |
| 78 | + index = initials.length; |
| 79 | + initials.push(initial); |
| 80 | + } |
| 81 | + pageSizes[index] = (pageSizes[index] || 0) + 1; |
| 82 | + return pageSizes; |
| 83 | + }, []); |
| 84 | + } |
| 85 | + |
| 86 | + function getPage(data, pageNumber) |
| 87 | + { |
| 88 | + var initials = []; |
| 89 | + return data.reduce(function(pages, row) { |
| 90 | + var initial = row.name.charAt(0); |
| 91 | + |
| 92 | + if(!pages[initial]) pages[initial] = []; |
| 93 | + pages[initial].push(row); |
| 94 | + |
| 95 | + if(initials.indexOf(initial) < 0) |
| 96 | + { |
| 97 | + initials.push(initial); |
| 98 | + initials.sort(); |
| 99 | + } |
| 100 | + |
| 101 | + return pages; |
| 102 | + |
| 103 | + }, {})[initials[pageNumber - 1]] || []; |
| 104 | + } |
| 105 | + |
| 106 | + } |
| 107 | + ]); |
| 108 | + </file> |
| 109 | + <file name="index.html"> |
| 110 | + <div ng-controller="MainCtrl"> |
| 111 | + <div ui-grid="gridOptions1" ui-grid-pagination class="grid"></div> |
| 112 | + <div ui-grid="gridOptions2" ui-grid-pagination class="grid"></div> |
| 113 | + </div> |
| 114 | + </file> |
| 115 | + <file name="main.css"> |
| 116 | + .grid { |
| 117 | + width: 600px; |
| 118 | + } |
| 119 | + </file> |
| 120 | +</example> |
0 commit comments