Skip to content

Instantly share code, notes, and snippets.

@danlynn
Created September 30, 2011 15:47
Show Gist options
  • Select an option

  • Save danlynn/1254172 to your computer and use it in GitHub Desktop.

Select an option

Save danlynn/1254172 to your computer and use it in GitHub Desktop.
Description
/*
* Rewraps the elements in a table to the specified 'width'. If 'elements'
* is provided then populates the innerHTML of the cells of the specified
* 'table' using the data in the 'elements' array instead of re-using the
* elements already in the table.
*
* rewrapTableToWidth($('#myTable'), $(window).width())
*/
function rewrapTableToWidth(table, width, elements) {
table = typeof(table[0]) != "undefined" ? table[0] : table;
elements = typeof(elements) != 'undefined' ? elements : null;
var cols = 0;
do {
rewrapTableToCols(table, ++cols, elements);
if (table.rows[0].cells.length < cols)
break;
} while (table.offsetWidth < width);
rewrapTableToCols(table, (cols-1 < 1 ? 1 : cols-1), elements);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment