tech @ Beacon Deacon

Making Tables Responsive - Jamie Johnson

Jamie Johnson

April 26, 2016

What if I have a plain table and I want to make it responsive? Thankfully, SpryMedia's DataTables Table plug-in for jQuery offers responsive tables. Using their plug-in, I have written a "tool" to do the conversion for you. All one has to do is add class="RWDtable" to the table tag and let the code do the rest! However, DataTables are quite picky about there being both a thead and tbody in the table. My tool accounts for those instances as well. Below is a plain table:

The Phantom MenaceIPG1999more dataand more dataand even more data
Attack of the ClonesIIPG2002more dataand more dataand even more data
Clone WarsN/ATV2008more dataand more dataand even more data
Revenge of the SithIIIPG-132005more dataand more dataand even more data
RebelsN/ATV2014more dataand more dataand even more data
Rogue OneN/APG-132016more dataand more dataand even more data
A New HopeIVPG1977more dataand more dataand even more data
Empire Strikes BackVPG1980more dataand more dataand even more data
Return of the JediVIPG1983more dataand more dataand even more data
The Force AwakensVIIPG-132015more dataand more dataand even more data

Now, if I add class="RWDtable" to the table tag, then once the code for my tool is involved, the table becomes a responsive table:

The Phantom MenaceIPG1999more dataand more dataand even more data
Attack of the ClonesIIPG2002more dataand more dataand even more data
Clone WarsN/ATV2008more dataand more dataand even more data
Revenge of the SithIIIPG-132005more dataand more dataand even more data
RebelsN/ATV2014more dataand more dataand even more data
Rogue OneN/APG-132016more dataand more dataand even more data
A New HopeIVPG1977more dataand more dataand even more data
Empire Strikes BackVPG1980more dataand more dataand even more data
Return of the JediVIPG1983more dataand more dataand even more data
The Force AwakensVIIPG-132015more dataand more dataand even more data

While it uses DataTables, it does not have the search and paging features enabled since it is retaining the original functionality (or lack of functionality) of the table and just making it responsive. However, that would be easy to accomplish by altering the options on the DataTable call.

Here's the code:


<script type="text/javascript" src="//code.jquery.com/jquery-1.12.0.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/t/dt/dt-1.10.11,r-2.0.2/datatables.min.css"/>
<script type="text/javascript" src="//cdn.datatables.net/t/dt/dt-1.10.11,r-2.0.2/datatables.min.js"></script>
<script type="text/javascript" src="//tech.beacondeacon.com/datatable/PlainTableToRWDTableTool.js"></script>

The last line in bold is my actual tool. My recommendation is to download my tool and upload to your site, which you will have to do if you want to alter any of the DataTable options therein. Please keep the comments in tact for my tool.

Back to top