I am looking to design a table that can be horizontally scrolled with a dynamic number of columns. The goal is to keep the first column fixed/frozen while scrolling horizontally. I attempted to achieve this using the following CSS, which successfully keeps the first column fixed but causes the second and third columns to be hidden under it.
.mydiv {
overflow-y: scroll;
}
.headcol {
position:absolute;
width: 100px;
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tablemy {
border-collapse: collapse;
border: 1px solid #c4c0c0;
}
.thmy {
text-align:center;
background-color: #4CAF50;
color: white;
font-family:arial;
font-size:13px;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
border: 1px solid #c4c0c0;
}
.tdmy {
white-space: nowrap;
padding:8px;
border-left:1px solid #ccc;
border-top:1px solid #ccc;
padding-left:15px;
padding-right:15px;
padding-top:10px;
padding-bottom:10px;
font-size:12px;
font-family:arial;
border: 1px solid #c4c0c0;
color:#585858;
}
.trmy:nth-child(odd) {
background-color:#F5F5F5;
}
tr:nth-child(even) {
background-color:#ffffff;
}
Here is an image of my current table: https://i.sstatic.net/Yy3FD.png
I am constructing this table in JavaScript.
$(window).load(function(){
var customers = new Array();
customers.push(["TN04 AH 0253", "55"]);
customers.push(["TN04 AF 8830", "67"]);
customers.push(["TN37 CM 1249", "33"]);
customers.push(["TN19 F 9175", "47"]);
customers.push(["TN37CM 1932", "0"]);
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
var dateArray = getDates(firstDay, lastDay);
// Create an HTML Table element.
var table = document.createElement("TABLE");
table.setAttribute('class', 'tablemy');
table.border = "1";
// Get the count of columns.
var columnCount = customers[0].length;
var row = table.insertRow(-1);
for (var i = 0; i < dateArray.length; i++) {
var headerCell = document.createElement("TH");
if (i === 0) {
headerCell.setAttribute('class', 'headcol');
} else {
headerCell.setAttribute('class', 'thmy');
}
headerCell.innerHTML = dateArray[i];
row.appendChild(headerCell);
}
// Add the data rows.
for (var i = 0; i < customers.length; i++) {
row = table.insertRow(-1);
row.setAttribute('class', 'trmy');
for (var j = 0; j < columnCount; j++) {
var cell = row.insertCell(-1);
if (j === 0) {
cell.setAttribute('class', 'headcol');
} else {
cell.setAttribute('class', 'tdmy');
}
cell.innerHTML = customers[i][j];
}
}
var dvTable = document.getElementById("dvTable");
dvTable.innerHTML = "";
dvTable.appendChild(table);
})
function getDates(date1, date2) {
var day = 1000 * 60 * 60 * 24;
var diff = (date2.getTime() - date1.getTime())/day;
var dateArray = new Array();
dateArray.push("Unit/Count");
for(var i=0; i<=diff; i++)
{
var xx = date1.getTime() + day * i;
var yy = new Date(xx);
var strDate = yy.getDate() + "/" + (yy.getMonth() + 1);
dateArray.push(strDate);
}
return dateArray;
}