dataTables.fixedHeader.js 16 KB

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