| 41 |
lars |
1 |
/*! Responsive 1.0.4
|
|
|
2 |
* 2014 SpryMedia Ltd - datatables.net/license
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* @summary Responsive
|
|
|
7 |
* @description Responsive tables plug-in for DataTables
|
|
|
8 |
* @version 1.0.4
|
|
|
9 |
* @file dataTables.responsive.js
|
|
|
10 |
* @author SpryMedia Ltd (www.sprymedia.co.uk)
|
|
|
11 |
* @contact www.sprymedia.co.uk/contact
|
|
|
12 |
* @copyright Copyright 2014 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 |
|
|
|
24 |
(function(window, document, undefined) {
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
var factory = function( $, DataTable ) {
|
|
|
28 |
"use strict";
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* Responsive is a plug-in for the DataTables library that makes use of
|
|
|
32 |
* DataTables' ability to change the visibility of columns, changing the
|
|
|
33 |
* visibility of columns so the displayed columns fit into the table container.
|
|
|
34 |
* The end result is that complex tables will be dynamically adjusted to fit
|
|
|
35 |
* into the viewport, be it on a desktop, tablet or mobile browser.
|
|
|
36 |
*
|
|
|
37 |
* Responsive for DataTables has two modes of operation, which can used
|
|
|
38 |
* individually or combined:
|
|
|
39 |
*
|
|
|
40 |
* * Class name based control - columns assigned class names that match the
|
|
|
41 |
* breakpoint logic can be shown / hidden as required for each breakpoint.
|
|
|
42 |
* * Automatic control - columns are automatically hidden when there is no
|
|
|
43 |
* room left to display them. Columns removed from the right.
|
|
|
44 |
*
|
|
|
45 |
* In additional to column visibility control, Responsive also has built into
|
|
|
46 |
* options to use DataTables' child row display to show / hide the information
|
|
|
47 |
* from the table that has been hidden. There are also two modes of operation
|
|
|
48 |
* for this child row display:
|
|
|
49 |
*
|
|
|
50 |
* * Inline - when the control element that the user can use to show / hide
|
|
|
51 |
* child rows is displayed inside the first column of the table.
|
|
|
52 |
* * Column - where a whole column is dedicated to be the show / hide control.
|
|
|
53 |
*
|
|
|
54 |
* Initialisation of Responsive is performed by:
|
|
|
55 |
*
|
|
|
56 |
* * Adding the class `responsive` or `dt-responsive` to the table. In this case
|
|
|
57 |
* Responsive will automatically be initialised with the default configuration
|
|
|
58 |
* options when the DataTable is created.
|
|
|
59 |
* * Using the `responsive` option in the DataTables configuration options. This
|
|
|
60 |
* can also be used to specify the configuration options, or simply set to
|
|
|
61 |
* `true` to use the defaults.
|
|
|
62 |
*
|
|
|
63 |
* @class
|
|
|
64 |
* @param {object} settings DataTables settings object for the host table
|
|
|
65 |
* @param {object} [opts] Configuration options
|
|
|
66 |
* @requires jQuery 1.7+
|
|
|
67 |
* @requires DataTables 1.10.1+
|
|
|
68 |
*
|
|
|
69 |
* @example
|
|
|
70 |
* $('#example').DataTable( {
|
|
|
71 |
* responsive: true
|
|
|
72 |
* } );
|
|
|
73 |
* } );
|
|
|
74 |
*/
|
|
|
75 |
var Responsive = function ( settings, opts ) {
|
|
|
76 |
// Sanity check that we are using DataTables 1.10 or newer
|
|
|
77 |
if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.1' ) ) {
|
|
|
78 |
throw 'DataTables Responsive requires DataTables 1.10.1 or newer';
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
this.s = {
|
|
|
82 |
dt: new DataTable.Api( settings ),
|
|
|
83 |
columns: []
|
|
|
84 |
};
|
|
|
85 |
|
|
|
86 |
// Check if responsive has already been initialised on this table
|
|
|
87 |
if ( this.s.dt.settings()[0].responsive ) {
|
|
|
88 |
return;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// details is an object, but for simplicity the user can give it as a string
|
|
|
92 |
if ( opts && typeof opts.details === 'string' ) {
|
|
|
93 |
opts.details = { type: opts.details };
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
this.c = $.extend( true, {}, Responsive.defaults, DataTable.defaults.responsive, opts );
|
|
|
97 |
settings.responsive = this;
|
|
|
98 |
this._constructor();
|
|
|
99 |
};
|
|
|
100 |
|
|
|
101 |
Responsive.prototype = {
|
|
|
102 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
103 |
* Constructor
|
|
|
104 |
*/
|
|
|
105 |
|
|
|
106 |
/**
|
|
|
107 |
* Initialise the Responsive instance
|
|
|
108 |
*
|
|
|
109 |
* @private
|
|
|
110 |
*/
|
|
|
111 |
_constructor: function ()
|
|
|
112 |
{
|
|
|
113 |
var that = this;
|
|
|
114 |
var dt = this.s.dt;
|
|
|
115 |
|
|
|
116 |
dt.settings()[0]._responsive = this;
|
|
|
117 |
|
|
|
118 |
// Use DataTables' private throttle function to avoid processor thrashing
|
|
|
119 |
$(window).on( 'resize.dtr orientationchange.dtr', dt.settings()[0].oApi._fnThrottle( function () {
|
|
|
120 |
that._resize();
|
|
|
121 |
} ) );
|
|
|
122 |
|
|
|
123 |
// Destroy event handler
|
|
|
124 |
dt.on( 'destroy.dtr', function () {
|
|
|
125 |
$(window).off( 'resize.dtr orientationchange.dtr draw.dtr' );
|
|
|
126 |
} );
|
|
|
127 |
|
|
|
128 |
// Reorder the breakpoints array here in case they have been added out
|
|
|
129 |
// of order
|
|
|
130 |
this.c.breakpoints.sort( function (a, b) {
|
|
|
131 |
return a.width < b.width ? 1 :
|
|
|
132 |
a.width > b.width ? -1 : 0;
|
|
|
133 |
} );
|
|
|
134 |
|
|
|
135 |
// Determine which columns are already hidden, and should therefore
|
|
|
136 |
// remain hidden. TODO - should this be done? See thread 22677
|
|
|
137 |
//
|
|
|
138 |
// this.s.alwaysHidden = dt.columns(':hidden').indexes();
|
|
|
139 |
|
|
|
140 |
this._classLogic();
|
|
|
141 |
this._resizeAuto();
|
|
|
142 |
|
|
|
143 |
// First pass - draw the table for the current viewport size
|
|
|
144 |
this._resize();
|
|
|
145 |
|
|
|
146 |
// Details handler
|
|
|
147 |
var details = this.c.details;
|
|
|
148 |
if ( details.type ) {
|
|
|
149 |
that._detailsInit();
|
|
|
150 |
this._detailsVis();
|
|
|
151 |
|
|
|
152 |
dt.on( 'column-visibility.dtr', function () {
|
|
|
153 |
that._detailsVis();
|
|
|
154 |
} );
|
|
|
155 |
|
|
|
156 |
// Redraw the details box on each draw. This is used until
|
|
|
157 |
// DataTables implements a native `updated` event for rows
|
|
|
158 |
dt.on( 'draw.dtr', function () {
|
|
|
159 |
dt.rows().iterator( 'row', function ( settings, idx ) {
|
|
|
160 |
var row = dt.row( idx );
|
|
|
161 |
|
|
|
162 |
if ( row.child.isShown() ) {
|
|
|
163 |
var info = that.c.details.renderer( dt, idx );
|
|
|
164 |
row.child( info, 'child' ).show();
|
|
|
165 |
}
|
|
|
166 |
} );
|
|
|
167 |
} );
|
|
|
168 |
|
|
|
169 |
$(dt.table().node()).addClass( 'dtr-'+details.type );
|
|
|
170 |
}
|
|
|
171 |
},
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
|
|
175 |
* Private methods
|
|
|
176 |
*/
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Calculate the visibility for the columns in a table for a given
|
|
|
180 |
* breakpoint. The result is pre-determined based on the class logic if
|
|
|
181 |
* class names are used to control all columns, but the width of the table
|
|
|
182 |
* is also used if there are columns which are to be automatically shown
|
|
|
183 |
* and hidden.
|
|
|
184 |
*
|
|
|
185 |
* @param {string} breakpoint Breakpoint name to use for the calculation
|
|
|
186 |
* @return {array} Array of boolean values initiating the visibility of each
|
|
|
187 |
* column.
|
|
|
188 |
* @private
|
|
|
189 |
*/
|
|
|
190 |
_columnsVisiblity: function ( breakpoint )
|
|
|
191 |
{
|
|
|
192 |
var dt = this.s.dt;
|
|
|
193 |
var columns = this.s.columns;
|
|
|
194 |
var i, ien;
|
|
|
195 |
|
|
|
196 |
// Class logic - determine which columns are in this breakpoint based
|
|
|
197 |
// on the classes. If no class control (i.e. `auto`) then `-` is used
|
|
|
198 |
// to indicate this to the rest of the function
|
|
|
199 |
var display = $.map( columns, function ( col ) {
|
|
|
200 |
return col.auto && col.minWidth === null ?
|
|
|
201 |
false :
|
|
|
202 |
col.auto === true ?
|
|
|
203 |
'-' :
|
|
|
204 |
$.inArray( breakpoint, col.includeIn ) !== -1;
|
|
|
205 |
} );
|
|
|
206 |
|
|
|
207 |
// Auto column control - first pass: how much width is taken by the
|
|
|
208 |
// ones that must be included from the non-auto columns
|
|
|
209 |
var requiredWidth = 0;
|
|
|
210 |
for ( i=0, ien=display.length ; i<ien ; i++ ) {
|
|
|
211 |
if ( display[i] === true ) {
|
|
|
212 |
requiredWidth += columns[i].minWidth;
|
|
|
213 |
}
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
// Second pass, use up any remaining width for other columns
|
|
|
217 |
var widthAvailable = dt.table().container().offsetWidth;
|
|
|
218 |
var usedWidth = widthAvailable - requiredWidth;
|
|
|
219 |
|
|
|
220 |
// Control column needs to always be included. This makes it sub-
|
|
|
221 |
// optimal in terms of using the available with, but to stop layout
|
|
|
222 |
// thrashing or overflow. Also we need to account for the control column
|
|
|
223 |
// width first so we know how much width is available for the other
|
|
|
224 |
// columns, since the control column might not be the first one shown
|
|
|
225 |
for ( i=0, ien=display.length ; i<ien ; i++ ) {
|
|
|
226 |
if ( columns[i].control ) {
|
|
|
227 |
usedWidth -= columns[i].minWidth;
|
|
|
228 |
}
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
// Allow columns to be shown (counting from the left) until we run out
|
|
|
232 |
// of room
|
|
|
233 |
for ( i=0, ien=display.length ; i<ien ; i++ ) {
|
|
|
234 |
if ( display[i] === '-' && ! columns[i].control ) {
|
|
|
235 |
display[i] = usedWidth - columns[i].minWidth < 0 ?
|
|
|
236 |
false :
|
|
|
237 |
true;
|
|
|
238 |
|
|
|
239 |
usedWidth -= columns[i].minWidth;
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
// Determine if the 'control' column should be shown (if there is one).
|
|
|
244 |
// This is the case when there is a hidden column (that is not the
|
|
|
245 |
// control column). The two loops look inefficient here, but they are
|
|
|
246 |
// trivial and will fly through. We need to know the outcome from the
|
|
|
247 |
// first , before the action in the second can be taken
|
|
|
248 |
var showControl = false;
|
|
|
249 |
|
|
|
250 |
for ( i=0, ien=columns.length ; i<ien ; i++ ) {
|
|
|
251 |
if ( ! columns[i].control && ! columns[i].never && ! display[i] ) {
|
|
|
252 |
showControl = true;
|
|
|
253 |
break;
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
for ( i=0, ien=columns.length ; i<ien ; i++ ) {
|
|
|
258 |
if ( columns[i].control ) {
|
|
|
259 |
display[i] = showControl;
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
// Finally we need to make sure that there is at least one column that
|
|
|
264 |
// is visible
|
|
|
265 |
if ( $.inArray( true, display ) === -1 ) {
|
|
|
266 |
display[0] = true;
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
return display;
|
|
|
270 |
},
|
|
|
271 |
|
|
|
272 |
|
|
|
273 |
/**
|
|
|
274 |
* Create the internal `columns` array with information about the columns
|
|
|
275 |
* for the table. This includes determining which breakpoints the column
|
|
|
276 |
* will appear in, based upon class names in the column, which makes up the
|
|
|
277 |
* vast majority of this method.
|
|
|
278 |
*
|
|
|
279 |
* @private
|
|
|
280 |
*/
|
|
|
281 |
_classLogic: function ()
|
|
|
282 |
{
|
|
|
283 |
var that = this;
|
|
|
284 |
var calc = {};
|
|
|
285 |
var breakpoints = this.c.breakpoints;
|
|
|
286 |
var columns = this.s.dt.columns().eq(0).map( function (i) {
|
|
|
287 |
var className = this.column(i).header().className;
|
|
|
288 |
|
|
|
289 |
return {
|
|
|
290 |
className: className,
|
|
|
291 |
includeIn: [],
|
|
|
292 |
auto: false,
|
|
|
293 |
control: false,
|
|
|
294 |
never: className.match(/\bnever\b/) ? true : false
|
|
|
295 |
};
|
|
|
296 |
} );
|
|
|
297 |
|
|
|
298 |
// Simply add a breakpoint to `includeIn` array, ensuring that there are
|
|
|
299 |
// no duplicates
|
|
|
300 |
var add = function ( colIdx, name ) {
|
|
|
301 |
var includeIn = columns[ colIdx ].includeIn;
|
|
|
302 |
|
|
|
303 |
if ( $.inArray( name, includeIn ) === -1 ) {
|
|
|
304 |
includeIn.push( name );
|
|
|
305 |
}
|
|
|
306 |
};
|
|
|
307 |
|
|
|
308 |
var column = function ( colIdx, name, operator, matched ) {
|
|
|
309 |
var size, i, ien;
|
|
|
310 |
|
|
|
311 |
if ( ! operator ) {
|
|
|
312 |
columns[ colIdx ].includeIn.push( name );
|
|
|
313 |
}
|
|
|
314 |
else if ( operator === 'max-' ) {
|
|
|
315 |
// Add this breakpoint and all smaller
|
|
|
316 |
size = that._find( name ).width;
|
|
|
317 |
|
|
|
318 |
for ( i=0, ien=breakpoints.length ; i<ien ; i++ ) {
|
|
|
319 |
if ( breakpoints[i].width <= size ) {
|
|
|
320 |
add( colIdx, breakpoints[i].name );
|
|
|
321 |
}
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
else if ( operator === 'min-' ) {
|
|
|
325 |
// Add this breakpoint and all larger
|
|
|
326 |
size = that._find( name ).width;
|
|
|
327 |
|
|
|
328 |
for ( i=0, ien=breakpoints.length ; i<ien ; i++ ) {
|
|
|
329 |
if ( breakpoints[i].width >= size ) {
|
|
|
330 |
add( colIdx, breakpoints[i].name );
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
}
|
|
|
334 |
else if ( operator === 'not-' ) {
|
|
|
335 |
// Add all but this breakpoint (xxx need extra information)
|
|
|
336 |
|
|
|
337 |
for ( i=0, ien=breakpoints.length ; i<ien ; i++ ) {
|
|
|
338 |
if ( breakpoints[i].name.indexOf( matched ) === -1 ) {
|
|
|
339 |
add( colIdx, breakpoints[i].name );
|
|
|
340 |
}
|
|
|
341 |
}
|
|
|
342 |
}
|
|
|
343 |
};
|
|
|
344 |
|
|
|
345 |
// Loop over each column and determine if it has a responsive control
|
|
|
346 |
// class
|
|
|
347 |
columns.each( function ( col, i ) {
|
|
|
348 |
var classNames = col.className.split(' ');
|
|
|
349 |
var hasClass = false;
|
|
|
350 |
|
|
|
351 |
// Split the class name up so multiple rules can be applied if needed
|
|
|
352 |
for ( var k=0, ken=classNames.length ; k<ken ; k++ ) {
|
|
|
353 |
var className = $.trim( classNames[k] );
|
|
|
354 |
|
|
|
355 |
if ( className === 'all' ) {
|
|
|
356 |
// Include in all
|
|
|
357 |
hasClass = true;
|
|
|
358 |
col.includeIn = $.map( breakpoints, function (a) {
|
|
|
359 |
return a.name;
|
|
|
360 |
} );
|
|
|
361 |
return;
|
|
|
362 |
}
|
|
|
363 |
else if ( className === 'none' || className === 'never' ) {
|
|
|
364 |
// Include in none (default) and no auto
|
|
|
365 |
hasClass = true;
|
|
|
366 |
return;
|
|
|
367 |
}
|
|
|
368 |
else if ( className === 'control' ) {
|
|
|
369 |
// Special column that is only visible, when one of the other
|
|
|
370 |
// columns is hidden. This is used for the details control
|
|
|
371 |
hasClass = true;
|
|
|
372 |
col.control = true;
|
|
|
373 |
return;
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
$.each( breakpoints, function ( j, breakpoint ) {
|
|
|
377 |
// Does this column have a class that matches this breakpoint?
|
|
|
378 |
var brokenPoint = breakpoint.name.split('-');
|
|
|
379 |
var re = new RegExp( '(min\\-|max\\-|not\\-)?('+brokenPoint[0]+')(\\-[_a-zA-Z0-9])?' );
|
|
|
380 |
var match = className.match( re );
|
|
|
381 |
|
|
|
382 |
if ( match ) {
|
|
|
383 |
hasClass = true;
|
|
|
384 |
|
|
|
385 |
if ( match[2] === brokenPoint[0] && match[3] === '-'+brokenPoint[1] ) {
|
|
|
386 |
// Class name matches breakpoint name fully
|
|
|
387 |
column( i, breakpoint.name, match[1], match[2]+match[3] );
|
|
|
388 |
}
|
|
|
389 |
else if ( match[2] === brokenPoint[0] && ! match[3] ) {
|
|
|
390 |
// Class name matched primary breakpoint name with no qualifier
|
|
|
391 |
column( i, breakpoint.name, match[1], match[2] );
|
|
|
392 |
}
|
|
|
393 |
}
|
|
|
394 |
} );
|
|
|
395 |
}
|
|
|
396 |
|
|
|
397 |
// If there was no control class, then automatic sizing is used
|
|
|
398 |
if ( ! hasClass ) {
|
|
|
399 |
col.auto = true;
|
|
|
400 |
}
|
|
|
401 |
} );
|
|
|
402 |
|
|
|
403 |
this.s.columns = columns;
|
|
|
404 |
},
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
/**
|
|
|
408 |
* Initialisation for the details handler
|
|
|
409 |
*
|
|
|
410 |
* @private
|
|
|
411 |
*/
|
|
|
412 |
_detailsInit: function ()
|
|
|
413 |
{
|
|
|
414 |
var that = this;
|
|
|
415 |
var dt = this.s.dt;
|
|
|
416 |
var details = this.c.details;
|
|
|
417 |
|
|
|
418 |
// The inline type always uses the first child as the target
|
|
|
419 |
if ( details.type === 'inline' ) {
|
|
|
420 |
details.target = 'td:first-child';
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
// type.target can be a string jQuery selector or a column index
|
|
|
424 |
var target = details.target;
|
|
|
425 |
var selector = typeof target === 'string' ? target : 'td';
|
|
|
426 |
|
|
|
427 |
// Click handler to show / hide the details rows when they are available
|
|
|
428 |
$( dt.table().body() ).on( 'click', selector, function (e) {
|
|
|
429 |
// If the table is not collapsed (i.e. there is no hidden columns)
|
|
|
430 |
// then take no action
|
|
|
431 |
if ( ! $(dt.table().node()).hasClass('collapsed' ) ) {
|
|
|
432 |
return;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
// Check that the row is actually a DataTable's controlled node
|
|
|
436 |
if ( ! dt.row( $(this).closest('tr') ).length ) {
|
|
|
437 |
return;
|
|
|
438 |
}
|
|
|
439 |
|
|
|
440 |
// For column index, we determine if we should act or not in the
|
|
|
441 |
// handler - otherwise it is already okay
|
|
|
442 |
if ( typeof target === 'number' ) {
|
|
|
443 |
var targetIdx = target < 0 ?
|
|
|
444 |
dt.columns().eq(0).length + target :
|
|
|
445 |
target;
|
|
|
446 |
|
|
|
447 |
if ( dt.cell( this ).index().column !== targetIdx ) {
|
|
|
448 |
return;
|
|
|
449 |
}
|
|
|
450 |
}
|
|
|
451 |
|
|
|
452 |
// $().closest() includes itself in its check
|
|
|
453 |
var row = dt.row( $(this).closest('tr') );
|
|
|
454 |
|
|
|
455 |
if ( row.child.isShown() ) {
|
|
|
456 |
row.child( false );
|
|
|
457 |
$( row.node() ).removeClass( 'parent' );
|
|
|
458 |
}
|
|
|
459 |
else {
|
|
|
460 |
var info = that.c.details.renderer( dt, row[0] );
|
|
|
461 |
row.child( info, 'child' ).show();
|
|
|
462 |
$( row.node() ).addClass( 'parent' );
|
|
|
463 |
}
|
|
|
464 |
} );
|
|
|
465 |
},
|
|
|
466 |
|
|
|
467 |
|
|
|
468 |
/**
|
|
|
469 |
* Update the child rows in the table whenever the column visibility changes
|
|
|
470 |
*
|
|
|
471 |
* @private
|
|
|
472 |
*/
|
|
|
473 |
_detailsVis: function ()
|
|
|
474 |
{
|
|
|
475 |
var that = this;
|
|
|
476 |
var dt = this.s.dt;
|
|
|
477 |
|
|
|
478 |
// Find how many columns are hidden
|
|
|
479 |
var hiddenColumns = dt.columns().indexes().filter( function ( idx ) {
|
|
|
480 |
var col = dt.column( idx );
|
|
|
481 |
|
|
|
482 |
if ( col.visible() ) {
|
|
|
483 |
return null;
|
|
|
484 |
}
|
|
|
485 |
|
|
|
486 |
// Only counts as hidden if it doesn't have the `never` class
|
|
|
487 |
return $( col.header() ).hasClass( 'never' ) ? null : idx;
|
|
|
488 |
} );
|
|
|
489 |
var haveHidden = true;
|
|
|
490 |
|
|
|
491 |
if ( hiddenColumns.length === 0 || ( hiddenColumns.length === 1 && this.s.columns[ hiddenColumns[0] ].control ) ) {
|
|
|
492 |
haveHidden = false;
|
|
|
493 |
}
|
|
|
494 |
|
|
|
495 |
if ( haveHidden ) {
|
|
|
496 |
// Got hidden columns
|
|
|
497 |
$( dt.table().node() ).addClass('collapsed');
|
|
|
498 |
|
|
|
499 |
// Show all existing child rows
|
|
|
500 |
dt.rows().eq(0).each( function (idx) {
|
|
|
501 |
var row = dt.row( idx );
|
|
|
502 |
|
|
|
503 |
if ( row.child() ) {
|
|
|
504 |
var info = that.c.details.renderer( dt, row[0] );
|
|
|
505 |
|
|
|
506 |
// The renderer can return false to have no child row
|
|
|
507 |
if ( info === false ) {
|
|
|
508 |
row.child.hide();
|
|
|
509 |
}
|
|
|
510 |
else {
|
|
|
511 |
row.child( info, 'child' ).show();
|
|
|
512 |
}
|
|
|
513 |
}
|
|
|
514 |
} );
|
|
|
515 |
}
|
|
|
516 |
else {
|
|
|
517 |
// No hidden columns
|
|
|
518 |
$( dt.table().node() ).removeClass('collapsed');
|
|
|
519 |
|
|
|
520 |
// Hide all existing child rows
|
|
|
521 |
dt.rows().eq(0).each( function (idx) {
|
|
|
522 |
dt.row( idx ).child.hide();
|
|
|
523 |
} );
|
|
|
524 |
}
|
|
|
525 |
},
|
|
|
526 |
|
|
|
527 |
|
|
|
528 |
/**
|
|
|
529 |
* Find a breakpoint object from a name
|
|
|
530 |
* @param {string} name Breakpoint name to find
|
|
|
531 |
* @return {object} Breakpoint description object
|
|
|
532 |
*/
|
|
|
533 |
_find: function ( name )
|
|
|
534 |
{
|
|
|
535 |
var breakpoints = this.c.breakpoints;
|
|
|
536 |
|
|
|
537 |
for ( var i=0, ien=breakpoints.length ; i<ien ; i++ ) {
|
|
|
538 |
if ( breakpoints[i].name === name ) {
|
|
|
539 |
return breakpoints[i];
|
|
|
540 |
}
|
|
|
541 |
}
|
|
|
542 |
},
|
|
|
543 |
|
|
|
544 |
|
|
|
545 |
/**
|
|
|
546 |
* Alter the table display for a resized viewport. This involves first
|
|
|
547 |
* determining what breakpoint the window currently is in, getting the
|
|
|
548 |
* column visibilities to apply and then setting them.
|
|
|
549 |
*
|
|
|
550 |
* @private
|
|
|
551 |
*/
|
|
|
552 |
_resize: function ()
|
|
|
553 |
{
|
|
|
554 |
var dt = this.s.dt;
|
|
|
555 |
var width = $(window).width();
|
|
|
556 |
var breakpoints = this.c.breakpoints;
|
|
|
557 |
var breakpoint = breakpoints[0].name;
|
|
|
558 |
|
|
|
559 |
// Determine what breakpoint we are currently at
|
|
|
560 |
for ( var i=breakpoints.length-1 ; i>=0 ; i-- ) {
|
|
|
561 |
if ( width <= breakpoints[i].width ) {
|
|
|
562 |
breakpoint = breakpoints[i].name;
|
|
|
563 |
break;
|
|
|
564 |
}
|
|
|
565 |
}
|
|
|
566 |
|
|
|
567 |
// Show the columns for that break point
|
|
|
568 |
var columns = this._columnsVisiblity( breakpoint );
|
|
|
569 |
|
|
|
570 |
dt.columns().eq(0).each( function ( colIdx, i ) {
|
|
|
571 |
dt.column( colIdx ).visible( columns[i] );
|
|
|
572 |
} );
|
|
|
573 |
},
|
|
|
574 |
|
|
|
575 |
|
|
|
576 |
/**
|
|
|
577 |
* Determine the width of each column in the table so the auto column hiding
|
|
|
578 |
* has that information to work with. This method is never going to be 100%
|
|
|
579 |
* perfect since column widths can change slightly per page, but without
|
|
|
580 |
* seriously compromising performance this is quite effective.
|
|
|
581 |
*
|
|
|
582 |
* @private
|
|
|
583 |
*/
|
|
|
584 |
_resizeAuto: function ()
|
|
|
585 |
{
|
|
|
586 |
var dt = this.s.dt;
|
|
|
587 |
var columns = this.s.columns;
|
|
|
588 |
|
|
|
589 |
// Are we allowed to do auto sizing?
|
|
|
590 |
if ( ! this.c.auto ) {
|
|
|
591 |
return;
|
|
|
592 |
}
|
|
|
593 |
|
|
|
594 |
// Are there any columns that actually need auto-sizing, or do they all
|
|
|
595 |
// have classes defined
|
|
|
596 |
if ( $.inArray( true, $.map( columns, function (c) { return c.auto; } ) ) === -1 ) {
|
|
|
597 |
return;
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
// Clone the table with the current data in it
|
|
|
601 |
var tableWidth = dt.table().node().offsetWidth;
|
|
|
602 |
var columnWidths = dt.columns;
|
|
|
603 |
var clonedTable = dt.table().node().cloneNode( false );
|
|
|
604 |
var clonedHeader = $( dt.table().header().cloneNode( false ) ).appendTo( clonedTable );
|
|
|
605 |
var clonedBody = $( dt.table().body().cloneNode( false ) ).appendTo( clonedTable );
|
|
|
606 |
|
|
|
607 |
// This is a bit slow, but we need to get a clone of each row that
|
|
|
608 |
// includes all columns. As such, try to do this as little as possible.
|
|
|
609 |
dt.rows( { page: 'current' } ).indexes().flatten().each( function ( idx ) {
|
|
|
610 |
var clone = dt.row( idx ).node().cloneNode( true );
|
|
|
611 |
|
|
|
612 |
if ( dt.columns( ':hidden' ).flatten().length ) {
|
|
|
613 |
$(clone).append( dt.cells( idx, ':hidden' ).nodes().to$().clone() );
|
|
|
614 |
}
|
|
|
615 |
|
|
|
616 |
$(clone).appendTo( clonedBody );
|
|
|
617 |
} );
|
|
|
618 |
|
|
|
619 |
var cells = dt.columns().header().to$().clone( false );
|
|
|
620 |
$('<tr/>')
|
|
|
621 |
.append( cells )
|
|
|
622 |
.appendTo( clonedHeader );
|
|
|
623 |
|
|
|
624 |
var inserted = $('<div/>')
|
|
|
625 |
.css( {
|
|
|
626 |
width: 1,
|
|
|
627 |
height: 1,
|
|
|
628 |
overflow: 'hidden'
|
|
|
629 |
} )
|
|
|
630 |
.append( clonedTable )
|
|
|
631 |
.insertBefore( dt.table().node() );
|
|
|
632 |
|
|
|
633 |
// The cloned header now contains the smallest that each column can be
|
|
|
634 |
dt.columns().eq(0).each( function ( idx ) {
|
|
|
635 |
columns[idx].minWidth = cells[ idx ].offsetWidth || 0;
|
|
|
636 |
} );
|
|
|
637 |
|
|
|
638 |
inserted.remove();
|
|
|
639 |
}
|
|
|
640 |
};
|
|
|
641 |
|
|
|
642 |
|
|
|
643 |
/**
|
|
|
644 |
* List of default breakpoints. Each item in the array is an object with two
|
|
|
645 |
* properties:
|
|
|
646 |
*
|
|
|
647 |
* * `name` - the breakpoint name.
|
|
|
648 |
* * `width` - the breakpoint width
|
|
|
649 |
*
|
|
|
650 |
* @name Responsive.breakpoints
|
|
|
651 |
* @static
|
|
|
652 |
*/
|
|
|
653 |
Responsive.breakpoints = [
|
|
|
654 |
{ name: 'desktop', width: Infinity },
|
|
|
655 |
{ name: 'tablet-l', width: 1024 },
|
|
|
656 |
{ name: 'tablet-p', width: 768 },
|
|
|
657 |
{ name: 'mobile-l', width: 480 },
|
|
|
658 |
{ name: 'mobile-p', width: 320 }
|
|
|
659 |
];
|
|
|
660 |
|
|
|
661 |
|
|
|
662 |
/**
|
|
|
663 |
* Responsive default settings for initialisation
|
|
|
664 |
*
|
|
|
665 |
* @namespace
|
|
|
666 |
* @name Responsive.defaults
|
|
|
667 |
* @static
|
|
|
668 |
*/
|
|
|
669 |
Responsive.defaults = {
|
|
|
670 |
/**
|
|
|
671 |
* List of breakpoints for the instance. Note that this means that each
|
|
|
672 |
* instance can have its own breakpoints. Additionally, the breakpoints
|
|
|
673 |
* cannot be changed once an instance has been creased.
|
|
|
674 |
*
|
|
|
675 |
* @type {Array}
|
|
|
676 |
* @default Takes the value of `Responsive.breakpoints`
|
|
|
677 |
*/
|
|
|
678 |
breakpoints: Responsive.breakpoints,
|
|
|
679 |
|
|
|
680 |
/**
|
|
|
681 |
* Enable / disable auto hiding calculations. It can help to increase
|
|
|
682 |
* performance slightly if you disable this option, but all columns would
|
|
|
683 |
* need to have breakpoint classes assigned to them
|
|
|
684 |
*
|
|
|
685 |
* @type {Boolean}
|
|
|
686 |
* @default `true`
|
|
|
687 |
*/
|
|
|
688 |
auto: true,
|
|
|
689 |
|
|
|
690 |
/**
|
|
|
691 |
* Details control. If given as a string value, the `type` property of the
|
|
|
692 |
* default object is set to that value, and the defaults used for the rest
|
|
|
693 |
* of the object - this is for ease of implementation.
|
|
|
694 |
*
|
|
|
695 |
* The object consists of the following properties:
|
|
|
696 |
*
|
|
|
697 |
* * `renderer` - function that is called for display of the child row data.
|
|
|
698 |
* The default function will show the data from the hidden columns
|
|
|
699 |
* * `target` - Used as the selector for what objects to attach the child
|
|
|
700 |
* open / close to
|
|
|
701 |
* * `type` - `false` to disable the details display, `inline` or `column`
|
|
|
702 |
* for the two control types
|
|
|
703 |
*
|
|
|
704 |
* @type {Object|string}
|
|
|
705 |
*/
|
|
|
706 |
details: {
|
|
|
707 |
renderer: function ( api, rowIdx ) {
|
|
|
708 |
var data = api.cells( rowIdx, ':hidden' ).eq(0).map( function ( cell ) {
|
|
|
709 |
var header = $( api.column( cell.column ).header() );
|
|
|
710 |
var idx = api.cell( cell ).index();
|
|
|
711 |
|
|
|
712 |
if ( header.hasClass( 'control' ) || header.hasClass( 'never' ) ) {
|
|
|
713 |
return '';
|
|
|
714 |
}
|
|
|
715 |
|
|
|
716 |
// Use a non-public DT API method to render the data for display
|
|
|
717 |
// This needs to be updated when DT adds a suitable method for
|
|
|
718 |
// this type of data retrieval
|
|
|
719 |
var dtPrivate = api.settings()[0];
|
|
|
720 |
var cellData = dtPrivate.oApi._fnGetCellData(
|
|
|
721 |
dtPrivate, idx.row, idx.column, 'display'
|
|
|
722 |
);
|
|
|
723 |
var title = header.text();
|
|
|
724 |
if ( title ) {
|
|
|
725 |
title = title + ':';
|
|
|
726 |
}
|
|
|
727 |
|
|
|
728 |
return '<li data-dtr-index="'+idx.column+'">'+
|
|
|
729 |
'<span class="dtr-title">'+
|
|
|
730 |
title+
|
|
|
731 |
'</span> '+
|
|
|
732 |
'<span class="dtr-data">'+
|
|
|
733 |
cellData+
|
|
|
734 |
'</span>'+
|
|
|
735 |
'</li>';
|
|
|
736 |
} ).toArray().join('');
|
|
|
737 |
|
|
|
738 |
return data ?
|
|
|
739 |
$('<ul data-dtr-index="'+rowIdx+'"/>').append( data ) :
|
|
|
740 |
false;
|
|
|
741 |
},
|
|
|
742 |
|
|
|
743 |
target: 0,
|
|
|
744 |
|
|
|
745 |
type: 'inline'
|
|
|
746 |
}
|
|
|
747 |
};
|
|
|
748 |
|
|
|
749 |
|
|
|
750 |
/*
|
|
|
751 |
* API
|
|
|
752 |
*/
|
|
|
753 |
var Api = $.fn.dataTable.Api;
|
|
|
754 |
|
|
|
755 |
// Doesn't do anything - work around for a bug in DT... Not documented
|
|
|
756 |
Api.register( 'responsive()', function () {
|
|
|
757 |
return this;
|
|
|
758 |
} );
|
|
|
759 |
|
|
|
760 |
Api.register( 'responsive.index()', function ( li ) {
|
|
|
761 |
li = $(li);
|
|
|
762 |
|
|
|
763 |
return {
|
|
|
764 |
column: li.data('dtr-index'),
|
|
|
765 |
row: li.parent().data('dtr-index')
|
|
|
766 |
};
|
|
|
767 |
} );
|
|
|
768 |
|
|
|
769 |
Api.register( 'responsive.rebuild()', function () {
|
|
|
770 |
return this.iterator( 'table', function ( ctx ) {
|
|
|
771 |
if ( ctx._responsive ) {
|
|
|
772 |
ctx._responsive._classLogic();
|
|
|
773 |
}
|
|
|
774 |
} );
|
|
|
775 |
} );
|
|
|
776 |
|
|
|
777 |
Api.register( 'responsive.recalc()', function () {
|
|
|
778 |
return this.iterator( 'table', function ( ctx ) {
|
|
|
779 |
if ( ctx._responsive ) {
|
|
|
780 |
ctx._responsive._resizeAuto();
|
|
|
781 |
ctx._responsive._resize();
|
|
|
782 |
}
|
|
|
783 |
} );
|
|
|
784 |
} );
|
|
|
785 |
|
|
|
786 |
|
|
|
787 |
/**
|
|
|
788 |
* Version information
|
|
|
789 |
*
|
|
|
790 |
* @name Responsive.version
|
|
|
791 |
* @static
|
|
|
792 |
*/
|
|
|
793 |
Responsive.version = '1.0.4';
|
|
|
794 |
|
|
|
795 |
|
|
|
796 |
$.fn.dataTable.Responsive = Responsive;
|
|
|
797 |
$.fn.DataTable.Responsive = Responsive;
|
|
|
798 |
|
|
|
799 |
// Attach a listener to the document which listens for DataTables initialisation
|
|
|
800 |
// events so we can automatically initialise
|
|
|
801 |
$(document).on( 'init.dt.dtr', function (e, settings, json) {
|
|
|
802 |
if ( $(settings.nTable).hasClass( 'responsive' ) ||
|
|
|
803 |
$(settings.nTable).hasClass( 'dt-responsive' ) ||
|
|
|
804 |
settings.oInit.responsive ||
|
|
|
805 |
DataTable.defaults.responsive
|
|
|
806 |
) {
|
|
|
807 |
var init = settings.oInit.responsive;
|
|
|
808 |
|
|
|
809 |
if ( init !== false ) {
|
|
|
810 |
new Responsive( settings, $.isPlainObject( init ) ? init : {} );
|
|
|
811 |
}
|
|
|
812 |
}
|
|
|
813 |
} );
|
|
|
814 |
|
|
|
815 |
return Responsive;
|
|
|
816 |
}; // /factory
|
|
|
817 |
|
|
|
818 |
|
|
|
819 |
// Define as an AMD module if possible
|
|
|
820 |
if ( typeof define === 'function' && define.amd ) {
|
|
|
821 |
define( ['jquery', 'datatables'], factory );
|
|
|
822 |
}
|
|
|
823 |
else if ( typeof exports === 'object' ) {
|
|
|
824 |
// Node/CommonJS
|
|
|
825 |
factory( require('jquery'), require('datatables') );
|
|
|
826 |
}
|
|
|
827 |
else if ( jQuery && !jQuery.fn.dataTable.Responsive ) {
|
|
|
828 |
// Otherwise simply initialise as normal, stopping multiple evaluation
|
|
|
829 |
factory( jQuery, jQuery.fn.dataTable );
|
|
|
830 |
}
|
|
|
831 |
|
|
|
832 |
|
|
|
833 |
})(window, document);
|
|
|
834 |
|