| 1 |
|
| 2 |
e |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
(function(window, document, undefined) { |
| 27 |
|
| 28 |
|
| 29 |
var factory = function( $, DataTable ) { |
| 30 |
"use strict"; |
| 31 |
|
| 32 |
var _instCounter = 0; |
| 33 |
|
| 34 |
var FixedHeader = function ( dt, config ) { |
| 35 |
|
| 36 |
if ( ! (this instanceof FixedHeader) ) { |
| 37 |
throw "FixedHeader must be initialised with the 'new' keyword."; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
if ( config === true ) { |
| 42 |
config = {}; |
| 43 |
} |
| 44 |
|
| 45 |
dt = new DataTable.Api( dt ); |
| 46 |
|
| 47 |
this.c = $.extend( true, {}, FixedHeader.defaults, config ); |
| 48 |
|
| 49 |
this.s = { |
| 50 |
dt: dt, |
| 51 |
position: { |
| 52 |
theadTop: 0, |
| 53 |
tbodyTop: 0, |
| 54 |
tfootTop: 0, |
| 55 |
tfootBottom: 0, |
| 56 |
width: 0, |
| 57 |
left: 0, |
| 58 |
tfootHeight: 0, |
| 59 |
theadHeight: 0, |
| 60 |
windowHeight: $(window).height(), |
| 61 |
visible: true |
| 62 |
}, |
| 63 |
headerMode: null, |
| 64 |
footerMode: null, |
| 65 |
namespace: '.dtfc'+(_instCounter++) |
| 66 |
}; |
| 67 |
|
| 68 |
this.dom = { |
| 69 |
floatingHeader: null, |
| 70 |
thead: $(dt.table().header()), |
| 71 |
tbody: $(dt.table().body()), |
| 72 |
tfoot: $(dt.table().footer()), |
| 73 |
header: { |
| 74 |
host: null, |
| 75 |
floating: null, |
| 76 |
placeholder: null |
| 77 |
}, |
| 78 |
footer: { |
| 79 |
host: null, |
| 80 |
floating: null, |
| 81 |
placeholder: null |
| 82 |
} |
| 83 |
}; |
| 84 |
|
| 85 |
this.dom.header.host = this.dom.thead.parent(); |
| 86 |
this.dom.footer.host = this.dom.tfoot.parent(); |
| 87 |
|
| 88 |
var dtSettings = dt.settings()[0]; |
| 89 |
if ( dtSettings._fixedHeader ) { |
| 90 |
throw "FixedHeader already initialised on table "+dtSettings.nTable.id; |
| 91 |
} |
| 92 |
|
| 93 |
dtSettings._fixedHeader = this; |
| 94 |
|
| 95 |
this._constructor(); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
FixedHeader.prototype = { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
update: function () { |
| 113 |
this._positions(); |
| 114 |
this._scroll( true ); |
| 115 |
}, |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
_constructor: function () |
| 129 |
{ |
| 130 |
var that = this; |
| 131 |
var dt = this.s.dt; |
| 132 |
|
| 133 |
$(window) |
| 134 |
.on( 'scroll'+this.s.namespace, function () { |
| 135 |
that._scroll(); |
| 136 |
} ) |
| 137 |
.on( 'resize'+this.s.namespace, function () { |
| 138 |
that.s.position.windowHeight = $(window).height(); |
| 139 |
that._positions(); |
| 140 |
that._scroll( true ); |
| 141 |
} ); |
| 142 |
|
| 143 |
dt |
| 144 |
.on( 'column-reorder.dt.dtfc column-visibility.dt.dtfc', function () { |
| 145 |
that._positions(); |
| 146 |
that._scroll( true ); |
| 147 |
} ) |
| 148 |
.on( 'draw.dtfc', function () { |
| 149 |
that._positions(); |
| 150 |
that._scroll(); |
| 151 |
} ); |
| 152 |
|
| 153 |
dt.on( 'destroy.dtfc', function () { |
| 154 |
dt.off( '.dtfc' ); |
| 155 |
$(window).off( this.s.namespace ); |
| 156 |
} ); |
| 157 |
|
| 158 |
this._positions(); |
| 159 |
this._scroll(); |
| 160 |
}, |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
_clone: function ( item, force ) |
| 178 |
{ |
| 179 |
var dt = this.s.dt; |
| 180 |
var itemDom = this.dom[ item ]; |
| 181 |
var itemElement = item === 'header' ? |
| 182 |
this.dom.thead : |
| 183 |
this.dom.tfoot; |
| 184 |
|
| 185 |
if ( ! force && itemDom.floating ) { |
| 186 |
|
| 187 |
itemDom.floating.removeClass( 'fixedHeader-floating fixedHeader-locked' ); |
| 188 |
} |
| 189 |
else { |
| 190 |
if ( itemDom.floating ) { |
| 191 |
itemDom.placeholder.remove(); |
| 192 |
itemDom.floating.children().detach(); |
| 193 |
itemDom.floating.remove(); |
| 194 |
} |
| 195 |
|
| 196 |
itemDom.floating = $( dt.table().node().cloneNode( false ) ) |
| 197 |
.removeAttr( 'id' ) |
| 198 |
.append( itemElement ) |
| 199 |
.appendTo( 'body' ); |
| 200 |
|
| 201 |
|
| 202 |
itemDom.placeholder = itemElement.clone( false ); |
| 203 |
itemDom.host.append( itemDom.placeholder ); |
| 204 |
|
| 205 |
|
| 206 |
if ( item === 'footer' ) { |
| 207 |
this._footerMatch( itemDom.placeholder, itemDom.floating ); |
| 208 |
} |
| 209 |
} |
| 210 |
}, |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
_footerMatch: function ( from, to ) { |
| 223 |
var type = function ( name ) { |
| 224 |
var toWidths = $(name, from) |
| 225 |
.map( function () { |
| 226 |
return $(this).width(); |
| 227 |
} ).toArray(); |
| 228 |
|
| 229 |
$(name, to).each( function ( i ) { |
| 230 |
$(this).width( toWidths[i] ); |
| 231 |
} ); |
| 232 |
}; |
| 233 |
|
| 234 |
type( 'th' ); |
| 235 |
type( 'td' ); |
| 236 |
}, |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
_footerUnsize: function () { |
| 246 |
var footer = this.dom.footer.floating; |
| 247 |
|
| 248 |
if ( footer ) { |
| 249 |
$('th, td', footer).css( 'width', '' ); |
| 250 |
} |
| 251 |
}, |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
_modeChange: function ( mode, item, forceChange ) |
| 269 |
{ |
| 270 |
var dt = this.s.dt; |
| 271 |
var itemDom = this.dom[ item ]; |
| 272 |
var position = this.s.position; |
| 273 |
|
| 274 |
if ( mode === 'in-place' ) { |
| 275 |
|
| 276 |
if ( itemDom.placeholder ) { |
| 277 |
itemDom.placeholder.remove(); |
| 278 |
itemDom.placeholder = null; |
| 279 |
} |
| 280 |
|
| 281 |
itemDom.host.append( item === 'header' ? |
| 282 |
this.dom.thead : |
| 283 |
this.dom.tfoot |
| 284 |
); |
| 285 |
|
| 286 |
if ( itemDom.floating ) { |
| 287 |
itemDom.floating.remove(); |
| 288 |
itemDom.floating = null; |
| 289 |
} |
| 290 |
|
| 291 |
if ( item === 'footer' ) { |
| 292 |
this._footerUnsize(); |
| 293 |
} |
| 294 |
} |
| 295 |
else if ( mode === 'in' ) { |
| 296 |
|
| 297 |
|
| 298 |
this._clone( item, forceChange ); |
| 299 |
|
| 300 |
itemDom.floating |
| 301 |
.addClass( 'fixedHeader-floating' ) |
| 302 |
.css( item === 'header' ? 'top' : 'bottom', this.c[item+'Offset'] ) |
| 303 |
.css( 'left', position.left+'px' ) |
| 304 |
.css( 'width', position.width+'px' ); |
| 305 |
|
| 306 |
if ( item === 'footer' ) { |
| 307 |
itemDom.floating.css( 'top', '' ); |
| 308 |
} |
| 309 |
} |
| 310 |
else if ( mode === 'below' ) { |
| 311 |
|
| 312 |
this._clone( item, forceChange ); |
| 313 |
|
| 314 |
itemDom.floating |
| 315 |
.addClass( 'fixedHeader-locked' ) |
| 316 |
.css( 'top', position.tfootTop - position.theadHeight ) |
| 317 |
.css( 'left', position.left+'px' ) |
| 318 |
.css( 'width', position.width+'px' ); |
| 319 |
} |
| 320 |
else if ( mode === 'above' ) { |
| 321 |
|
| 322 |
this._clone( item, forceChange ); |
| 323 |
|
| 324 |
itemDom.floating |
| 325 |
.addClass( 'fixedHeader-locked' ) |
| 326 |
.css( 'top', position.tbodyTop ) |
| 327 |
.css( 'left', position.left+'px' ) |
| 328 |
.css( 'width', position.width+'px' ); |
| 329 |
} |
| 330 |
|
| 331 |
this.s[item+'Mode'] = mode; |
| 332 |
}, |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
_positions: function () |
| 341 |
{ |
| 342 |
var dt = this.s.dt; |
| 343 |
var table = dt.table(); |
| 344 |
var position = this.s.position; |
| 345 |
var dom = this.dom; |
| 346 |
var tableNode = $(table.node()); |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
var thead = tableNode.children('thead'); |
| 352 |
var tfoot = tableNode.children('tfoot'); |
| 353 |
var tbody = dom.tbody; |
| 354 |
|
| 355 |
position.visible = tableNode.is(':visible'); |
| 356 |
position.width = tableNode.outerWidth(); |
| 357 |
position.left = tableNode.offset().left; |
| 358 |
position.theadTop = thead.offset().top; |
| 359 |
position.tbodyTop = tbody.offset().top; |
| 360 |
position.theadHeight = position.tbodyTop - position.theadTop; |
| 361 |
|
| 362 |
if ( tfoot.length ) { |
| 363 |
position.tfootTop = tfoot.offset().top; |
| 364 |
position.tfootBottom = position.tfootTop + tfoot.outerHeight(); |
| 365 |
position.tfootHeight = position.tfootBottom - position.tfootTop; |
| 366 |
} |
| 367 |
else { |
| 368 |
position.tfootTop = position.tbodyTop + tbody.outerHeight(); |
| 369 |
position.tfootBottom = position.tfootTop; |
| 370 |
position.tfootHeight = position.tfootTop; |
| 371 |
} |
| 372 |
}, |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
_scroll: function ( forceChange ) |
| 384 |
{ |
| 385 |
var windowTop = $(document).scrollTop(); |
| 386 |
var position = this.s.position; |
| 387 |
var headerMode, footerMode; |
| 388 |
|
| 389 |
if ( this.c.header ) { |
| 390 |
if ( ! position.visible || windowTop <= position.theadTop - this.c.headerOffset ) { |
| 391 |
headerMode = 'in-place'; |
| 392 |
} |
| 393 |
else if ( windowTop <= position.tfootTop - position.theadHeight - this.c.headerOffset ) { |
| 394 |
headerMode = 'in'; |
| 395 |
} |
| 396 |
else { |
| 397 |
headerMode = 'below'; |
| 398 |
} |
| 399 |
|
| 400 |
if ( forceChange || headerMode !== this.s.headerMode ) { |
| 401 |
this._modeChange( headerMode, 'header', forceChange ); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
if ( this.c.footer && this.dom.tfoot.length ) { |
| 406 |
if ( ! position.visible || windowTop + position.windowHeight >= position.tfootBottom + this.c.footerOffset ) { |
| 407 |
footerMode = 'in-place'; |
| 408 |
} |
| 409 |
else if ( position.windowHeight + windowTop > position.tbodyTop + position.tfootHeight + this.c.footerOffset ) { |
| 410 |
footerMode = 'in'; |
| 411 |
} |
| 412 |
else { |
| 413 |
footerMode = 'above'; |
| 414 |
} |
| 415 |
|
| 416 |
if ( forceChange || footerMode !== this.s.footerMode ) { |
| 417 |
this._modeChange( footerMode, 'footer', forceChange ); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
}; |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
FixedHeader.version = "3.0.0"; |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
FixedHeader.defaults = { |
| 437 |
header: true, |
| 438 |
footer: false, |
| 439 |
headerOffset: 0, |
| 440 |
footerOffset: 0 |
| 441 |
}; |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
$.fn.dataTable.FixedHeader = FixedHeader; |
| 450 |
$.fn.DataTable.FixedHeader = FixedHeader; |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
$(document).on( 'init.dt.dtb', function (e, settings, json) { |
| 456 |
if ( e.namespace !== 'dt' ) { |
| 457 |
return; |
| 458 |
} |
| 459 |
|
| 460 |
var opts = settings.oInit.fixedHeader || DataTable.defaults.fixedHeader; |
| 461 |
|
| 462 |
if ( opts && ! settings._buttons ) { |
| 463 |
new FixedHeader( settings, opts ); |
| 464 |
} |
| 465 |
} ); |
| 466 |
|
| 467 |
|
| 468 |
DataTable.Api.register( 'fixedHeader()', function () {} ); |
| 469 |
|
| 470 |
DataTable.Api.register( 'fixedHeader.adjust()', function () { |
| 471 |
return this.iterator( 'table', function ( ctx ) { |
| 472 |
var fh = ctx._fixedHeader; |
| 473 |
|
| 474 |
if ( fh ) { |
| 475 |
fh.update(); |
| 476 |
} |
| 477 |
} ); |
| 478 |
} ); |
| 479 |
|
| 480 |
|
| 481 |
return FixedHeader; |
| 482 |
}; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
if ( typeof define === 'function' && define.amd ) { |
| 487 |
define( ['jquery', 'datatables'], factory ); |
| 488 |
} |
| 489 |
else if ( typeof exports === 'object' ) { |
| 490 |
|
| 491 |
factory( require('jquery'), require('datatables') ); |
| 492 |
} |
| 493 |
else if ( jQuery && !jQuery.fn.dataTable.FixedHeader ) { |
| 494 |
|
| 495 |
factory( jQuery, jQuery.fn.dataTable ); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
})(window, document); |