I have a big array and I need to render it into a table. Instead of rendering all items I am rendering only few items horizontally and vertically. Then on scroll based on mouse scroll whether happened vertical / horizontal updating table values. But I have two problems in this
Here are problems with my code
- When scrolling horizontally data is not updated.
- Scrolling horizontally is flickering the screen also.
- Horizontal elements are not aligned properly.
Here is the jsbin link http://jsbin.com/oSOsIQe/2/edit
Here is the JS code, I know code is not clean but I will clean it later.
var $matrix = (function () {
function $matrix(data, holder, hidescrollbar, config) {
var header_h = config.header || "150px";
var data_h = config.header || "90px";
!data && (function () {
// Fake Data, will be removed later
data = new Array(50000);
for (var i = 0, l = data.length; i < l; i++) {
var dummy = Math.random().toString(36).substring(5);
var dum = [];
for (var j = 0; j < 26; j++) {
dum.push(dummy + i);
}
data[i] = dum;
}
}());
hidescrollbar = hidescrollbar || false;
var heightForcer = holder.appendChild(document.createElement('div'));
heightForcer.id = "heightForcer";
var view = null;
//get the height of a single item
var dimensions = (function () {
//generate a fake item and calculate dimensions of our targets
var div = document.createElement('div');
div.style.height = "auto";
div.innerHTML = "<div class='rowHeader'>fake</div><div class='rowData'>fake</div>";
holder.appendChild(div);
var output = {
row: div.firstChild.offsetHeight,
header: div.firstChild.offsetWidth,
data: div.lastChild.offsetWidth
}
holder.removeChild(div);
return output;
})();
function refreshWindow() {
//remove old view
if (view != null) {
view.innerHTML = "";
} else {
//create new view
view = holder.appendChild(document.createElement('div'));
}
var firstItem = Math.floor(holder.scrollTop / dimensions.row);
var lastItem = firstItem + Math.ceil(holder.offsetHeight / dimensions.row) + 1;
if (lastItem + 1 >= data.length) lastItem = data.length - 1;
var hfirstItem = Math.floor(holder.scrollLeft / dimensions.data);
var hlastItem = hfirstItem + Math.ceil(holder.offsetWidth / dimensions.data) + 1;
if (hlastItem + 1 >= data[firstItem].length) hlastItem = data[firstItem].length - 1;
//position view in users face
view.id = 'view';
view.style.top = (firstItem * dimensions.row) + 'px';
view.style.left = (hfirstItem * dimensions.data) + 'px';
var div;
//add the items
for (var index = firstItem; index <= lastItem; ++index) {
div = document.createElement('div');
var curData = data[index].slice(hfirstItem, hlastItem);
div.innerHTML = '<div class="rowHeader">' +
curData.join('</div><div class="rowData">') +
"</div>";
div.className = "listItem";
div.style.height = dimensions.row + "px";
view.appendChild(div);
}
console.log('viewing items ' + firstItem + ' to ' + lastItem);
}
heightForcer.style.height = (data.length * dimensions.row) + 'px';
heightForcer.style.width = (data[0].length * dimensions.data) + 'px';
if (hidescrollbar) {
//work around for non-chrome browsers, hides the scrollbar
holder.style.width = (holder.offsetWidth * 2 - view.offsetWidth) + 'px';
}
refreshWindow();
function delayingHandler() {
//wait for the scroll to finish
//setTimeout(refreshWindow, 10);
refreshWindow();
}
if (holder.addEventListener) holder.addEventListener("scroll", delayingHandler, false);
else holder.attachEvent("onscroll", delayingHandler);
}
return $matrix;
}());
new $matrix(undefined, document.getElementById('listHolder'), false, {
header: "150px",
data: "90px",
headerColumns: 2
});
Please help me on this.