As part of my responsive design, I'm trying to dynamically hide columns of a table if the viewport size shrinks past a certain point. I've tried to set
.style.visibility = "collapse" for all <tr> elements and .style.opacity = "0" for all <td> elements. I then have to hide the background of the table so that it doesn't show that the table width is still there while also increasing the width of the table (160%) so that the remaining columns fill the screen.
While this actually works on Chrome, Firefox, IE (including ie8!) and my mobile browsers, it seems like a ridiculous kludgy hack. Any other suggestions?
var jmq = window.matchMedia("screen and (max-width: 610px)");
jmq.addListener(jmqListener);
function jmqListener(jmq){
var colTitle = getElementsByClassName('col-title');
var colName = getElementsByClassName('col-name');
var colDate = getElementsByClassName('col-date');
var colFb = getElementsByClassName('col-fb');
var table = getElementsByClassName('default');
if (jmq.matches || window.innerWidth < 611 ) {
//Mobile
... resize controls
// hide table columns
if (colName !== null){
for(var i=0,j=colName.length; i<j; i++){
colName[i].style.visibility = "collapse";
colName[i].style.opacity = "0";
}
}
// HACK - increase table width and hide the background which would show the reserved table space
if (table !== null){
for(var i=0,j=table.length; i<j; i++){
table[i].style.width = "160%";
table[i].style.background = "transparent";
}
}
}
else {
// Desktop
... restore control layout for desktop
// restore table column(s)
if (colName !== null){
for(var i=0,j=colName.length; i<j; i++){
colName[i].style.visibility = "visible";
colName[i].style.opacity = "100";
}
}
if (table !== null){
for(var i=0,j=table.length; i<j; i++){
table[i].style.width = "100%";
table[i].style.background = "#C8C8C8";
}
}
}
}
function getElementsByClassName(className) {
if (document.getElementsByClassName) {
return document.getElementsByClassName(className); }
else {
return document.querySelectorAll('.' + className);
}
}