I would like to make a responsive table. My goal is to switch it from a table to more, as shown here . However, although it still looks like a table, I would like all the rows to have the height of the highest row.
I managed to do this work separately, but when I try to combine them, resizing distorts the table format as a list. How do I reset the initial state of a table to any resize when I go below 760 pixels?
Here is the html that I have so far:
function resizeTable(tableID){
var tbl=document.getElementById(tableID), row=0;
if ($(window).width() > 760) {
var biggestRow=0, rowHeight=0;
for (row=0; row < tbl.rows.length; row++) {
rowHeight=parseInt(tbl.rows[row].offsetHeight);
if (rowHeight > biggestRow) {biggestRow=rowHeight;}
}
for (row=0; row < tbl.rows.length; row++) {
tbl.rows[row].style.height=biggestRow + "px";
}
} else {
for (row=0; row < tbl.rows.length; row++) {
tbl.rows[row].style.height='auto';
}
}
}
$(window).resize(function () {
resizeTable('myTable');
});
table {
width: 100%;
border-collapse: collapse;
}
.myTable tr:nth-of-type(odd) {
background: #eee;
}
.myTable th {
background: #3F3F3F;
color: white;
font-weight: bold;
}
td, th {
padding: 6px;
border: 1px solid #ccc;
text-align: left;
}
@media
only screen and (max-width: 760px),
(min-device-width: 768px) and (max-device-width: 1024px) {
table, thead, tbody, th, td, tr {
display: block;
}
thead tr {
position: absolute;
top: -9999px;
left: -9999px;
}
.myTable tr { border: 1px solid #ccc; }
.myTable td {
border: none;
border-bottom: 1px solid #eee;
position: relative;
padding-left: 50%;
}
.myTable td:before {
position: absolute;
top: 6px;
left: 6px;
width: 45%;
padding-right: 10px;
white-space: nowrap;
}
.myTable td:nth-of-type(1):before { content: "Date"; }
.myTable td:nth-of-type(2):before { content: "Title"; }
.myTable td:nth-of-type(3):before { content: "Speaker(s)"; }
.myTable td:nth-of-type(4):before { content: "Institution"; }
}
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body onload="resizeTable('myTable');">
<div class="myTable">
<table id="myTable">
<thead>
<tr>
<th class="c1">Date</th>
<th class="c2">Title</th>
<th class="c3">Speaker(s)</th>
<th class="c4">Institution</th>
</tr>
</thead>
<tbody>
<tr>
<td>09/01</td>
<td>Departmental Research - Hard Matter</td>
<td>Drs. J. Cui, S. Mishra, X. Shen and T. Hoan</td>
<td>University of Memphis</td>
</tr>
<tr>
<td>09/08</td>
<td>Departmental Research - Soft Matter</td>
<td>Drs. S. Jahan, M. Laradji, F. Sabri and P. Pradhan</td>
<td>University of Memphis</td>
</tr>
<tr>
<td>09/15</td>
<td>Super-paramagnetic Relaxations in Magnetic Nanoparticles</td>
<td>Sunghyun Yoon</td>
<td>Gunsan National University, Korea & University of Memphis</td>
</tr>
<tr>
<td>09/22</td>
<td>Simulating Disorder in Functional Materials</td>
<td>Tom Berlijn</td>
<td>Oak Ridge National Lab</td>
</tr>
<tr>
<td>09/29</td>
<td>Computational Self-Assembly on Lipid Membranes</td>
<td>Alexander D. Olinger</td>
<td>University of Memphis</td>
</tr>
<tr>
<td>10/06</td>
<td>No seminar. Materials Day</td>
<td>Dr. Mishra</td>
<td>University of Memphis</td>
</tr>
<tr>
<td>10/13</td>
<td>Electro- and Photocatalytics H2 Production by Molecular Co Complexes with Pentantate Ligands</td>
<td>Xuan Zhao</td>
<td>University of Memphis</td>
</tr>
<tr>
<td>10/20</td>
<td>Van der Waals Heterojunctions with Two-dimensional Materials for Low Power Electronics</td>
<td>Tania Roy</td>
<td>University of Central Florida</td>
</tr>
<tr>
<td>10/27</td>
<td>Hearing the Shape of a Drum: Characterization of Biological Tissue Microstructure Using Advanced Diffusion MR Imaging</td>
<td>Junzhong Xu</td>
<td>Vanderbilt University</td>
</tr>
<tr>
<td>11/03</td>
<td>Ultrafast Spectroscopy of Nanomaterials for Energy Applications</td>
<td>Kannatassen Appavoo</td>
<td>University of Alabama at Birmingham</td>
</tr>
<tr>
<td>11/10</td>
<td>Prediction and Alteration of Surface Wettability</td>
<td>Jian Liu</td>
<td>Vanderbilt University</td>
</tr>
<tr>
<td>11/17</td>
<td>The Role of Phospholipid Membrane Shape in Cellular Function</td>
<td>Eric Spangler</td>
<td>University of Memphis</td>
</tr>
</tbody>
</table>
</div>
</body>
Run codeHide result
source
share