1 /*! ScrollResize for DataTables v1.0.0
2 * 2015 SpryMedia Ltd - datatables.net/license
6 * @summary ScrollResize
7 * @description Automatically alter the DataTables page length to fit the table
10 * @file dataTables.scrollResize.js
11 * @author SpryMedia Ltd (www.sprymedia.co.uk)
12 * @contact www.sprymedia.co.uk/contact
13 * @copyright Copyright 2015 SpryMedia Ltd.
15 * License MIT - http://datatables.net/license/mit
17 * This feature plug-in for DataTables will automatically change the DataTables
18 * page length in order to fit inside its container. This can be particularly
19 * useful for control panels and other interfaces which resize dynamically with
20 * the user's browser window instead of scrolling.
22 * Page resizing in DataTables can be enabled by using any one of the following
25 * * Setting the `scrollResize` parameter in the DataTables initialisation to
26 * be true - i.e. `scrollResize: true`
27 * * Setting the `scrollResize` parameter to be true in the DataTables
28 * defaults (thus causing all tables to have this feature) - i.e.
29 * `$.fn.dataTable.defaults.scrollResize = true`.
30 * * Creating a new instance: `new $.fn.dataTable.ScrollResize( table );` where
31 * `table` is a DataTable's API instance.
36 var ScrollResize = function ( dt )
39 var table = dt.table();
43 host: $(table.container()).parent(),
44 header: $(table.header()),
45 footer: $(table.footer()),
46 body: $(table.body()),
47 container: $(table.container()),
48 table: $(table.node())
51 var host = this.s.host;
52 if ( host.css('position') === 'static' ) {
53 host.css( 'position', 'relative' );
56 dt.on( 'draw', function () {
65 ScrollResize.prototype = {
68 var settings = this.s;
71 var offsetTop = $( settings.table ).offset().top;
72 var availableHeight = settings.host.height();
73 var scrollBody = $('div.dataTables_scrollBody', t.container());
75 // Subtract the height of the header, footer and the elements
76 // surrounding the table
77 availableHeight -= offsetTop;
78 availableHeight -= settings.container.height() - ( offsetTop + scrollBody.height() );
80 $('div.dataTables_scrollBody', t.container()).css( {
81 maxHeight: availableHeight,
82 height: availableHeight
85 if ( dt.fixedColumns ) {
86 dt.fixedColumns().relayout();
90 _attach: function () {
91 // There is no `resize` event for elements, so to trigger this effect,
92 // create an empty HTML document using an <iframe> which will issue a
93 // resize event inside itself when the document resizes. Since it is
94 // 100% x 100% that will occur whenever the host element is resized.
96 var obj = $('<iframe/>')
106 .attr( 'frameBorder', '0' )
107 .attr( 'src', 'about:blank' );
109 obj[0].onload = function () {
110 var body = this.contentDocument.body;
111 var height = body.offsetHeight;
112 var contentDoc = this.contentDocument;
113 var defaultView = contentDoc.defaultView || contentDoc.parentWindow;
115 defaultView.onresize = function () {
116 // Three methods to get the iframe height, to keep all browsers happy
117 var newHeight = body.clientHeight || body.offsetHeight;
118 var docClientHeight = contentDoc.documentElement.clientHeight;
120 if ( ! newHeight && docClientHeight ) {
121 newHeight = docClientHeight;
124 if ( newHeight !== height ) {
133 .appendTo( this.s.host )
134 .attr( 'data', 'about:blank' );
139 $.fn.dataTable.ScrollResize = ScrollResize;
140 $.fn.DataTable.ScrollResize = ScrollResize;
142 // Automatic initialisation listener
143 $(document).on( 'init.dt', function ( e, settings ) {
144 if ( e.namespace !== 'dt' ) {
148 var api = new $.fn.dataTable.Api( settings );
150 if ( settings.oInit.scrollResize || $.fn.dataTable.defaults.scrollResize ) {
151 new ScrollResize( api );