Fork me on GitHub

Hello my name is Pui, I am a web developer based in London.

jQuery/CSS - Responsive Tables: Solution for Back-end integration and having multiple tables

11 June 2014

Tables are always fun. Especially when you want to make them responsive. Having worked on many projects where tables are part of the WYSIWYG editor and bog-standard table markup will be generated by the CMS, I’ve found a lovely way to make the tables responsive but also make it friendly for the back-ender who’s integrating the CMS.

The best responsive table technique I’ve found is by Chris Coyier. I use this every time and it works a treat and is easy. It’s not a hack and is so clean to use.

The issue for the back-ender is to generate the CSS parameter for the content. Like so:

@media only screen and (max-width: 760px), (min-device-width: 768px) and (max-device-width: 1024px)  {  

td:nth-of-type(1):before { content: "First Name"; } 
td:nth-of-type(2):before { content: "Last Name"; } 
 td:nth-of-type(3):before { content: "Job Title"; } 
 td:nth-of-type(4):before { content: "Favorite Color"; } 
 td:nth-of-type(5):before { content: "Wars of Trek?"; } 
 td:nth-of-type(6):before { content: "Porn Name"; } 
 td:nth-of-type(7):before { content: "Date of Birth"; } 
 td:nth-of-type(8):before { content: "Dream Vacation City"; } 
 td:nth-of-type(9):before { content: "GPA"; } 
 td:nth-of-type(10):before { content: "Arbitrary Data"; }

}

 

I’ve had it before where I’ve written this particular piece of CSS as inline styling, if the back-ender can’t reach my CSS stylesheet, surely they can reach it this way? However this is still difficult for the back-ender to integrate as tables aren’t just a one-off on the site. The user is expected to add tables in the WYSIWYG whenever they want. Doing it this way means quite a big job for the build to make this inline styling appear whenever a table is generated.

So that’s when I thought to write a script to generate this styling.

var generatedCSS = ""; $('table').each(function( index ) { 
var tableId = $(this).attr('id'); 
$('#'+tableId+' th').each(function( thIndex ) {  
var thText = $(this).text().trim(); generatedCSS = generatedCSS + "#"+tableId+" td:nth-of-type(" + (thIndex+1) + "):before { content: '"+thText+"'; }"; 
}); 
}); 
$('#table1').prepend('');

So what this does is loop through each of the table headers and grab the text inside and it’s index. It then spits this out and prints it on the page on document ready. Nice.

Before I jumped for joy too much I realised that there was problem: What if there is more than one table on the page? So I reworked the above script to take into account multiple tables:

$(document).ready(function(){ 
 if ( $('table').length ) {  
//add ids for the table incase there is more than one on the page  $('table').each(function(i) {      
$(this).attr('id', "table" + (i + 1));   
});  
//Responsive table - adding header content to inline CSS so don't have to do it in back-end  
var generatedCSS = "";  
$('table').each(function( index ) {   
var tableId = $(this).attr('id');   
$('#'+tableId+' th').each(function( thIndex ) {    
var thText = $(this).text().trim();      
generatedCSS = generatedCSS + "#"+tableId+" td:nth-of-type(" + (thIndex+1)  + "):before { content: '"+thText+"'; }";   
});  
});  //only prepends this styling once if table1 exists which is should if there is a table on the page  
$('#table1').prepend(''); 
} 
});

Now I loop through each table on the page, give it it’s own ID and then target that ID to generate the CSS for each particular table. Bingo. All the CMS has to do is to ping out bog-standard table markup and the jQuery does all the responsiveness in the background via CSS. No need for inline styling, no need for dirty back-end hacks.

On a final note, it doesn’t seem like much difference is made visually. Chris Coyier’s CSS is brilliant and works, but when working alongside a CMS that won’t automatically spit out or able to create that inline styling for you, this serves as a solution to that.

Demo with HTML/CSS/jQuery.

About the author

Pui-Ling Lau, front end web developer and designer based in Kent and London

Pui is a User Experience developer working as a contractor in and around London.