Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix proposal for: DataTables with column widths defined in % loose the % and don't resize correctly #33 #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a bug fix only temporary clone of the <https://github.com/DataTables/FixedHeader> project
82 changes: 58 additions & 24 deletions js/dataTables.fixedHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @summary FixedHeader
* @description Fix a table's header or footer, so it is always visible while
* Scrolling
* @version 2.1.1
* @version 2.1.2.beta
* @file dataTables.fixedHeader.js
* @author SpryMedia Ltd (www.sprymedia.co.uk)
* @contact www.sprymedia.co.uk/contact
Expand Down 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 Expand Up @@ -982,7 +1016,7 @@ FixedHeader.fnMeasure = function ()
};


FixedHeader.version = "2.1.1";
FixedHeader.version = "2.1.2.beta";


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Expand Down