Gör om första raden i en tabell till thead med th

Gör om första raden i en tabell till <thead> med <th> för att kunna använda jQuery pluginet jQuery tablesorter utan att tabellerna har korrekt markup från början.

jQuery(document).ready(function()
{
	// Loop through all sortable tables
	jQuery('table.sortable').each(function(i, myTable) {
		// If table dont have a thead defined
		if(jQuery('thead', myTable).length == 0){
			// Extract first row from table
			var myHead = '<tr>';
			jQuery('tr:first td', myTable).each(function(i, td) {
				myHead += '<th>' + jQuery(td).html() + '</th>';
			});
			myHead += '</tr>';
			// Remove first row from table
			jQuery('tr:first', myTable).remove();
			// Add the first row to thead
			jQuery(myTable).prepend(
				jQuery('<thead></thead>').append(myHead)
			);
		}
		// Sort the table
		jQuery(myTable).tablesorter(); 
	});
});
Ladda fler artiklar
Daniel Hansson Avatar