Subversion-Projekte lars-tiefland.faltradxxs.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
2 lars 1
/*! PageResize for DataTables v1.0.0
2
 * 2014 SpryMedia Ltd - datatables.net/license
3
 */
4
 
5
/**
6
 * @summary     PageResize
7
 * @description Automatically alter the DataTables page length to fit the table
8
     into a container
9
 * @version     1.0.0
10
 * @file        dataTables.pageResize.js
11
 * @author      SpryMedia Ltd (www.sprymedia.co.uk)
12
 * @contact     www.sprymedia.co.uk/contact
13
 * @copyright   Copyright 2015 SpryMedia Ltd.
14
 *
15
 * License      MIT - http://datatables.net/license/mit
16
 *
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.
21
 *
22
 * Page resizing in DataTables can be enabled by using any one of the following
23
 * options:
24
 *
25
 * * Adding the class `pageResize` to the HTML table
26
 * * Setting the `pageResize` parameter in the DataTables initialisation to
27
 *   be true - i.e. `pageResize: true`
28
 * * Setting the `pageResize` parameter to be true in the DataTables
29
 *   defaults (thus causing all tables to have this feature) - i.e.
30
 *   `$.fn.dataTable.defaults.pageResize = true`.
31
 * * Creating a new instance: `new $.fn.dataTable.PageResize( table );` where
32
 *   `table` is a DataTable's API instance.
33
 *
34
 * For more detailed information please see:
35
 *     http://datatables.net/blog/
36
 */
37
 
38
(function($){
39
 
40
var PageResize = function ( dt )
41
{
42
	var table = dt.table();
43
 
44
	this.s = {
45
		dt:        dt,
46
		host:      $(table.container()).parent(),
47
		header:    $(table.header()),
48
		footer:    $(table.footer()),
49
		body:      $(table.body()),
50
		container: $(table.container()),
51
		table:     $(table.node())
52
	};
53
 
54
	var host = this.s.host;
55
	if ( host.css('position') === 'static' ) {
56
		host.css( 'position', 'relative' );
57
	}
58
 
59
	this._attach();
60
	this._size();
61
}
62
 
63
PageResize.prototype = {
64
	_size: function ()
65
	{
66
		var settings = this.s;
67
		var dt = settings.dt;
68
		var t = dt.table();
69
		var offsetTop = $( settings.table ).offset().top;
70
		var rowHeight = $( 'tr', settings.body ).eq(0).height();
71
		var availableHeight = settings.host.height();
72
 
73
		// Subtract the height of the header, footer and the elements
74
		// surrounding the table
75
		availableHeight -= settings.header.height();
76
		availableHeight -= settings.footer.height();
77
		availableHeight -= offsetTop;
78
		availableHeight -= settings.container.height() - ( offsetTop + settings.table.height() );
79
 
80
		var drawRows = Math.floor( availableHeight / rowHeight );
81
 
82
		if ( drawRows !== dt.page.len() ) {
83
			dt.page.len( drawRows ).draw();
84
		}
85
	},
86
 
87
	_attach: function () {
88
		// There is no `resize` event for elements, so to trigger this effect,
89
		// create an empty HTML document using an <object> which will issue a
90
		// resize event inside itself when the document resizes. Since it is
91
		// 100% x 100% that will occur whenever the host element is resized.
92
		var that = this;
93
		var obj = $('<object/>')
94
			.css( {
95
				position: 'absolute',
96
				top: 0,
97
				left: 0,
98
				height: '100%',
99
				width: '100%',
100
				zIndex: -1
101
			} )
102
			.attr( 'type', 'text/html' )
103
			.attr( 'data', 'about:blank' );
104
 
105
		obj[0].onload = function () {
106
			var body = this.contentDocument.body;
107
			var height = body.offsetHeight;
108
 
109
			this.contentDocument.defaultView.onresize = function () {
110
				var newHeight = body.offsetHeight;
111
 
112
				if ( newHeight !== height ) {
113
					height = newHeight;
114
 
115
					that._size();
116
				}
117
			}
118
		};
119
 
120
		obj.appendTo( this.s.host );
121
	}
122
};
123
 
124
 
125
$.fn.dataTable.PageResize = PageResize;
126
$.fn.DataTable.PageResize = PageResize;
127
 
128
// Automatic initialisation listener
129
$(document).on( 'init.dt', function ( e, settings ) {
130
	var api = new $.fn.dataTable.Api( settings );
131
 
132
	if ( $( api.table().node() ).hasClass( 'pageResize' ) ||
133
		 settings.oInit.pageResize ||
134
		 $.fn.dataTable.defaults.pageResize )
135
	{
136
		new PageResize( api );
137
	}
138
} );
139
 
140
}(jQuery));
141