Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

dataTables.fixedHeader.js 22KB

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