/** * stacktable.js * Author & copyright (c) 2012: John Polacek * Dual MIT & GPL license * * Page: http://johnpolacek.github.com/stacktable.js * Repo: https://github.com/johnpolacek/stacktable.js/ * * jQuery plugin for stacking tables on small screens * */ ;(function($) { $.fn.stacktable = function(options) { var $tables = this, defaults = {id:'stacktable',hideOriginal:false}, settings = $.extend({}, defaults, options), stacktable; return $tables.each(function() { var $stacktable = $('
'); if (typeof settings.class !== undefined) $stacktable.addClass(settings.class); var markup = ''; $table = $(this); $topRow = $table.find('tr').first(); $table.find('tr').each(function(index,value) { markup += ''; // for the first row, top left table cell is the head of the table if (index===0) { markup += ''+$(this).find('th,td').first().html()+''; } // for the other rows, put the left table cell as the head for that row // then iterate through the key/values else { $(this).find('td').each(function(index,value) { if (index===0) { markup += ''+$(this).html()+''; } else { if ($(this).html() !== ''){ markup += ''; if ($topRow.find('td,th').eq(index).html()){ markup += ''+$topRow.find('td,th').eq(index).html()+''; } else { markup += ''; } markup += ''+$(this).html()+''; markup += ''; } } }); } }); $stacktable.append($(markup)); $table.before($stacktable); if (settings.hideOriginal) $table.hide(); }); }; }(jQuery));