@@ -26,6 +26,16 @@ var PivotView = function (controller, container) {
26
26
messageElement : undefined
27
27
} ;
28
28
29
+ /**
30
+ * Pagination object.
31
+ * @see pushTable
32
+ * @type {{on: boolean, page: number, pages: number, rows: number} }
33
+ */
34
+ this . pagination = null ;
35
+
36
+ this . PAGINATION_BLOCK_HEIGHT = 20 ;
37
+ this . ANIMATION_TIMEOUT = 500 ;
38
+
29
39
this . controller = controller ;
30
40
31
41
this . SCROLLBAR_WIDTH = ( function ( ) {
@@ -116,18 +126,27 @@ PivotView.prototype.getCurrentTableData = function () {
116
126
PivotView . prototype . pushTable = function ( opts ) {
117
127
118
128
var _ = this ,
129
+ pg ,
119
130
tableElement = document . createElement ( "div" ) ;
120
131
121
132
tableElement . className = "tableContainer" ;
122
133
if ( this . tablesStack . length ) tableElement . style . left = "100%" ;
123
134
124
135
this . tablesStack . push ( {
125
136
element : tableElement ,
126
- opts : opts || { }
137
+ opts : opts || { } ,
138
+ pagination : pg = { // defaults copied to pushTable
139
+ on : false ,
140
+ rows : Infinity , // rows by page including (headers + summary + rows from config)
141
+ page : 0 , // current page,
142
+ pages : 0 // available pages
143
+ }
127
144
} ) ;
128
145
129
146
this . elements . base . appendChild ( tableElement ) ;
130
147
this . elements . tableContainer = tableElement ;
148
+ console . log ( "pagination changed to " , pg ) ;
149
+ this . pagination = pg ;
131
150
132
151
setTimeout ( function ( ) {
133
152
_ . _updateTablesPosition ( ) ;
@@ -141,14 +160,38 @@ PivotView.prototype.popTable = function () {
141
160
142
161
this . _updateTablesPosition ( 1 ) ;
143
162
var garbage = this . tablesStack . pop ( ) ;
163
+ this . pagination = this . tablesStack [ this . tablesStack . length - 1 ] . pagination ;
144
164
145
165
setTimeout ( function ( ) {
146
166
garbage . element . parentNode . removeChild ( garbage . element ) ;
147
- } , 500 ) ;
167
+ } , this . ANIMATION_TIMEOUT ) ;
148
168
this . elements . tableContainer = this . tablesStack [ this . tablesStack . length - 1 ] . element ;
149
169
150
170
} ;
151
171
172
+ /**
173
+ * Data change handler.
174
+ *
175
+ * @param data
176
+ */
177
+ PivotView . prototype . dataChanged = function ( data ) {
178
+
179
+ var dataRows =
180
+ data . rawData . length - data . info . topHeaderRowsNumber ; // - (data.info.SUMMARY_SHOWN ? 1 : 0);
181
+
182
+ if ( this . controller . CONFIG . pagination ) this . pagination . on = true ;
183
+ this . pagination . rows = this . controller . CONFIG . pagination || Infinity ;
184
+ //? this.controller.CONFIG.pagination
185
+ //+ data.info.topHeaderRowsNumber + (data.info.SUMMARY_SHOWN ? 1 : 0)
186
+ //: Infinity;
187
+ this . pagination . page = 0 ;
188
+ this . pagination . pages = Math . ceil ( dataRows / this . pagination . rows ) ;
189
+ if ( this . pagination . pages < 2 ) this . pagination . on = false ;
190
+
191
+ this . renderRawData ( data ) ;
192
+
193
+ } ;
194
+
152
195
PivotView . prototype . _columnClickHandler = function ( columnIndex ) {
153
196
154
197
this . controller . dataController . sortByColumn ( columnIndex ) ;
@@ -161,6 +204,13 @@ PivotView.prototype._rowClickHandler = function (rowIndex, cellData) {
161
204
162
205
} ;
163
206
207
+ PivotView . prototype . _pageSwitcherHandler = function ( pageIndex ) {
208
+
209
+ this . pagination . page = pageIndex ;
210
+ this . renderRawData ( this . controller . dataController . getData ( ) ) ;
211
+
212
+ } ;
213
+
164
214
PivotView . prototype . _backClickHandler = function ( event ) {
165
215
166
216
if ( event ) {
@@ -383,12 +433,13 @@ PivotView.prototype.recalculateSizes = function (container) {
383
433
lTableHead . removeChild ( lTableHead . lastChild ) ;
384
434
}
385
435
386
- var headerW = leftHeader . offsetWidth ,
436
+ var pagedHeight = this . pagination . on ? this . PAGINATION_BLOCK_HEIGHT : 0 ,
437
+ headerW = leftHeader . offsetWidth ,
387
438
headerH = topHeader . offsetHeight ,
388
439
containerHeight = container . offsetHeight ,
389
440
mainHeaderWidth = headerContainer . offsetWidth ,
390
- hasVerticalScrollBar = tableBlock . scrollHeight > containerHeight - headerH ,
391
- addExtraLeftHeaderCell = lTableHead . offsetHeight > containerHeight - headerH
441
+ hasVerticalScrollBar = tableBlock . scrollHeight > containerHeight - headerH - pagedHeight ,
442
+ addExtraLeftHeaderCell = lTableHead . offsetHeight > containerHeight - headerH - pagedHeight
392
443
&& this . SCROLLBAR_WIDTH > 0 ,
393
444
cell , tr , cellWidths = [ ] , columnHeights = [ ] , i ;
394
445
@@ -416,10 +467,10 @@ PivotView.prototype.recalculateSizes = function (container) {
416
467
417
468
topHeader . style . marginLeft = headerW + "px" ;
418
469
tableBlock . style . marginLeft = headerW + "px" ;
419
- leftHeader . style . height = containerHeight - headerH + "px" ;
470
+ leftHeader . style . height = containerHeight - headerH - pagedHeight + "px" ;
420
471
leftHeader . style . width = headerW + "px" ;
421
472
if ( mainHeaderWidth > headerW ) leftHeader . style . width = mainHeaderWidth + "px" ;
422
- tableBlock . style . height = containerHeight - headerH + "px" ;
473
+ tableBlock . style . height = containerHeight - headerH - pagedHeight + "px" ;
423
474
headerContainer . style . height = headerH + "px" ;
424
475
425
476
if ( addExtraLeftHeaderCell ) {
@@ -436,7 +487,7 @@ PivotView.prototype.recalculateSizes = function (container) {
436
487
leftHeader . className = leftHeader . className . replace ( / \s b o r d e r e d / , "" )
437
488
+ " bordered" ;
438
489
cell . colSpan = lTableHead . childNodes . length ;
439
- cell . style . height = this . SCROLLBAR_WIDTH + "px" ;
490
+ cell . style . height = ( this . SCROLLBAR_WIDTH ? this . SCROLLBAR_WIDTH + 1 : 0 ) + "px" ;
440
491
}
441
492
442
493
for ( i in tableTr . childNodes ) {
@@ -518,7 +569,11 @@ PivotView.prototype.renderRawData = function (data) {
518
569
LHTHead = document . createElement ( "thead" ) ,
519
570
mainTable = document . createElement ( "table" ) ,
520
571
mainTBody = document . createElement ( "tbody" ) ,
521
- x , y , tr = null , th , td , primaryColumns = [ ] , primaryRows = [ ] , ratio , cellStyle ;
572
+ pageSwitcher = this . pagination . on ? document . createElement ( "div" ) : null ,
573
+ pageNumbers = this . pagination . on ? [ ] : null ,
574
+ pageSwitcherContainer = pageSwitcher ? document . createElement ( "div" ) : null ,
575
+ i , x , y , tr = null , th , td , primaryColumns = [ ] , primaryRows = [ ] , ratio , cellStyle ,
576
+ tempI , tempJ ;
522
577
523
578
// clean previous content
524
579
this . removeMessage ( ) ;
@@ -617,13 +672,15 @@ PivotView.prototype.renderRawData = function (data) {
617
672
renderHeader (
618
673
0 ,
619
674
info . leftHeaderColumnsNumber ,
620
- info . topHeaderRowsNumber ,
621
- rawData . length ,
675
+ tempI = info . topHeaderRowsNumber + ( this . pagination . page * this . pagination . rows || 0 ) ,
676
+ tempJ = this . pagination . on
677
+ ? Math . min ( tempI + this . pagination . rows , rawData . length )
678
+ : rawData . length ,
622
679
LHTHead
623
680
) ;
624
681
625
682
// render table
626
- for ( y = info . topHeaderRowsNumber ; y < rawData . length ; y ++ ) {
683
+ for ( y = tempI /* info.topHeaderRowsNumber*/ ; y < tempJ /* rawData.length*/ ; y ++ ) {
627
684
tr = document . createElement ( "tr" ) ;
628
685
for ( x = info . leftHeaderColumnsNumber ; x < rawData [ 0 ] . length ; x ++ ) {
629
686
@@ -727,6 +784,39 @@ PivotView.prototype.renderRawData = function (data) {
727
784
pivotBottomSection . appendChild ( tableBlock ) ;
728
785
container . appendChild ( pivotTopSection ) ;
729
786
container . appendChild ( pivotBottomSection ) ;
787
+ if ( pageSwitcher ) {
788
+ pageSwitcher . className = "lpt-pageSwitcher" ;
789
+ pageNumbers = ( function getPageNumbersArray ( currentPage , pages ) { // minPage = 1
790
+
791
+ var step = 0 ,
792
+ pagesArr = [ currentPage ] ;
793
+ while ( currentPage > 1 ) {
794
+ currentPage = Math . max ( 1 , currentPage - ( step || 1 ) ) ;
795
+ pagesArr . unshift ( currentPage ) ;
796
+ step = step * step + 1 ;
797
+ }
798
+ step = 0 ;
799
+ currentPage = pagesArr [ pagesArr . length - 1 ] ;
800
+ while ( currentPage < pages ) {
801
+ currentPage = Math . min ( pages , currentPage + ( step || 1 ) ) ;
802
+ pagesArr . push ( currentPage ) ;
803
+ step = step * step + 1 ;
804
+ }
805
+ return pagesArr ;
806
+
807
+ } ) ( this . pagination . page + 1 , this . pagination . pages ) ;
808
+ for ( i in pageNumbers ) {
809
+ td = document . createElement ( "span" ) ;
810
+ if ( pageNumbers [ i ] === this . pagination . page + 1 ) { td . className = "lpt-active" ; }
811
+ td . textContent = pageNumbers [ i ] ;
812
+ ( function ( page ) { td . addEventListener ( CLICK_EVENT , function ( ) { // add handler
813
+ _ . _pageSwitcherHandler . call ( _ , page - 1 ) ;
814
+ } ) } ) ( pageNumbers [ i ] ) ;
815
+ pageSwitcherContainer . appendChild ( td ) ;
816
+ }
817
+ pageSwitcher . appendChild ( pageSwitcherContainer ) ;
818
+ container . appendChild ( pageSwitcher ) ;
819
+ }
730
820
container [ "_primaryColumns" ] = primaryColumns ;
731
821
container [ "_primaryRows" ] = primaryRows ;
732
822
0 commit comments