dataTables.fixedHeader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*! FixedHeader 3.1.4
  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.4
  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. * Enable / disable the fixed elements
  121. *
  122. * @param {boolean} enable `true` to enable, `false` to disable
  123. */
  124. enable: function ( enable )
  125. {
  126. this.s.enable = enable;
  127. if ( this.c.header ) {
  128. this._modeChange( 'in-place', 'header', true );
  129. }
  130. if ( this.c.footer && this.dom.tfoot.length ) {
  131. this._modeChange( 'in-place', 'footer', true );
  132. }
  133. this.update();
  134. },
  135. /**
  136. * Set header offset
  137. *
  138. * @param {int} new value for headerOffset
  139. */
  140. headerOffset: function ( offset )
  141. {
  142. if ( offset !== undefined ) {
  143. this.c.headerOffset = offset;
  144. this.update();
  145. }
  146. return this.c.headerOffset;
  147. },
  148. /**
  149. * Set footer offset
  150. *
  151. * @param {int} new value for footerOffset
  152. */
  153. footerOffset: function ( offset )
  154. {
  155. if ( offset !== undefined ) {
  156. this.c.footerOffset = offset;
  157. this.update();
  158. }
  159. return this.c.footerOffset;
  160. },
  161. /**
  162. * Recalculate the position of the fixed elements and force them into place
  163. */
  164. update: function ()
  165. {
  166. this._positions();
  167. this._scroll( true );
  168. },
  169. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  170. * Constructor
  171. */
  172. /**
  173. * FixedHeader constructor - adding the required event listeners and
  174. * simple initialisation
  175. *
  176. * @private
  177. */
  178. _constructor: function ()
  179. {
  180. var that = this;
  181. var dt = this.s.dt;
  182. $(window)
  183. .on( 'scroll'+this.s.namespace, function () {
  184. that._scroll();
  185. } )
  186. .on( 'resize'+this.s.namespace, DataTable.util.throttle( function () {
  187. that.s.position.windowHeight = $(window).height();
  188. that.update();
  189. }, 50 ) );
  190. var autoHeader = $('.fh-fixedHeader');
  191. if ( ! this.c.headerOffset && autoHeader.length ) {
  192. this.c.headerOffset = autoHeader.outerHeight();
  193. }
  194. var autoFooter = $('.fh-fixedFooter');
  195. if ( ! this.c.footerOffset && autoFooter.length ) {
  196. this.c.footerOffset = autoFooter.outerHeight();
  197. }
  198. dt.on( 'column-reorder.dt.dtfc column-visibility.dt.dtfc draw.dt.dtfc column-sizing.dt.dtfc responsive-display.dt.dtfc', function () {
  199. that.update();
  200. } );
  201. dt.on( 'destroy.dtfc', function () {
  202. if ( that.c.header ) {
  203. that._modeChange( 'in-place', 'header', true );
  204. }
  205. if ( that.c.footer && that.dom.tfoot.length ) {
  206. that._modeChange( 'in-place', 'footer', true );
  207. }
  208. dt.off( '.dtfc' );
  209. $(window).off( that.s.namespace );
  210. } );
  211. this._positions();
  212. this._scroll();
  213. },
  214. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  215. * Private methods
  216. */
  217. /**
  218. * Clone a fixed item to act as a place holder for the original element
  219. * which is moved into a clone of the table element, and moved around the
  220. * document to give the fixed effect.
  221. *
  222. * @param {string} item 'header' or 'footer'
  223. * @param {boolean} force Force the clone to happen, or allow automatic
  224. * decision (reuse existing if available)
  225. * @private
  226. */
  227. _clone: function ( item, force )
  228. {
  229. var dt = this.s.dt;
  230. var itemDom = this.dom[ item ];
  231. var itemElement = item === 'header' ?
  232. this.dom.thead :
  233. this.dom.tfoot;
  234. if ( ! force && itemDom.floating ) {
  235. // existing floating element - reuse it
  236. itemDom.floating.removeClass( 'fixedHeader-floating fixedHeader-locked' );
  237. }
  238. else {
  239. if ( itemDom.floating ) {
  240. itemDom.placeholder.remove();
  241. this._unsize( item );
  242. itemDom.floating.children().detach();
  243. itemDom.floating.remove();
  244. }
  245. itemDom.floating = $( dt.table().node().cloneNode( false ) )
  246. .css( 'table-layout', 'fixed' )
  247. .attr( 'aria-hidden', 'true' )
  248. .removeAttr( 'id' )
  249. .append( itemElement )
  250. .appendTo( 'body' );
  251. // Insert a fake thead/tfoot into the DataTable to stop it jumping around
  252. itemDom.placeholder = itemElement.clone( false )
  253. itemDom.placeholder
  254. .find( '*[id]' )
  255. .removeAttr( 'id' );
  256. itemDom.host.prepend( itemDom.placeholder );
  257. // Clone widths
  258. this._matchWidths( itemDom.placeholder, itemDom.floating );
  259. }
  260. },
  261. /**
  262. * Copy widths from the cells in one element to another. This is required
  263. * for the footer as the footer in the main table takes its sizes from the
  264. * header columns. That isn't present in the footer so to have it still
  265. * align correctly, the sizes need to be copied over. It is also required
  266. * for the header when auto width is not enabled
  267. *
  268. * @param {jQuery} from Copy widths from
  269. * @param {jQuery} to Copy widths to
  270. * @private
  271. */
  272. _matchWidths: function ( from, to ) {
  273. var get = function ( name ) {
  274. return $(name, from)
  275. .map( function () {
  276. return $(this).width();
  277. } ).toArray();
  278. };
  279. var set = function ( name, toWidths ) {
  280. $(name, to).each( function ( i ) {
  281. $(this).css( {
  282. width: toWidths[i],
  283. minWidth: toWidths[i]
  284. } );
  285. } );
  286. };
  287. var thWidths = get( 'th' );
  288. var tdWidths = get( 'td' );
  289. set( 'th', thWidths );
  290. set( 'td', tdWidths );
  291. },
  292. /**
  293. * Remove assigned widths from the cells in an element. This is required
  294. * when inserting the footer back into the main table so the size is defined
  295. * by the header columns and also when auto width is disabled in the
  296. * DataTable.
  297. *
  298. * @param {string} item The `header` or `footer`
  299. * @private
  300. */
  301. _unsize: function ( item ) {
  302. var el = this.dom[ item ].floating;
  303. if ( el && (item === 'footer' || (item === 'header' && ! this.s.autoWidth)) ) {
  304. $('th, td', el).css( {
  305. width: '',
  306. minWidth: ''
  307. } );
  308. }
  309. else if ( el && item === 'header' ) {
  310. $('th, td', el).css( 'min-width', '' );
  311. }
  312. },
  313. /**
  314. * Reposition the floating elements to take account of horizontal page
  315. * scroll
  316. *
  317. * @param {string} item The `header` or `footer`
  318. * @param {int} scrollLeft Document scrollLeft
  319. * @private
  320. */
  321. _horizontal: function ( item, scrollLeft )
  322. {
  323. var itemDom = this.dom[ item ];
  324. var position = this.s.position;
  325. var lastScrollLeft = this.s.scrollLeft;
  326. if ( itemDom.floating && lastScrollLeft[ item ] !== scrollLeft ) {
  327. itemDom.floating.css( 'left', position.left - scrollLeft );
  328. lastScrollLeft[ item ] = scrollLeft;
  329. }
  330. },
  331. /**
  332. * Change from one display mode to another. Each fixed item can be in one
  333. * of:
  334. *
  335. * * `in-place` - In the main DataTable
  336. * * `in` - Floating over the DataTable
  337. * * `below` - (Header only) Fixed to the bottom of the table body
  338. * * `above` - (Footer only) Fixed to the top of the table body
  339. *
  340. * @param {string} mode Mode that the item should be shown in
  341. * @param {string} item 'header' or 'footer'
  342. * @param {boolean} forceChange Force a redraw of the mode, even if already
  343. * in that mode.
  344. * @private
  345. */
  346. _modeChange: function ( mode, item, forceChange )
  347. {
  348. var dt = this.s.dt;
  349. var itemDom = this.dom[ item ];
  350. var position = this.s.position;
  351. // Record focus. Browser's will cause input elements to loose focus if
  352. // they are inserted else where in the doc
  353. var tablePart = this.dom[ item==='footer' ? 'tfoot' : 'thead' ];
  354. var focus = $.contains( tablePart[0], document.activeElement ) ?
  355. document.activeElement :
  356. null;
  357. if ( focus ) {
  358. focus.blur();
  359. }
  360. if ( mode === 'in-place' ) {
  361. // Insert the header back into the table's real header
  362. if ( itemDom.placeholder ) {
  363. itemDom.placeholder.remove();
  364. itemDom.placeholder = null;
  365. }
  366. this._unsize( item );
  367. if ( item === 'header' ) {
  368. itemDom.host.prepend( tablePart );
  369. }
  370. else {
  371. itemDom.host.append( tablePart );
  372. }
  373. if ( itemDom.floating ) {
  374. itemDom.floating.remove();
  375. itemDom.floating = null;
  376. }
  377. }
  378. else if ( mode === 'in' ) {
  379. // Remove the header from the read header and insert into a fixed
  380. // positioned floating table clone
  381. this._clone( item, forceChange );
  382. itemDom.floating
  383. .addClass( 'fixedHeader-floating' )
  384. .css( item === 'header' ? 'top' : 'bottom', this.c[item+'Offset'] )
  385. .css( 'left', position.left+'px' )
  386. .css( 'width', position.width+'px' );
  387. if ( item === 'footer' ) {
  388. itemDom.floating.css( 'top', '' );
  389. }
  390. }
  391. else if ( mode === 'below' ) { // only used for the header
  392. // Fix the position of the floating header at base of the table body
  393. this._clone( item, forceChange );
  394. itemDom.floating
  395. .addClass( 'fixedHeader-locked' )
  396. .css( 'top', position.tfootTop - position.theadHeight )
  397. .css( 'left', position.left+'px' )
  398. .css( 'width', position.width+'px' );
  399. }
  400. else if ( mode === 'above' ) { // only used for the footer
  401. // Fix the position of the floating footer at top of the table body
  402. this._clone( item, forceChange );
  403. itemDom.floating
  404. .addClass( 'fixedHeader-locked' )
  405. .css( 'top', position.tbodyTop )
  406. .css( 'left', position.left+'px' )
  407. .css( 'width', position.width+'px' );
  408. }
  409. // Restore focus if it was lost
  410. if ( focus && focus !== document.activeElement ) {
  411. setTimeout( function () {
  412. focus.focus();
  413. }, 10 );
  414. }
  415. this.s.scrollLeft.header = -1;
  416. this.s.scrollLeft.footer = -1;
  417. this.s[item+'Mode'] = mode;
  418. },
  419. /**
  420. * Cache the positional information that is required for the mode
  421. * calculations that FixedHeader performs.
  422. *
  423. * @private
  424. */
  425. _positions: function ()
  426. {
  427. var dt = this.s.dt;
  428. var table = dt.table();
  429. var position = this.s.position;
  430. var dom = this.dom;
  431. var tableNode = $(table.node());
  432. // Need to use the header and footer that are in the main table,
  433. // regardless of if they are clones, since they hold the positions we
  434. // want to measure from
  435. var thead = tableNode.children('thead');
  436. var tfoot = tableNode.children('tfoot');
  437. var tbody = dom.tbody;
  438. position.visible = tableNode.is(':visible');
  439. position.width = tableNode.outerWidth();
  440. position.left = tableNode.offset().left;
  441. position.theadTop = thead.offset().top;
  442. position.tbodyTop = tbody.offset().top;
  443. position.theadHeight = position.tbodyTop - position.theadTop;
  444. if ( tfoot.length ) {
  445. position.tfootTop = tfoot.offset().top;
  446. position.tfootBottom = position.tfootTop + tfoot.outerHeight();
  447. position.tfootHeight = position.tfootBottom - position.tfootTop;
  448. }
  449. else {
  450. position.tfootTop = position.tbodyTop + tbody.outerHeight();
  451. position.tfootBottom = position.tfootTop;
  452. position.tfootHeight = position.tfootTop;
  453. }
  454. },
  455. /**
  456. * Mode calculation - determine what mode the fixed items should be placed
  457. * into.
  458. *
  459. * @param {boolean} forceChange Force a redraw of the mode, even if already
  460. * in that mode.
  461. * @private
  462. */
  463. _scroll: function ( forceChange )
  464. {
  465. var windowTop = $(document).scrollTop();
  466. var windowLeft = $(document).scrollLeft();
  467. var position = this.s.position;
  468. var headerMode, footerMode;
  469. if ( ! this.s.enable ) {
  470. return;
  471. }
  472. if ( this.c.header ) {
  473. if ( ! position.visible || windowTop <= position.theadTop - this.c.headerOffset ) {
  474. headerMode = 'in-place';
  475. }
  476. else if ( windowTop <= position.tfootTop - position.theadHeight - this.c.headerOffset ) {
  477. headerMode = 'in';
  478. }
  479. else {
  480. headerMode = 'below';
  481. }
  482. if ( forceChange || headerMode !== this.s.headerMode ) {
  483. this._modeChange( headerMode, 'header', forceChange );
  484. }
  485. this._horizontal( 'header', windowLeft );
  486. }
  487. if ( this.c.footer && this.dom.tfoot.length ) {
  488. if ( ! position.visible || windowTop + position.windowHeight >= position.tfootBottom + this.c.footerOffset ) {
  489. footerMode = 'in-place';
  490. }
  491. else if ( position.windowHeight + windowTop > position.tbodyTop + position.tfootHeight + this.c.footerOffset ) {
  492. footerMode = 'in';
  493. }
  494. else {
  495. footerMode = 'above';
  496. }
  497. if ( forceChange || footerMode !== this.s.footerMode ) {
  498. this._modeChange( footerMode, 'footer', forceChange );
  499. }
  500. this._horizontal( 'footer', windowLeft );
  501. }
  502. }
  503. } );
  504. /**
  505. * Version
  506. * @type {String}
  507. * @static
  508. */
  509. FixedHeader.version = "3.1.4";
  510. /**
  511. * Defaults
  512. * @type {Object}
  513. * @static
  514. */
  515. FixedHeader.defaults = {
  516. header: true,
  517. footer: false,
  518. headerOffset: 0,
  519. footerOffset: 0
  520. };
  521. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  522. * DataTables interfaces
  523. */
  524. // Attach for constructor access
  525. $.fn.dataTable.FixedHeader = FixedHeader;
  526. $.fn.DataTable.FixedHeader = FixedHeader;
  527. // DataTables creation - check if the FixedHeader option has been defined on the
  528. // table and if so, initialise
  529. $(document).on( 'init.dt.dtfh', function (e, settings, json) {
  530. if ( e.namespace !== 'dt' ) {
  531. return;
  532. }
  533. var init = settings.oInit.fixedHeader;
  534. var defaults = DataTable.defaults.fixedHeader;
  535. if ( (init || defaults) && ! settings._fixedHeader ) {
  536. var opts = $.extend( {}, defaults, init );
  537. if ( init !== false ) {
  538. new FixedHeader( settings, opts );
  539. }
  540. }
  541. } );
  542. // DataTables API methods
  543. DataTable.Api.register( 'fixedHeader()', function () {} );
  544. DataTable.Api.register( 'fixedHeader.adjust()', function () {
  545. return this.iterator( 'table', function ( ctx ) {
  546. var fh = ctx._fixedHeader;
  547. if ( fh ) {
  548. fh.update();
  549. }
  550. } );
  551. } );
  552. DataTable.Api.register( 'fixedHeader.enable()', function ( flag ) {
  553. return this.iterator( 'table', function ( ctx ) {
  554. var fh = ctx._fixedHeader;
  555. flag = ( flag !== undefined ? flag : true );
  556. if ( fh && flag !== fh.s.enable ) {
  557. fh.enable( flag );
  558. }
  559. } );
  560. } );
  561. DataTable.Api.register( 'fixedHeader.disable()', function ( ) {
  562. return this.iterator( 'table', function ( ctx ) {
  563. var fh = ctx._fixedHeader;
  564. if ( fh && fh.s.enable ) {
  565. fh.enable( false );
  566. }
  567. } );
  568. } );
  569. $.each( ['header', 'footer'], function ( i, el ) {
  570. DataTable.Api.register( 'fixedHeader.'+el+'Offset()', function ( offset ) {
  571. var ctx = this.context;
  572. if ( offset === undefined ) {
  573. return ctx.length && ctx[0]._fixedHeader ?
  574. ctx[0]._fixedHeader[el +'Offset']() :
  575. undefined;
  576. }
  577. return this.iterator( 'table', function ( ctx ) {
  578. var fh = ctx._fixedHeader;
  579. if ( fh ) {
  580. fh[ el +'Offset' ]( offset );
  581. }
  582. } );
  583. } );
  584. } );
  585. return FixedHeader;
  586. }));