@@ -176,4 +176,111 @@ describe('ui.grid.pagination uiGridPaginationService', function () {
176
176
} ) ;
177
177
} ) ;
178
178
} ) ;
179
+
180
+ describe ( 'custom pagination' , function ( ) {
181
+
182
+ var pages = [ 'COSU' , 'DJLPQTVX' , 'ABFGHIKNRY' , 'EMWZ' ] ;
183
+
184
+ function getPage ( data , pageNumber ) {
185
+ return data . filter ( function ( datum ) {
186
+ return pages [ pageNumber - 1 ] . indexOf ( datum . col2 ) >= 0 ;
187
+ } ) ;
188
+ }
189
+
190
+ beforeEach ( inject ( function ( _$rootScope_ , _$timeout_ , $compile ) {
191
+ $rootScope = _$rootScope_ ;
192
+ $timeout = _$timeout_ ;
193
+
194
+ $rootScope . gridOptions . useCustomPagination = true ;
195
+ $rootScope . gridOptions . useExternalPagination = true ;
196
+ $rootScope . gridOptions . paginationPageSizes = [ 4 , 8 , 10 , 4 ] ;
197
+
198
+ var data = $rootScope . gridOptions . data ;
199
+ $rootScope . gridOptions . data = getPage ( data , 1 ) ;
200
+ gridApi . pagination . on . paginationChanged ( $rootScope , function ( pageNumber ) {
201
+ $rootScope . gridOptions . data = getPage ( data , pageNumber ) ;
202
+ } ) ;
203
+
204
+ $rootScope . $digest ( ) ;
205
+ } ) ) ;
206
+
207
+ it ( 'starts at page 1 with 4 records' , function ( ) {
208
+ var gridRows = gridElement . find ( 'div.ui-grid-row' ) ;
209
+
210
+ expect ( gridApi . pagination . getPage ( ) ) . toBe ( 1 ) ;
211
+ expect ( gridRows . length ) . toBe ( 4 ) ;
212
+
213
+ var firstCell = gridRows . eq ( 0 ) . find ( 'div.ui-grid-cell:first-child' ) ;
214
+ expect ( firstCell . text ( ) ) . toBe ( '6_1' ) ;
215
+
216
+ var lastCell = gridRows . eq ( 3 ) . find ( 'div.ui-grid-cell:last-child' ) ;
217
+ expect ( lastCell . text ( ) ) . toBe ( '25_4' ) ;
218
+ } ) ;
219
+
220
+ it ( 'calculates the total number of pages correctly' , function ( ) {
221
+ expect ( gridApi . pagination . getTotalPages ( ) ) . toBe ( 4 ) ;
222
+ } ) ;
223
+
224
+ it ( 'displays page 2 if I call nextPage()' , function ( ) {
225
+ gridApi . pagination . nextPage ( ) ;
226
+ $rootScope . $digest ( ) ;
227
+ $timeout . flush ( ) ;
228
+
229
+ var gridRows = gridElement . find ( 'div.ui-grid-row' ) ;
230
+
231
+ expect ( gridApi . pagination . getPage ( ) ) . toBe ( 2 ) ;
232
+ expect ( gridRows . length ) . toBe ( 8 ) ;
233
+
234
+ var firstCell = gridRows . eq ( 0 ) . find ( 'div.ui-grid-cell:first-child' ) ;
235
+ expect ( firstCell . text ( ) ) . toBe ( '4_1' ) ;
236
+
237
+ var lastCell = gridRows . eq ( 7 ) . find ( 'div.ui-grid-cell:last-child' ) ;
238
+ expect ( lastCell . text ( ) ) . toBe ( '21_4' ) ;
239
+ } ) ;
240
+
241
+ it ( 'displays 10 rows on page 3' , function ( ) {
242
+ gridApi . pagination . seek ( 3 ) ;
243
+ $rootScope . $digest ( ) ;
244
+ $timeout . flush ( ) ;
245
+
246
+ var gridRows = gridElement . find ( 'div.ui-grid-row' ) ;
247
+
248
+ expect ( gridApi . pagination . getPage ( ) ) . toBe ( 3 ) ;
249
+ expect ( gridRows . length ) . toBe ( 10 ) ;
250
+
251
+ var firstCell = gridRows . eq ( 0 ) . find ( 'div.ui-grid-cell:first-child' ) ;
252
+ expect ( firstCell . text ( ) ) . toBe ( '1_1' ) ;
253
+
254
+ var lastCell = gridRows . eq ( 9 ) . find ( 'div.ui-grid-cell:last-child' ) ;
255
+ expect ( lastCell . text ( ) ) . toBe ( '26_4' ) ;
256
+ } ) ;
257
+
258
+ it ( 'paginates correctly on a sorted grid' , function ( ) {
259
+ gridApi . grid . sortColumn ( gridApi . grid . columns [ 1 ] ) . then ( function ( ) {
260
+ gridApi . pagination . seek ( 3 ) ;
261
+ $rootScope . $digest ( ) ;
262
+ $timeout . flush ( ) ;
263
+
264
+ var gridRows = gridElement . find ( 'div.ui-grid-row' ) ;
265
+ expect ( gridApi . pagination . getPage ( ) ) . toBe ( 1 ) ;
266
+ expect ( gridRows . eq ( 0 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'A' ) ;
267
+ expect ( gridRows . eq ( 1 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'B' ) ;
268
+ expect ( gridRows . eq ( 2 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'F' ) ;
269
+ expect ( gridRows . eq ( 3 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'G' ) ;
270
+ expect ( gridRows . eq ( 4 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'H' ) ;
271
+ expect ( gridRows . eq ( 5 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'I' ) ;
272
+ expect ( gridRows . eq ( 6 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'K' ) ;
273
+ expect ( gridRows . eq ( 7 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'N' ) ;
274
+ expect ( gridRows . eq ( 8 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'R' ) ;
275
+ expect ( gridRows . eq ( 9 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'Y' ) ;
276
+
277
+ gridApi . pagination . nextPage ( ) ;
278
+ $rootScope . $digest ( ) ;
279
+
280
+ gridRows = gridElement . find ( 'div.ui-grid-row' ) ;
281
+ expect ( gridApi . pagination . getPage ( ) ) . toBe ( 2 ) ;
282
+ expect ( gridRows . eq ( 0 ) . find ( 'div.ui-grid-cell' ) . eq ( 1 ) . text ( ) ) . toBe ( 'E' ) ;
283
+ } ) ;
284
+ } ) ;
285
+ } ) ;
179
286
} ) ;
0 commit comments