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

Fixed Column Integration #85

Open
wants to merge 2 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
32 changes: 32 additions & 0 deletions css/fixedHeader.dataTables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,35 @@ table.fixedHeader-locked {
display: none;
}
}

.fixedHeader-container {
position: fixed;
top: 50px;
z-index: 99;
overflow: hidden;
box-shadow: 0 3px 5px rgba(222, 222, 222, 0.65);
}
.fixedHeader-scroll {
position: relative;
width: 100%;
overflow: hidden;
}
.table-analyze-results.fixedHeader-floating {

}
.fixedHeader-container table.fixedHeader-floating {
position: relative !important;
top: 0 !important;
left: 0 !important;
}
.DTFH_Wrapper {
position: absolute;
top: 0;
z-index: 999;
}
.DTFH_LeftWrapper {
left: 0;
}
.DTFH_RightWrapper {
right: 0;
}
110 changes: 103 additions & 7 deletions js/dataTables.fixedHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ var FixedHeader = function ( dt, config ) {
header: {
host: null,
floating: null,
placeholder: null
placeholder: null,
container: null,
},
footer: {
host: null,
Expand Down Expand Up @@ -265,6 +266,7 @@ $.extend( FixedHeader.prototype, {
var itemElement = item === 'header' ?
this.dom.thead :
this.dom.tfoot;
var fixedColumns = this.c.fixedColumns;

if ( ! force && itemDom.floating ) {
// existing floating element - reuse it
Expand All @@ -278,11 +280,84 @@ $.extend( FixedHeader.prototype, {
itemDom.floating.remove();
}

itemDom.floating = $( dt.table().node().cloneNode( false ) )
.css( 'table-layout', 'fixed' )
.removeAttr( 'id' )
.append( itemElement )
.appendTo( 'body' );
// Check if there is fixed columns options
if ( fixedColumns.isFixed ) {
itemDom.container = $('.fixedHeader-container');
if (itemDom.container.length <= 0) {
// Create
itemDom.container = $('<div></div>');
itemDom.container.addClass('fixedHeader-container');

} else {
itemDom.container.empty();
}

var itemScroll = $('<div></div>');
itemScroll
.addClass('fixedHeader-scroll')
.appendTo( itemDom.container );

itemDom.floating = $( dt.table().node().cloneNode( false ) )
.css( 'table-layout', 'fixed' )
.removeAttr( 'id' )
.append( itemElement )
.appendTo( itemScroll );

// Sync Scroll with scrollBody
var tableScrollBody = $(".dataTables_scrollBody");
tableScrollBody.scroll( function() {
itemScroll.scrollLeft( tableScrollBody.scrollLeft() );
});

var itemElementLeft = itemElement;
var itemElementRight = itemElement;

// Append left fixed column head
if ( fixedColumns.left > 0 ) {
var leftWrapper = $('<div></div>');
leftWrapper.addClass('DTFH_Wrapper DTFH_LeftWrapper');

var tableFloatingLeft = $( dt.table().node().cloneNode( false ) );
var floatingCloneLeft = tableFloatingLeft.clone( true )
.removeAttr('id')
.addClass('fixedHeader-floating dataTable-clone-left').css('width', 'auto');

itemElementLeft.clone( true ).appendTo( floatingCloneLeft );

floatingCloneLeft.find('th:gt('+ (fixedColumns.left - 1) +')').remove();
floatingCloneLeft.appendTo( leftWrapper );

leftWrapper.appendTo( itemDom.container );
}

// Append right fixed column head
if ( fixedColumns.right > 0 ) {
var rightWrapper = $('<div></div>');
rightWrapper.addClass('DTFH_Wrapper DTFH_RightWrapper');

var tableFloatingRight = $( dt.table().node().cloneNode( false ) );
var floatingCloneRight = tableFloatingRight.clone()
.removeAttr('id')
.addClass('fixedHeader-floating dataTable-clone-right').css('width', 'auto');

itemElementRight.clone( true ).appendTo( floatingCloneRight );

var elementToRemove = floatingCloneRight.find('th:lt(-'+ fixedColumns.right +')');
elementToRemove.remove();
floatingCloneRight.appendTo( rightWrapper );

rightWrapper.appendTo( itemDom.container );
}

itemDom.container.appendTo('body');

} else {
itemDom.floating = $( dt.table().node().cloneNode( false ) )
.css( 'table-layout', 'fixed' )
.removeAttr( 'id' )
.append( itemElement )
.appendTo( 'body' );
}

// Insert a fake thead/tfoot into the DataTable to stop it jumping around
itemDom.placeholder = itemElement.clone( false );
Expand Down Expand Up @@ -392,6 +467,11 @@ $.extend( FixedHeader.prototype, {
var dt = this.s.dt;
var itemDom = this.dom[ item ];
var position = this.s.position;
var table = dt.table();
var tableNode = $( dt.table().node() );
var tableParent = tableNode.parent();
var tableParentWidth = tableParent.width();
var tableParentLeft = tableParent.offset().left;

// Record focus. Browser's will cause input elements to loose focus if
// they are inserted else where in the doc
Expand Down Expand Up @@ -419,6 +499,10 @@ $.extend( FixedHeader.prototype, {
if ( itemDom.floating ) {
itemDom.floating.remove();
itemDom.floating = null;
if ( itemDom.container ) {
itemDom.container.remove();
itemDom.container = null;
}
}
}
else if ( mode === 'in' ) {
Expand All @@ -432,6 +516,13 @@ $.extend( FixedHeader.prototype, {
.css( 'left', position.left+'px' )
.css( 'width', position.width+'px' );

if ( itemDom.container ) {
itemDom.container
.css( item === 'header' ? 'top' : 'bottom', this.c[item+'Offset'] )
.css( 'left', tableParentLeft+'px' )
.css( 'width', tableParentWidth+'px' );
}

if ( item === 'footer' ) {
itemDom.floating.css( 'top', '' );
}
Expand Down Expand Up @@ -582,7 +673,12 @@ FixedHeader.defaults = {
header: true,
footer: false,
headerOffset: 0,
footerOffset: 0
footerOffset: 0,
fixedColumns: {
isFixed: false,
left: 0,
right: 0
}
};


Expand Down