henningn commented on a change in pull request #28: TOBAGO-2021: Sheet should
be able to lazy load rows by scroll events
URL: https://github.com/apache/myfaces-tobago/pull/28#discussion_r387645447
##########
File path:
tobago-theme/tobago-theme-standard/src/main/resources/META-INF/resources/tobago/standard/tobago-bootstrap/_version/js/tobago-sheet.js
##########
@@ -426,7 +494,69 @@ Tobago.Sheet.setup2 = function (sheets) {
sheet.find(".tobago-sheet-cell >
input.tobago-sheet-columnSelector").click(function(event)
{event.preventDefault()});
});
- // init reload
+ // lazy load by scrolling
+ jQuery(sheets).each(function () {
+ var $sheet = jQuery(this);
+ var lazy = $sheet.data("tobago-lazy");
+
+ function getTemplate(height, columns, rowIndex) {
+ var tr = document.createElement("tr");
+ tr.dataset.tobagoRowIndex = rowIndex;
+ tr.classList.add("tobago-sheet-row");
+ tr.setAttribute("dummy", "dummy");
+ tr.style.height = height + "px";
+ var td = document.createElement("td");
+ td.classList.add("tobago-sheet-cell");
+ td.colSpan = columns;
+ tr.appendChild(td);
+ return tr;
+ }
+
+ console.time("lazy");
+ if (lazy) {
+ // prepare the sheet with some auto-created (empty) rows
+ var rows = $sheet.data("tobago-rows");
+ var rowCount = $sheet.data("tobago-row-count");
+ var $sheetBody = $sheet.find(".tobago-sheet-body");
+ var $tbody = $sheetBody.find("tbody");
+ var columns = $tbody.find("tr:first").find("td").length;
+ var height = $tbody.height() / rows;
+ var pointer = $tbody.get(0).rows[0]; // points current row
+ console.time("lazy2");
+ for (var i = 0; i < rowCount; i++) {
+ if (pointer) {
+ var rowIndex = Number(pointer.dataset.tobagoRowIndex);
+ if (i < rowIndex) {
+ var template = getTemplate(height, columns, i);
+ pointer.insertAdjacentElement("beforebegin", template);
+ } else if (i === rowIndex) {
+ pointer = pointer.nextElementSibling;
+ } else {
+ template = getTemplate(height, columns, i);
+ pointer.insertAdjacentElement("afterend", template);
+ pointer = template;
+ }
+ } else {
+ template = getTemplate(height, columns, i);
+ $tbody.get(0).insertAdjacentElement("beforeend", template);
+ pointer = template;
+ }
+ }
+ console.timeEnd("lazy2");
+
+ $sheetBody.bind("scroll", function () {
+ Tobago.Sheet.lazyCheck($sheet);
+ });
+
+ // initial
+ Tobago.Sheet.lazyCheck($sheet);
+
+ $sheet.data("tobago-lazy-initialized", true);
+ }
+ console.timeEnd("lazy");
Review comment:
dev_only missing
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services