Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Attemt for Fix: column widths defined in % get lost after resize
  • Loading branch information
xmojmr committed May 18, 2014
commit 2f828ebe0dbd7af3882ec37542c05750820330a9
78 changes: 56 additions & 22 deletions js/dataTables.fixedHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,24 @@ FixedHeader.prototype = {
* Cloning functions
*/

_getWidth: function(source)
{
// return $(source).width();
if (!(source instanceof jQuery))
source = $(source);

return source.css("width");
},

_setWidth: function(target, value)
{
// $(target).width(value);
if (!(target instanceof jQuery))
target = $(target);

target.css("width", value);
},

/*
* Function: _fnCloneThead
* Purpose: Clone the thead element
Expand All @@ -694,6 +712,8 @@ FixedHeader.prototype = {
*/
_fnCloneThead: function ( oCache )
{
var that = this;

var s = this.fnGetSettings();
var nTable = oCache.nNode;

Expand All @@ -703,49 +723,61 @@ FixedHeader.prototype = {
return;
}

/* Set the wrapper width to match that of the cloned table */
var iDtWidth = $(s.nTable).outerWidth();
oCache.nWrapper.style.width = iDtWidth+"px";
nTable.style.width = iDtWidth+"px";

/* Remove any children the cloned table has */
while ( nTable.childNodes.length > 0 )
var visible = $(s.nTable).css("display") != "none";
if (visible)
{
$('thead th', nTable).unbind( 'click' );
nTable.removeChild( nTable.childNodes[0] );
// http://stackoverflow.com/a/19873734/2626313
$(s.nTable).hide();
}

/* Clone the DataTables header */
var nThead = $('thead', s.nTable).clone(true)[0];
nTable.appendChild( nThead );

/* Copy the widths across - apparently a clone isn't good enough for this */
var a = [];
var b = [];

$("thead>tr th", s.nTable).each( function (i) {
a.push( $(this).width() );
a.push( that._getWidth(this) );
} );

$("thead>tr td", s.nTable).each( function (i) {
b.push( $(this).width() );
b.push( that._getWidth(this) );
} );

/* Set the wrapper width to match that of the cloned table */
var iDtWidth = that._getWidth(s.nTable);
that._setWidth(oCache.nWrapper, iDtWidth);;
that._setWidth(nTable, iDtWidth);

/* Remove any children the cloned table has */
while ( nTable.childNodes.length > 0 )
{
$('thead th', nTable).unbind( 'click' );
nTable.removeChild( nTable.childNodes[0] );
}

/* Clone the DataTables header */
var nThead = $('thead', s.nTable).clone(true)[0];
nTable.appendChild( nThead );

$("thead>tr th", s.nTable).each( function (i) {
$("thead>tr th:eq("+i+")", nTable).width( a[i] );
$(this).width( a[i] );
that._setWidth( $("thead>tr th:eq("+i+")", nTable), a[i] );
that._setWidth( this, a[i] );
} );

$("thead>tr td", s.nTable).each( function (i) {
$("thead>tr td:eq("+i+")", nTable).width( b[i] );
$(this).width( b[i] );
that._setWidth( $("thead>tr td:eq("+i+")", nTable), b[i] );
that._setWidth( this, b[i] );
} );

// Stop DataTables 1.9 from putting a focus ring on the headers when
// clicked to sort
$('th.sorting, th.sorting_desc, th.sorting_asc', nTable).bind( 'click', function () {
this.blur();
} );

if (visible)
{
$(s.nTable).show();
}
},

/*
Expand All @@ -756,11 +788,13 @@ FixedHeader.prototype = {
*/
_fnCloneTfoot: function ( oCache )
{
var that = this;

var s = this.fnGetSettings();
var nTable = oCache.nNode;

/* Set the wrapper width to match that of the cloned table */
oCache.nWrapper.style.width = $(s.nTable).outerWidth()+"px";
that._setWidth(oCache.nWrapper, that._getWidth(s.nTable));

/* Remove any children the cloned table has */
while ( nTable.childNodes.length > 0 )
Expand All @@ -774,11 +808,11 @@ FixedHeader.prototype = {

/* Copy the widths across - apparently a clone isn't good enough for this */
$("tfoot:eq(0)>tr th", s.nTable).each( function (i) {
$("tfoot:eq(0)>tr th:eq("+i+")", nTable).width( $(this).width() );
that._setWidth( $("tfoot:eq(0)>tr th:eq("+i+")", nTable), that._getWidth( this) );
} );

$("tfoot:eq(0)>tr td", s.nTable).each( function (i) {
$("tfoot:eq(0)>tr td:eq("+i+")", nTable).width( $(this).width() );
that._setWidth( $("tfoot:eq(0)>tr td:eq("+i+")", nTable), that._getWidth( this ) );
} );
},

Expand Down