Skip to content

Commit ad88fc3

Browse files
pagination feature add
1 parent 63acfa5 commit ad88fc3

File tree

6 files changed

+153
-17
lines changed

6 files changed

+153
-17
lines changed

example/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@
102102
// if cellDrillThrough callback returns boolean false, DrillThrough won't be performed.
103103
//, cellDrillThrough: function ({Object { event: {event}, filters: {string[]} }}) {}
104104
}
105+
, pagination: 30 // Enables pagination. Pass a number of lines to show by page.
105106
//, hideButtons: true // hides "back" and "drillThrough" buttons
106107
//, triggerEvent: "touchstart" // all "click" events will be replaced by this event
107108
//, caption: "My table" // if set, table basic caption will be replaced by this text
108109
, showSummary: true // show summary by columns
109-
, enableHeadersScrolling: true // enable scrolling both for table and headers
110+
//, enableHeadersScrolling: true // enable scrolling both for table and headers
110111
//, loadingMessageHTML: "LOADING DATA..." // HTML displaying during data load
111112
//, conditionalFormattingOn: true // enable conditional formatting rules
112113
//, defaultFilterSpecs: ["[Date].[H1].[month].&[]"] ] // default filters array

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "LightPivotTable",
33
"author": "ZitRo",
4-
"version": "1.0.0-beta.3",
4+
"version": "1.0.0-beta.4",
55
"description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache",
66
"main": "test/testServer.js",
77
"repository": {

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var setup = { // Object that contain settings. Any setting may be missed.
5252
// if cellDrillThrough callback returns boolean false, DrillThrough won't be performed.
5353
, cellDrillThrough: function ({Object { event: {event}, filters: {string[]}, cellData: {object} }}) {}
5454
} ]
55+
[ , pagination: 30 ] // Enables pagination. Pass a number of lines to show by page.
5556
[ , hideButtons: true ] // hides "back" and "drillThrough" buttons
5657
[ , triggerEvent: "touchstart" ] // all "click" events will be replaced by this event
5758
[ , caption: "My table" ] // if set, table basic caption will be replaced by this text
@@ -107,5 +108,4 @@ Also anytime you can build project and then check <code>build/example/index.html
107108
## Preview
108109

109110
Run <code>gulp</code> command and then check <code>build/WEBModule/index.html</code>.
110-
111-
![Light pivot table](https://cloud.githubusercontent.com/assets/4989256/5570072/4c3a161e-8f84-11e4-929b-6577728df6ba.png)
111+
![Light pivot table](https://cloud.githubusercontent.com/assets/4989256/5821832/b73c880c-a0d7-11e4-8458-832329b1c48a.png)

source/css/LightPivot.css

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
}
101101

102102
.lpt .lpt-topSection, .lpt .lpt-bottomSection {
103+
border-right: 1px solid rgb(208, 208, 208);
103104
position: relative;
104105
overflow: hidden;
105106
}
@@ -174,6 +175,46 @@
174175
/*border-bottom: none;*/
175176
}
176177

178+
.lpt .lpt-pageSwitcher {
179+
position: absolute;
180+
display: table;
181+
box-sizing: border-box;
182+
bottom: 0;
183+
width: 100%;
184+
height: 19px;
185+
font-size: 14px;
186+
border: 1px solid rgb(208, 208, 208);
187+
background: #F0F0F0;
188+
}
189+
190+
.lpt .lpt-pageSwitcher > div {
191+
position: relative;
192+
display: table-cell;
193+
vertical-align: middle;
194+
height: 100%;
195+
text-align: center;
196+
}
197+
198+
.lpt .lpt-pageSwitcher > div > span:first-child {
199+
border-left: 1px solid rgb(208, 208, 208);
200+
}
201+
202+
.lpt .lpt-pageSwitcher > div > span {
203+
cursor: pointer;
204+
padding: 0 5px 0 5px;
205+
display: inline-block;
206+
min-height: 100%;
207+
border-right: 1px solid rgb(208, 208, 208);
208+
}
209+
210+
.lpt .lpt-pageSwitcher > div > span:hover {
211+
background: #FFF7D7;
212+
}
213+
214+
.lpt-active {
215+
background: #FFF7D7;
216+
}
217+
177218
/*.lpt .lpt-leftHeader table {*/
178219
/*margin-bottom: 10em;*/
179220
/*}*/
@@ -254,6 +295,10 @@
254295
animation-delay: -0.4s;
255296
}
256297

298+
::-webkit-scrollbar-corner {
299+
background-color: #F0F0F0;
300+
}
301+
257302
@-webkit-keyframes lpt-stretch {
258303
0%, 40%, 100% {
259304
-webkit-transform: scaleY(0.4);

source/js/LightPivotTable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ LightPivotTable.prototype.popDataSource = function (popData) {
171171
*/
172172
LightPivotTable.prototype.dataIsChanged = function () {
173173

174-
this.pivotView.renderRawData(this.dataController.getData());
174+
this.pivotView.dataChanged(this.dataController.getData());
175175

176176
};
177177

source/js/PivotView.js

Lines changed: 102 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ var PivotView = function (controller, container) {
2626
messageElement: undefined
2727
};
2828

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+
2939
this.controller = controller;
3040

3141
this.SCROLLBAR_WIDTH = (function () {
@@ -116,18 +126,27 @@ PivotView.prototype.getCurrentTableData = function () {
116126
PivotView.prototype.pushTable = function (opts) {
117127

118128
var _ = this,
129+
pg,
119130
tableElement = document.createElement("div");
120131

121132
tableElement.className = "tableContainer";
122133
if (this.tablesStack.length) tableElement.style.left = "100%";
123134

124135
this.tablesStack.push({
125136
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+
}
127144
});
128145

129146
this.elements.base.appendChild(tableElement);
130147
this.elements.tableContainer = tableElement;
148+
console.log("pagination changed to ", pg);
149+
this.pagination = pg;
131150

132151
setTimeout(function () {
133152
_._updateTablesPosition();
@@ -141,14 +160,38 @@ PivotView.prototype.popTable = function () {
141160

142161
this._updateTablesPosition(1);
143162
var garbage = this.tablesStack.pop();
163+
this.pagination = this.tablesStack[this.tablesStack.length - 1].pagination;
144164

145165
setTimeout(function () {
146166
garbage.element.parentNode.removeChild(garbage.element);
147-
}, 500);
167+
}, this.ANIMATION_TIMEOUT);
148168
this.elements.tableContainer = this.tablesStack[this.tablesStack.length - 1].element;
149169

150170
};
151171

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+
152195
PivotView.prototype._columnClickHandler = function (columnIndex) {
153196

154197
this.controller.dataController.sortByColumn(columnIndex);
@@ -161,6 +204,13 @@ PivotView.prototype._rowClickHandler = function (rowIndex, cellData) {
161204

162205
};
163206

207+
PivotView.prototype._pageSwitcherHandler = function (pageIndex) {
208+
209+
this.pagination.page = pageIndex;
210+
this.renderRawData(this.controller.dataController.getData());
211+
212+
};
213+
164214
PivotView.prototype._backClickHandler = function (event) {
165215

166216
if (event) {
@@ -383,12 +433,13 @@ PivotView.prototype.recalculateSizes = function (container) {
383433
lTableHead.removeChild(lTableHead.lastChild);
384434
}
385435

386-
var headerW = leftHeader.offsetWidth,
436+
var pagedHeight = this.pagination.on ? this.PAGINATION_BLOCK_HEIGHT : 0,
437+
headerW = leftHeader.offsetWidth,
387438
headerH = topHeader.offsetHeight,
388439
containerHeight = container.offsetHeight,
389440
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
392443
&& this.SCROLLBAR_WIDTH > 0,
393444
cell, tr, cellWidths = [], columnHeights = [], i;
394445

@@ -416,10 +467,10 @@ PivotView.prototype.recalculateSizes = function (container) {
416467

417468
topHeader.style.marginLeft = headerW + "px";
418469
tableBlock.style.marginLeft = headerW + "px";
419-
leftHeader.style.height = containerHeight - headerH + "px";
470+
leftHeader.style.height = containerHeight - headerH - pagedHeight + "px";
420471
leftHeader.style.width = headerW + "px";
421472
if (mainHeaderWidth > headerW) leftHeader.style.width = mainHeaderWidth + "px";
422-
tableBlock.style.height = containerHeight - headerH + "px";
473+
tableBlock.style.height = containerHeight - headerH - pagedHeight + "px";
423474
headerContainer.style.height = headerH + "px";
424475

425476
if (addExtraLeftHeaderCell) {
@@ -436,7 +487,7 @@ PivotView.prototype.recalculateSizes = function (container) {
436487
leftHeader.className = leftHeader.className.replace(/\sbordered/, "")
437488
+ " bordered";
438489
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";
440491
}
441492

442493
for (i in tableTr.childNodes) {
@@ -518,7 +569,11 @@ PivotView.prototype.renderRawData = function (data) {
518569
LHTHead = document.createElement("thead"),
519570
mainTable = document.createElement("table"),
520571
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;
522577

523578
// clean previous content
524579
this.removeMessage();
@@ -617,13 +672,15 @@ PivotView.prototype.renderRawData = function (data) {
617672
renderHeader(
618673
0,
619674
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,
622679
LHTHead
623680
);
624681

625682
// render table
626-
for (y = info.topHeaderRowsNumber; y < rawData.length; y++) {
683+
for (y = tempI/*info.topHeaderRowsNumber*/; y < tempJ/*rawData.length*/; y++) {
627684
tr = document.createElement("tr");
628685
for (x = info.leftHeaderColumnsNumber; x < rawData[0].length; x++) {
629686

@@ -727,6 +784,39 @@ PivotView.prototype.renderRawData = function (data) {
727784
pivotBottomSection.appendChild(tableBlock);
728785
container.appendChild(pivotTopSection);
729786
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+
}
730820
container["_primaryColumns"] = primaryColumns;
731821
container["_primaryRows"] = primaryRows;
732822

0 commit comments

Comments
 (0)