1
0

dataTables.fixedHeader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. /*! FixedHeader 3.1.6-dev
  2. * ©2009-2018 SpryMedia Ltd - datatables.net/license
  3. */
  4. /**
  5. * @summary FixedHeader
  6. * @description Fix a table's header or footer, so it is always visible while
  7. * scrolling
  8. * @version 3.1.6-dev
  9. * @file dataTables.fixedHeader.js
  10. * @author SpryMedia Ltd (www.sprymedia.co.uk)
  11. * @contact www.sprymedia.co.uk/contact
  12. * @copyright Copyright 2009-2018 SpryMedia Ltd.
  13. *
  14. * This source file is free software, available under the following license:
  15. * MIT license - http://datatables.net/license/mit
  16. *
  17. * This source file is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  19. * or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
  20. *
  21. * For details please refer to: http://www.datatables.net
  22. */
  23. (function( factory ){
  24. if ( typeof define === 'function' && define.amd ) {
  25. // AMD
  26. define( ['jquery', 'datatables.net'], function ( $ ) {
  27. return factory( $, window, document );
  28. } );
  29. }
  30. else if ( typeof exports === 'object' ) {
  31. // CommonJS
  32. module.exports = function (root, $) {
  33. if ( ! root ) {
  34. root = window;
  35. }
  36. if ( ! $ || ! $.fn.dataTable ) {
  37. $ = require('datatables.net')(root, $).$;
  38. }
  39. return factory( $, root, root.document );
  40. };
  41. }
  42. else {
  43. // Browser
  44. factory( jQuery, window, document );
  45. }
  46. }(function( $, window, document, undefined ) {
  47. 'use strict';
  48. var DataTable = $.fn.dataTable;
  49. var _instCounter = 0;
  50. var FixedHeader = function ( dt, config ) {
  51. // Sanity check - you just know it will happen
  52. if ( ! (this instanceof FixedHeader) ) {
  53. throw "FixedHeader must be initialised with the 'new' keyword.";
  54. }
  55. // Allow a boolean true for defaults
  56. if ( config === true ) {
  57. config = {};
  58. }
  59. dt = new DataTable.Api( dt );
  60. this.c = $.extend( true, {}, FixedHeader.defaults, config );
  61. this.s = {
  62. dt: dt,
  63. position: {
  64. theadTop: 0,
  65. tbodyTop: 0,
  66. tfootTop: 0,
  67. tfootBottom: 0,
  68. width: 0,
  69. left: 0,
  70. tfootHeight: 0,
  71. theadHeight: 0,
  72. windowHeight: $(window).height(),
  73. visible: true
  74. },
  75. headerMode: null,
  76. footerMode: null,
  77. autoWidth: dt.settings()[0].oFeatures.bAutoWidth,
  78. namespace: '.dtfc'+(_instCounter++),
  79. scrollLeft: {
  80. header: -1,
  81. footer: -1
  82. },
  83. enable: true
  84. };
  85. this.dom = {
  86. floatingHeader: null,
  87. thead: $(dt.table().header()),
  88. tbody: $(dt.table().body()),
  89. tfoot: $(dt.table().footer()),
  90. header: {
  91. host: null,
  92. floating: null,
  93. placeholder: null
  94. },
  95. footer: {
  96. host: null,
  97. floating: null,
  98. placeholder: null
  99. }
  100. };
  101. this.dom.header.host = this.dom.thead.parent();
  102. this.dom.footer.host = this.dom.tfoot.parent();
  103. var dtSettings = dt.settings()[0];
  104. if ( dtSettings._fixedHeader ) {
  105. throw "FixedHeader already initialised on table "+dtSettings.nTable.id;
  106. }
  107. dtSettings._fixedHeader = this;
  108. this._constructor();
  109. };
  110. /*
  111. * Variable: FixedHeader
  112. * Purpose: Prototype for FixedHeader
  113. * Scope: global
  114. */
  115. $.extend( FixedHeader.prototype, {
  116. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  117. * API methods
  118. */
  119. /**
  120. * Kill off FH and any events
  121. */
  122. destroy: function () {
  123. this.s.dt.off( '.dtfc' );
  124. $(window).off( this.s.namespace );
  125. if ( this.c.header ) {
  126. this._modeChange( 'in-place', 'header', true );
  127. }
  128. if ( this.c.footer && this.dom.tfoot.length ) {
  129. this._modeChange( 'in-place', 'footer', true );
  130. }
  131. },
  132. /**
  133. * Enable / disable the fixed elements
  134. *
  135. * @param {boolean} enable `true` to enable, `false` to disable
  136. */
  137. enable: function ( enable, update )
  138. {
  139. this.s.enable = enable;
  140. if ( update || update === undefined ) {
  141. this._positions();
  142. this._scroll( true );
  143. }
  144. },
  145. /**
  146. * Get enabled status
  147. */
  148. enabled: function ()
  149. {
  150. return this.s.enable;
  151. },
  152. /**
  153. * Set header offset
  154. *
  155. * @param {int} new value for headerOffset
  156. */
  157. headerOffset: function ( offset )
  158. {
  159. if ( offset !== undefined ) {
  160. this.c.headerOffset = offset;
  161. this.update();
  162. }
  163. return this.c.headerOffset;
  164. },
  165. /**
  166. * Set footer offset
  167. *
  168. * @param {int} new value for footerOffset
  169. */
  170. footerOffset: function ( offset )
  171. {
  172. if ( offset !== undefined ) {
  173. this.c.footerOffset = offset;
  174. this.update();
  175. }
  176. return this.c.footerOffset;
  177. },
  178. /**
  179. * Recalculate the position of the fixed elements and force them into place
  180. */
  181. update: function ()
  182. {
  183. var table = this.s.dt.table().node();
  184. if ( $(table).is(':visible') ) {
  185. this.enable( true, false );
  186. }
  187. else {
  188. this.enable( false, false );
  189. }
  190. this._positions();
  191. this._scroll( true );
  192. },
  193. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  194. * Constructor
  195. */
  196. /**
  197. * FixedHeader constructor - adding the required event listeners and
  198. * simple initialisation
  199. *
  200. * @private
  201. */
  202. _constructor: function ()
  203. {
  204. var that = this;
  205. var dt = this.s.dt;
  206. $(window)
  207. .on( 'scroll'+this.s.namespace, function () {
  208. that._scroll();
  209. } )
  210. .on( 'resize'+this.s.namespace, DataTable.util.throttle( function () {
  211. that.s.position.windowHeight = $(window).height();
  212. that.update();
  213. }, 50 ) );
  214. var autoHeader = $('.fh-fixedHeader');
  215. if ( ! this.c.headerOffset && autoHeader.length ) {
  216. this.c.headerOffset = autoHeader.outerHeight();
  217. }
  218. var autoFooter = $('.fh-fixedFooter');
  219. if ( ! this.c.footerOffset && autoFooter.length ) {
  220. this.c.footerOffset = autoFooter.outerHeight();
  221. }
  222. dt.on( 'column-reorder.dt.dtfc column-visibility.dt.dtfc draw.dt.dtfc column-sizing.dt.dtfc responsive-display.dt.dtfc', function () {
  223. that.update();
  224. } );
  225. dt.on( 'destroy.dtfc', function () {
  226. that.destroy();
  227. } );
  228. this._positions();
  229. this._scroll();
  230. },
  231. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  232. * Private methods
  233. */
  234. /**
  235. * Clone a fixed item to act as a place holder for the original element
  236. * which is moved into a clone of the table element, and moved around the
  237. * document to give the fixed effect.
  238. *
  239. * @param {string} item 'header' or 'footer'
  240. * @param {boolean} force Force the clone to happen, or allow automatic
  241. * decision (reuse existing if available)
  242. * @private
  243. */
  244. _clone: function ( item, force )
  245. {
  246. var dt = this.s.dt;
  247. var itemDom = this.dom[ item ];
  248. var itemElement = item === 'header' ?
  249. this.dom.thead :
  250. this.dom.tfoot;
  251. if ( ! force && itemDom.floating ) {
  252. // existing floating element - reuse it
  253. itemDom.floating.removeClass( 'fixedHeader-floating fixedHeader-locked' );
  254. }
  255. else {
  256. if ( itemDom.floating ) {
  257. itemDom.placeholder.remove();
  258. this._unsize( item );
  259. itemDom.floating.children().detach();
  260. itemDom.floating.remove();
  261. }
  262. itemDom.floating = $( dt.table().node().cloneNode( false ) )
  263. .css( 'table-layout', 'fixed' )
  264. .attr( 'aria-hidden', 'true' )
  265. .removeAttr( 'id' )
  266. .append( itemElement )
  267. .appendTo( 'body' );
  268. // Insert a fake thead/tfoot into the DataTable to stop it jumping around
  269. itemDom.placeholder = itemElement.clone( false );
  270. itemDom.placeholder
  271. .find( '*[id]' )
  272. .removeAttr( 'id' );
  273. itemDom.host.prepend( itemDom.placeholder );
  274. // Clone widths
  275. this._matchWidths( itemDom.placeholder, itemDom.floating );
  276. }
  277. },
  278. /**
  279. * Copy widths from the cells in one element to another. This is required
  280. * for the footer as the footer in the main table takes its sizes from the
  281. * header columns. That isn't present in the footer so to have it still
  282. * align correctly, the sizes need to be copied over. It is also required
  283. * for the header when auto width is not enabled
  284. *
  285. * @param {jQuery} from Copy widths from
  286. * @param {jQuery} to Copy widths to
  287. * @private
  288. */
  289. _matchWidths: function ( from, to ) {
  290. var get = function ( name ) {
  291. return $(name, from)
  292. .map( function () {
  293. return $(this).width();
  294. } ).toArray();
  295. };
  296. var set = function ( name, toWidths ) {
  297. $(name, to).each( function ( i ) {
  298. $(this).css( {
  299. width: toWidths[i],
  300. minWidth: toWidths[i]
  301. } );
  302. } );
  303. };
  304. var thWidths = get( 'th' );
  305. var tdWidths = get( 'td' );
  306. set( 'th', thWidths );
  307. set( 'td', tdWidths );
  308. },
  309. /**
  310. * Remove assigned widths from the cells in an element. This is required
  311. * when inserting the footer back into the main table so the size is defined
  312. * by the header columns and also when auto width is disabled in the
  313. * DataTable.
  314. *
  315. * @param {string} item The `header` or `footer`
  316. * @private
  317. */
  318. _unsize: function ( item ) {
  319. var el = this.dom[ item ].floating;
  320. if ( el && (item === 'footer' || (item === 'header' && ! this.s.autoWidth)) ) {
  321. $('th, td', el).css( {
  322. width: '',
  323. minWidth: ''
  324. } );
  325. }
  326. else if ( el && item === 'header' ) {
  327. $('th, td', el).css( 'min-width', '' );
  328. }
  329. },
  330. /**
  331. * Reposition the floating elements to take account of horizontal page
  332. * scroll
  333. *
  334. * @param {string} item The `header` or `footer`
  335. * @param {int} scrollLeft Document scrollLeft
  336. * @private
  337. */
  338. _horizontal: function ( item, scrollLeft )
  339. {
  340. var itemDom = this.dom[ item ];
  341. var position = this.s.position;
  342. var lastScrollLeft = this.s.scrollLeft;
  343. if ( itemDom.floating && lastScrollLeft[ item ] !== scrollLeft ) {
  344. itemDom.floating.css( 'left', position.left - scrollLeft );
  345. lastScrollLeft[ item ] = scrollLeft;
  346. }
  347. },
  348. /**
  349. * Change from one display mode to another. Each fixed item can be in one
  350. * of:
  351. *
  352. * * `in-place` - In the main DataTable
  353. * * `in` - Floating over the DataTable
  354. * * `below` - (Header only) Fixed to the bottom of the table body
  355. * * `above` - (Footer only) Fixed to the top of the table body
  356. *
  357. * @param {string} mode Mode that the item should be shown in
  358. * @param {string} item 'header' or 'footer'
  359. * @param {boolean} forceChange Force a redraw of the mode, even if already
  360. * in that mode.
  361. * @private
  362. */
  363. _modeChange: function ( mode, item, forceChange )
  364. {
  365. var dt = this.s.dt;
  366. var itemDom = this.dom[ item ];
  367. var position = this.s.position;
  368. // Record focus. Browser's will cause input elements to loose focus if
  369. // they are inserted else where in the doc
  370. var tablePart = this.dom[ item==='footer' ? 'tfoot' : 'thead' ];
  371. var focus = $.contains( tablePart[0], document.activeElement ) ?
  372. document.activeElement :
  373. null;
  374. if ( focus ) {
  375. focus.blur();
  376. }
  377. if ( mode === 'in-place' ) {
  378. // Insert the header back into the table's real header
  379. if ( itemDom.placeholder ) {
  380. itemDom.placeholder.remove();
  381. itemDom.placeholder = null;
  382. }
  383. this._unsize( item );
  384. if ( item === 'header' ) {
  385. itemDom.host.prepend( tablePart );
  386. }
  387. else {
  388. itemDom.host.append( tablePart );
  389. }
  390. if ( itemDom.floating ) {
  391. itemDom.floating.remove();
  392. itemDom.floating = null;
  393. }
  394. }
  395. else if ( mode === 'in' ) {
  396. // Remove the header from the read header and insert into a fixed
  397. // positioned floating table clone
  398. this._clone( item, forceChange );
  399. itemDom.floating
  400. .addClass( 'fixedHeader-floating' )
  401. .css( item === 'header' ? 'top' : 'bottom', this.c[item+'Offset'] )
  402. .css( 'left', position.left+'px' )
  403. .css( 'width', position.width+'px' );
  404. if ( item === 'footer' ) {
  405. itemDom.floating.css( 'top', '' );
  406. }
  407. }
  408. else if ( mode === 'below' ) { // only used for the header
  409. // Fix the position of the floating header at base of the table body
  410. this._clone( item, forceChange );
  411. itemDom.floating
  412. .addClass( 'fixedHeader-locked' )
  413. .css( 'top', position.tfootTop - position.theadHeight )
  414. .css( 'left', position.left+'px' )
  415. .css( 'width', position.width+'px' );
  416. }
  417. else if ( mode === 'above' ) { // only used for the footer
  418. // Fix the position of the floating footer at top of the table body
  419. this._clone( item, forceChange );
  420. itemDom.floating
  421. .addClass( 'fixedHeader-locked' )
  422. .css( 'top', position.tbodyTop )
  423. .css( 'left', position.left+'px' )
  424. .css( 'width', position.width+'px' );
  425. }
  426. // Restore focus if it was lost
  427. if ( focus && focus !== document.activeElement ) {
  428. setTimeout( function () {
  429. focus.focus();
  430. }, 10 );
  431. }
  432. this.s.scrollLeft.header = -1;
  433. this.s.scrollLeft.footer = -1;
  434. this.s[item+'Mode'] = mode;
  435. },
  436. /**
  437. * Cache the positional information that is required for the mode
  438. * calculations that FixedHeader performs.
  439. *
  440. * @private
  441. */
  442. _positions: function ()
  443. {
  444. var dt = this.s.dt;
  445. var table = dt.table();
  446. var position = this.s.position;
  447. var dom = this.dom;
  448. var tableNode = $(table.node());
  449. // Need to use the header and footer that are in the main table,
  450. // regardless of if they are clones, since they hold the positions we
  451. // want to measure from
  452. var thead = tableNode.children('thead');
  453. var tfoot = tableNode.children('tfoot');
  454. var tbody = dom.tbody;
  455. position.visible = tableNode.is(':visible');
  456. position.width = tableNode.outerWidth();
  457. position.left = tableNode.offset().left;
  458. position.theadTop = thead.offset().top;
  459. position.tbodyTop = tbody.offset().top;
  460. position.tbodyHeight = tbody.outerHeight();
  461. position.theadHeight = position.tbodyTop - position.theadTop;
  462. if ( tfoot.length ) {
  463. position.tfootTop = tfoot.offset().top;
  464. position.tfootBottom = position.tfootTop + tfoot.outerHeight();
  465. position.tfootHeight = position.tfootBottom - position.tfootTop;
  466. }
  467. else {
  468. position.tfootTop = position.tbodyTop + tbody.outerHeight();
  469. position.tfootBottom = position.tfootTop;
  470. position.tfootHeight = position.tfootTop;
  471. }
  472. },
  473. /**
  474. * Mode calculation - determine what mode the fixed items should be placed
  475. * into.
  476. *
  477. * @param {boolean} forceChange Force a redraw of the mode, even if already
  478. * in that mode.
  479. * @private
  480. */
  481. _scroll: function ( forceChange )
  482. {
  483. var windowTop = $(document).scrollTop();
  484. var windowLeft = $(document).scrollLeft();
  485. var position = this.s.position;
  486. var headerMode, footerMode;
  487. if ( this.c.header ) {
  488. if ( ! this.s.enable ) {
  489. headerMode = 'in-place';
  490. }
  491. else if ( ! position.visible || windowTop <= position.theadTop - this.c.headerOffset ) {
  492. headerMode = 'in-place';
  493. }
  494. else if ( windowTop <= position.tfootTop - position.theadHeight - this.c.headerOffset ) {
  495. headerMode = 'in';
  496. }
  497. else {
  498. headerMode = 'below';
  499. }
  500. if ( forceChange || headerMode !== this.s.headerMode ) {
  501. this._modeChange( headerMode, 'header', forceChange );
  502. }
  503. this._horizontal( 'header', windowLeft );
  504. }
  505. if ( this.c.footer && this.dom.tfoot.length ) {
  506. if ( ! this.s.enable ) {
  507. headerMode = 'in-place';
  508. }
  509. else if ( ! position.visible || windowTop + position.windowHeight >= position.tfootBottom + this.c.footerOffset ) {
  510. footerMode = 'in-place';
  511. }
  512. else if ( position.windowHeight + windowTop > position.tbodyTop + position.tfootHeight + this.c.footerOffset ) {
  513. footerMode = 'in';
  514. }
  515. else {
  516. footerMode = 'above';
  517. }
  518. if ( forceChange || footerMode !== this.s.footerMode ) {
  519. this._modeChange( footerMode, 'footer', forceChange );
  520. }
  521. this._horizontal( 'footer', windowLeft );
  522. }
  523. }
  524. } );
  525. /**
  526. * Version
  527. * @type {String}
  528. * @static
  529. */
  530. FixedHeader.version = "3.1.6-dev";
  531. /**
  532. * Defaults
  533. * @type {Object}
  534. * @static
  535. */
  536. FixedHeader.defaults = {
  537. header: true,
  538. footer: false,
  539. headerOffset: 0,
  540. footerOffset: 0
  541. };
  542. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  543. * DataTables interfaces
  544. */
  545. // Attach for constructor access
  546. $.fn.dataTable.FixedHeader = FixedHeader;
  547. $.fn.DataTable.FixedHeader = FixedHeader;
  548. // DataTables creation - check if the FixedHeader option has been defined on the
  549. // table and if so, initialise
  550. $(document).on( 'init.dt.dtfh', function (e, settings, json) {
  551. if ( e.namespace !== 'dt' ) {
  552. return;
  553. }
  554. var init = settings.oInit.fixedHeader;
  555. var defaults = DataTable.defaults.fixedHeader;
  556. if ( (init || defaults) && ! settings._fixedHeader ) {
  557. var opts = $.extend( {}, defaults, init );
  558. if ( init !== false ) {
  559. new FixedHeader( settings, opts );
  560. }
  561. }
  562. } );
  563. // DataTables API methods
  564. DataTable.Api.register( 'fixedHeader()', function () {} );
  565. DataTable.Api.register( 'fixedHeader.adjust()', function () {
  566. return this.iterator( 'table', function ( ctx ) {
  567. var fh = ctx._fixedHeader;
  568. if ( fh ) {
  569. fh.update();
  570. }
  571. } );
  572. } );
  573. DataTable.Api.register( 'fixedHeader.enable()', function ( flag ) {
  574. return this.iterator( 'table', function ( ctx ) {
  575. var fh = ctx._fixedHeader;
  576. flag = ( flag !== undefined ? flag : true );
  577. if ( fh && flag !== fh.enabled() ) {
  578. fh.enable( flag );
  579. }
  580. } );
  581. } );
  582. DataTable.Api.register( 'fixedHeader.enabled()', function () {
  583. if ( this.context.length ) {
  584. var fx = this.content[0]._fixedHeader;
  585. if ( fh ) {
  586. return fh.enabled();
  587. }
  588. }
  589. return false;
  590. } );
  591. DataTable.Api.register( 'fixedHeader.disable()', function ( ) {
  592. return this.iterator( 'table', function ( ctx ) {
  593. var fh = ctx._fixedHeader;
  594. if ( fh && fh.enabled() ) {
  595. fh.enable( false );
  596. }
  597. } );
  598. } );
  599. $.each( ['header', 'footer'], function ( i, el ) {
  600. DataTable.Api.register( 'fixedHeader.'+el+'Offset()', function ( offset ) {
  601. var ctx = this.context;
  602. if ( offset === undefined ) {
  603. return ctx.length && ctx[0]._fixedHeader ?
  604. ctx[0]._fixedHeader[el +'Offset']() :
  605. undefined;
  606. }
  607. return this.iterator( 'table', function ( ctx ) {
  608. var fh = ctx._fixedHeader;
  609. if ( fh ) {
  610. fh[ el +'Offset' ]( offset );
  611. }
  612. } );
  613. } );
  614. } );
  615. return FixedHeader;
  616. }));