Two html tables I have, filled with various elements like span, input, select, and more. My goal is to make sure that the rows in both tables are of equal height. Unfortunately, simply setting the 'height' attribute on tr
or td
did not yield the desired results, as a row would adjust its height based on the tallest element within it.
Unable to find a CSS solution for enforcing row heights, I resorted to writing a JavaScript function. This function loops through each row, compares the heights of the corresponding rows in the two tables, and adjusts the height to match the taller row.
However, this method proved to be slow when dealing with tables containing numerous rows. It seems that the performance lag is caused by the reflow triggered by styling changes.
Is there any advice on how to improve this? Keep in mind that merging the tables is not an option.
Below is a snippet of my code, but I am open to suggestions for a completely different approach:
var rightTableRows = mainTable.children("tbody").children("tr:parent");
var leftTableRows = colHeader.children("tbody").children("tr:parent");
for (chr=0;chr < leftTableRows .length;chr++) {
var rowLeft = leftTableRows [chr];
var heightleft = rowLeft.offsetHeight;
var rowRight = rightTableRows[chr];
var heightright = rowRight.offsetHeight;
if(heightleft != heightright){
console.log("left: "+heightleft +" - right: "+heightright);
if(heightleft>heightright){
rowRight.setAttribute("style","height:"+heightleft+"px");
}else{
rowLeft.setAttribute("style","height:"+heightright+"px");
}
}
}