Responsive tables for phones
Responsive tables for phones
I combined some tools to create responsive tables for phones and I thought I'd share. Maybe someone can help me turn this into a plugin. To create it I used http://css-tricks.com/responsive-data-tables/ and http://twitter.github.com/bootstrap/scaffolding.html#responsive.
My Css
[code]
@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
.dataTable > table, thead, tbody, th, td, tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
.dataTable > thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.dataTable > tr { border: 1px solid #ccc; }
.dataTable > td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
.dataTable > td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
}
[/code]
I added a fnCreatedCell callback to each column to add a label only visible on the phone. I'm using angularjs so the reference to the column title comes my code.
[code]
"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
$(nTd).html("" + $scope.contactsColumns[iCol].sTitle + ": " + $(nTd).html() + '');
}
[/code]
The table ends up working like http://css-tricks.com/examples/ResponsiveTables/responsive.php.
I also added "sClass" .visible-desktop for columns I wanted to hide on tablets and phones or .hidden-tablet or .hidden-phone to hide just on one or the other.
My Css
[code]
@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
/* Force table to not be like tables anymore */
.dataTable > table, thead, tbody, th, td, tr {
display: block;
}
/* Hide table headers (but not display: none;, for accessibility) */
.dataTable > thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.dataTable > tr { border: 1px solid #ccc; }
.dataTable > td {
/* Behave like a "row" */
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
.dataTable > td:before {
/* Now like a table header */
position: absolute;
/* Top/left values mimic padding */
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
}
[/code]
I added a fnCreatedCell callback to each column to add a label only visible on the phone. I'm using angularjs so the reference to the column title comes my code.
[code]
"fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
$(nTd).html("" + $scope.contactsColumns[iCol].sTitle + ": " + $(nTd).html() + '');
}
[/code]
The table ends up working like http://css-tricks.com/examples/ResponsiveTables/responsive.php.
I also added "sClass" .visible-desktop for columns I wanted to hide on tablets and phones or .hidden-tablet or .hidden-phone to hide just on one or the other.
This discussion has been closed.
Replies
In Javascript terms, I think something similar could be done with fnSetColumnVis - which I hope to wrap up into a plug-in at some point in future as well. But 1.10 needs to take priority first!
Allan