diff --git a/docs/index.html b/docs/index.html index abb40724..d65d4a54 100644 --- a/docs/index.html +++ b/docs/index.html @@ -5542,6 +5542,28 @@ var wo = $('#mytable').data('tablesorter').widgetOptions; + + + This function adds a colgroup element to the table when widthFixed is true. +

+ A new colgroup with col elements is only added if: + + * Note If a colgroup was added by the plugin, calling this function additional times will refresh the set widths
+
+ Also, the col elements within the colgroup are set with a percentage width to dynamically maintain the fixed column width ratios.
+
+ Use it as follows: +
$.tablesorter.fixColumnWidth( table );
+ +
+ + + diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index 8f51e444..00a1279f 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -131,6 +131,7 @@ table : 'tablesorter', cssHasChild: 'tablesorter-hasChildRow', childRow : 'tablesorter-childRow', + colgroup : 'tablesorter-colgroup', header : 'tablesorter-header', headerRow : 'tablesorter-headerRow', headerIn : 'tablesorter-header-inner', @@ -590,21 +591,6 @@ }); } - // automatically add col group, and column sizes if set - function fixColumnWidth(table) { - var colgroup, overallWidth, - c = table.config; - if (c.widthFixed && c.$table.children('colgroup').length === 0) { - colgroup = $(''); - overallWidth = $(table).width(); - // only add col for visible columns - fixes #371 - $(table.tBodies).not('.' + c.cssInfoBlock).find("tr:first").children(":visible").each(function() { - colgroup.append($('').css('width', parseInt(($(this).width()/overallWidth)*1000, 10)/10 + '%')); - }); - c.$table.prepend(colgroup); - } - } - function updateHeaderSortCount(table, list) { var s, t, o, col, primary, c = table.config, @@ -1127,7 +1113,7 @@ buildHeaders(table); // fixate columns if the users supplies the fixedWidth option // do this after theme has been applied - fixColumnWidth(table); + ts.fixColumnWidth(table); // try to auto detect column type, and store in tables config buildParserCache(table); // start total row count at zero @@ -1183,6 +1169,28 @@ if (typeof c.initialized === 'function') { c.initialized(table); } }; + // automatically add a colgroup with col elements set to a percentage width + ts.fixColumnWidth = function(table) { + table = $(table)[0]; + var overallWidth, percent, + c = table.config, + colgroup = c.$table.children('colgroup'); + // remove plugin-added colgroup, in case we need to refresh the widths + if (colgroup.length && colgroup.hasClass(ts.css.colgroup)) { + colgroup.remove(); + } + if (c.widthFixed && c.$table.children('colgroup').length === 0) { + colgroup = $(''); + overallWidth = c.$table.width(); + // only add col for visible columns - fixes #371 + $(table.tBodies).not('.' + c.cssInfoBlock).find('tr:first').children(':visible').each(function() { + percent = parseInt( ( $(this).width() / overallWidth ) * 1000, 10 ) / 10 + '%'; + colgroup.append( $('').css('width', percent) ); + }); + c.$table.prepend(colgroup); + } + }; + ts.getColumnData = function(table, obj, indx, getCell, $headers){ if (typeof obj === 'undefined' || obj === null) { return; } table = $(table)[0];