1 | /*! RowGroup 1.1.1
|
---|
2 | * ©2017-2019 SpryMedia Ltd - datatables.net/license
|
---|
3 | */
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @summary RowGroup
|
---|
7 | * @description RowGrouping for DataTables
|
---|
8 | * @version 1.1.1
|
---|
9 | * @file dataTables.rowGroup.js
|
---|
10 | * @author SpryMedia Ltd (www.sprymedia.co.uk)
|
---|
11 | * @contact datatables.net
|
---|
12 | * @copyright Copyright 2017-2019 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( factory ){
|
---|
25 | if ( typeof define === 'function' && define.amd ) {
|
---|
26 | // AMD
|
---|
27 | define( ['jquery', 'datatables.net'], function ( $ ) {
|
---|
28 | return factory( $, window, document );
|
---|
29 | } );
|
---|
30 | }
|
---|
31 | else if ( typeof exports === 'object' ) {
|
---|
32 | // CommonJS
|
---|
33 | module.exports = function (root, $) {
|
---|
34 | if ( ! root ) {
|
---|
35 | root = window;
|
---|
36 | }
|
---|
37 |
|
---|
38 | if ( ! $ || ! $.fn.dataTable ) {
|
---|
39 | $ = require('datatables.net')(root, $).$;
|
---|
40 | }
|
---|
41 |
|
---|
42 | return factory( $, root, root.document );
|
---|
43 | };
|
---|
44 | }
|
---|
45 | else {
|
---|
46 | // Browser
|
---|
47 | factory( jQuery, window, document );
|
---|
48 | }
|
---|
49 | }(function( $, window, document, undefined ) {
|
---|
50 | 'use strict';
|
---|
51 | var DataTable = $.fn.dataTable;
|
---|
52 |
|
---|
53 |
|
---|
54 | var RowGroup = function ( dt, opts ) {
|
---|
55 | // Sanity check that we are using DataTables 1.10 or newer
|
---|
56 | if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {
|
---|
57 | throw 'RowGroup requires DataTables 1.10.8 or newer';
|
---|
58 | }
|
---|
59 |
|
---|
60 | // User and defaults configuration object
|
---|
61 | this.c = $.extend( true, {},
|
---|
62 | DataTable.defaults.rowGroup,
|
---|
63 | RowGroup.defaults,
|
---|
64 | opts
|
---|
65 | );
|
---|
66 |
|
---|
67 | // Internal settings
|
---|
68 | this.s = {
|
---|
69 | dt: new DataTable.Api( dt )
|
---|
70 | };
|
---|
71 |
|
---|
72 | // DOM items
|
---|
73 | this.dom = {
|
---|
74 |
|
---|
75 | };
|
---|
76 |
|
---|
77 | // Check if row grouping has already been initialised on this table
|
---|
78 | var settings = this.s.dt.settings()[0];
|
---|
79 | var existing = settings.rowGroup;
|
---|
80 | if ( existing ) {
|
---|
81 | return existing;
|
---|
82 | }
|
---|
83 |
|
---|
84 | settings.rowGroup = this;
|
---|
85 | this._constructor();
|
---|
86 | };
|
---|
87 |
|
---|
88 |
|
---|
89 | $.extend( RowGroup.prototype, {
|
---|
90 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
91 | * API methods for DataTables API interface
|
---|
92 | */
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Get/set the grouping data source - need to call draw after this is
|
---|
96 | * executed as a setter
|
---|
97 | * @returns string~RowGroup
|
---|
98 | */
|
---|
99 | dataSrc: function ( val )
|
---|
100 | {
|
---|
101 | if ( val === undefined ) {
|
---|
102 | return this.c.dataSrc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | var dt = this.s.dt;
|
---|
106 |
|
---|
107 | this.c.dataSrc = val;
|
---|
108 |
|
---|
109 | $(dt.table().node()).triggerHandler( 'rowgroup-datasrc.dt', [ dt, val ] );
|
---|
110 |
|
---|
111 | return this;
|
---|
112 | },
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Disable - need to call draw after this is executed
|
---|
116 | * @returns RowGroup
|
---|
117 | */
|
---|
118 | disable: function ()
|
---|
119 | {
|
---|
120 | this.c.enable = false;
|
---|
121 | return this;
|
---|
122 | },
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Enable - need to call draw after this is executed
|
---|
126 | * @returns RowGroup
|
---|
127 | */
|
---|
128 | enable: function ( flag )
|
---|
129 | {
|
---|
130 | if ( flag === false ) {
|
---|
131 | return this.disable();
|
---|
132 | }
|
---|
133 |
|
---|
134 | this.c.enable = true;
|
---|
135 | return this;
|
---|
136 | },
|
---|
137 |
|
---|
138 |
|
---|
139 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
140 | * Constructor
|
---|
141 | */
|
---|
142 | _constructor: function ()
|
---|
143 | {
|
---|
144 | var that = this;
|
---|
145 | var dt = this.s.dt;
|
---|
146 |
|
---|
147 | dt.on( 'draw.dtrg', function () {
|
---|
148 | if ( that.c.enable ) {
|
---|
149 | that._draw();
|
---|
150 | }
|
---|
151 | } );
|
---|
152 |
|
---|
153 | dt.on( 'column-visibility.dt.dtrg responsive-resize.dt.dtrg', function () {
|
---|
154 | that._adjustColspan();
|
---|
155 | } );
|
---|
156 |
|
---|
157 | dt.on( 'destroy', function () {
|
---|
158 | dt.off( '.dtrg' );
|
---|
159 | } );
|
---|
160 |
|
---|
161 | dt.on('responsive-resize.dt', function () {
|
---|
162 | that._adjustColspan();
|
---|
163 | })
|
---|
164 | },
|
---|
165 |
|
---|
166 |
|
---|
167 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
168 | * Private methods
|
---|
169 | */
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Adjust column span when column visibility changes
|
---|
173 | * @private
|
---|
174 | */
|
---|
175 | _adjustColspan: function ()
|
---|
176 | {
|
---|
177 | $( 'tr.'+this.c.className, this.s.dt.table().body() ).find('td')
|
---|
178 | .attr( 'colspan', this._colspan() );
|
---|
179 | },
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Get the number of columns that a grouping row should span
|
---|
183 | * @private
|
---|
184 | */
|
---|
185 | _colspan: function ()
|
---|
186 | {
|
---|
187 | return this.s.dt.columns().visible().reduce( function (a, b) {
|
---|
188 | return a + b;
|
---|
189 | }, 0 );
|
---|
190 | },
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Update function that is called whenever we need to draw the grouping rows.
|
---|
195 | * This is basically a bootstrap for the self iterative _group and _groupDisplay
|
---|
196 | * methods
|
---|
197 | * @private
|
---|
198 | */
|
---|
199 | _draw: function ()
|
---|
200 | {
|
---|
201 | var dt = this.s.dt;
|
---|
202 | var groupedRows = this._group( 0, dt.rows( { page: 'current' } ).indexes() );
|
---|
203 |
|
---|
204 | this._groupDisplay( 0, groupedRows );
|
---|
205 | },
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Get the grouping information from a data set (index) of rows
|
---|
209 | * @param {number} level Nesting level
|
---|
210 | * @param {DataTables.Api} rows API of the rows to consider for this group
|
---|
211 | * @returns {object[]} Nested grouping information - it is structured like this:
|
---|
212 | * {
|
---|
213 | * dataPoint: 'Edinburgh',
|
---|
214 | * rows: [ 1,2,3,4,5,6,7 ],
|
---|
215 | * children: [ {
|
---|
216 | * dataPoint: 'developer'
|
---|
217 | * rows: [ 1, 2, 3 ]
|
---|
218 | * },
|
---|
219 | * {
|
---|
220 | * dataPoint: 'support',
|
---|
221 | * rows: [ 4, 5, 6, 7 ]
|
---|
222 | * } ]
|
---|
223 | * }
|
---|
224 | * @private
|
---|
225 | */
|
---|
226 | _group: function ( level, rows ) {
|
---|
227 | var fns = $.isArray( this.c.dataSrc ) ? this.c.dataSrc : [ this.c.dataSrc ];
|
---|
228 | var fn = DataTable.ext.oApi._fnGetObjectDataFn( fns[ level ] );
|
---|
229 | var dt = this.s.dt;
|
---|
230 | var group, last;
|
---|
231 | var data = [];
|
---|
232 | var that = this;
|
---|
233 |
|
---|
234 | for ( var i=0, ien=rows.length ; i<ien ; i++ ) {
|
---|
235 | var rowIndex = rows[i];
|
---|
236 | var rowData = dt.row( rowIndex ).data();
|
---|
237 | var group = fn( rowData );
|
---|
238 |
|
---|
239 | if ( group === null || group === undefined ) {
|
---|
240 | group = that.c.emptyDataGroup;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if ( last === undefined || group !== last ) {
|
---|
244 | data.push( {
|
---|
245 | dataPoint: group,
|
---|
246 | rows: []
|
---|
247 | } );
|
---|
248 |
|
---|
249 | last = group;
|
---|
250 | }
|
---|
251 |
|
---|
252 | data[ data.length-1 ].rows.push( rowIndex );
|
---|
253 | }
|
---|
254 |
|
---|
255 | if ( fns[ level+1 ] !== undefined ) {
|
---|
256 | for ( var i=0, ien=data.length ; i<ien ; i++ ) {
|
---|
257 | data[i].children = this._group( level+1, data[i].rows );
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | return data;
|
---|
262 | },
|
---|
263 |
|
---|
264 | /**
|
---|
265 | * Row group display - insert the rows into the document
|
---|
266 | * @param {number} level Nesting level
|
---|
267 | * @param {object[]} groups Takes the nested array from `_group`
|
---|
268 | * @private
|
---|
269 | */
|
---|
270 | _groupDisplay: function ( level, groups )
|
---|
271 | {
|
---|
272 | var dt = this.s.dt;
|
---|
273 | var display;
|
---|
274 |
|
---|
275 | for ( var i=0, ien=groups.length ; i<ien ; i++ ) {
|
---|
276 | var group = groups[i];
|
---|
277 | var groupName = group.dataPoint;
|
---|
278 | var row;
|
---|
279 | var rows = group.rows;
|
---|
280 |
|
---|
281 | if ( this.c.startRender ) {
|
---|
282 | display = this.c.startRender.call( this, dt.rows(rows), groupName, level );
|
---|
283 | row = this._rowWrap( display, this.c.startClassName, level );
|
---|
284 |
|
---|
285 | if ( row ) {
|
---|
286 | row.insertBefore( dt.row( rows[0] ).node() );
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | if ( this.c.endRender ) {
|
---|
291 | display = this.c.endRender.call( this, dt.rows(rows), groupName, level );
|
---|
292 | row = this._rowWrap( display, this.c.endClassName, level );
|
---|
293 |
|
---|
294 | if ( row ) {
|
---|
295 | row.insertAfter( dt.row( rows[ rows.length-1 ] ).node() );
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | if ( group.children ) {
|
---|
300 | this._groupDisplay( level+1, group.children );
|
---|
301 | }
|
---|
302 | }
|
---|
303 | },
|
---|
304 |
|
---|
305 | /**
|
---|
306 | * Take a rendered value from an end user and make it suitable for display
|
---|
307 | * as a row, by wrapping it in a row, or detecting that it is a row.
|
---|
308 | * @param {node|jQuery|string} display Display value
|
---|
309 | * @param {string} className Class to add to the row
|
---|
310 | * @param {array} group
|
---|
311 | * @param {number} group level
|
---|
312 | * @private
|
---|
313 | */
|
---|
314 | _rowWrap: function ( display, className, level )
|
---|
315 | {
|
---|
316 | var row;
|
---|
317 |
|
---|
318 | if ( display === null || display === '' ) {
|
---|
319 | display = this.c.emptyDataGroup;
|
---|
320 | }
|
---|
321 |
|
---|
322 | if ( display === undefined || display === null ) {
|
---|
323 | return null;
|
---|
324 | }
|
---|
325 |
|
---|
326 | if ( typeof display === 'object' && display.nodeName && display.nodeName.toLowerCase() === 'tr') {
|
---|
327 | row = $(display);
|
---|
328 | }
|
---|
329 | else if (display instanceof $ && display.length && display[0].nodeName.toLowerCase() === 'tr') {
|
---|
330 | row = display;
|
---|
331 | }
|
---|
332 | else {
|
---|
333 | row = $('<tr/>')
|
---|
334 | .append(
|
---|
335 | $('<td/>')
|
---|
336 | .attr( 'colspan', this._colspan() )
|
---|
337 | .append( display )
|
---|
338 | );
|
---|
339 | }
|
---|
340 |
|
---|
341 | return row
|
---|
342 | .addClass( this.c.className )
|
---|
343 | .addClass( className )
|
---|
344 | .addClass( 'dtrg-level-'+level );
|
---|
345 | }
|
---|
346 | } );
|
---|
347 |
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * RowGroup default settings for initialisation
|
---|
351 | *
|
---|
352 | * @namespace
|
---|
353 | * @name RowGroup.defaults
|
---|
354 | * @static
|
---|
355 | */
|
---|
356 | RowGroup.defaults = {
|
---|
357 | /**
|
---|
358 | * Class to apply to grouping rows - applied to both the start and
|
---|
359 | * end grouping rows.
|
---|
360 | * @type string
|
---|
361 | */
|
---|
362 | className: 'dtrg-group',
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Data property from which to read the grouping information
|
---|
366 | * @type string|integer|array
|
---|
367 | */
|
---|
368 | dataSrc: 0,
|
---|
369 |
|
---|
370 | /**
|
---|
371 | * Text to show if no data is found for a group
|
---|
372 | * @type string
|
---|
373 | */
|
---|
374 | emptyDataGroup: 'No group',
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Initial enablement state
|
---|
378 | * @boolean
|
---|
379 | */
|
---|
380 | enable: true,
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Class name to give to the end grouping row
|
---|
384 | * @type string
|
---|
385 | */
|
---|
386 | endClassName: 'dtrg-end',
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * End grouping label function
|
---|
390 | * @function
|
---|
391 | */
|
---|
392 | endRender: null,
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Class name to give to the start grouping row
|
---|
396 | * @type string
|
---|
397 | */
|
---|
398 | startClassName: 'dtrg-start',
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Start grouping label function
|
---|
402 | * @function
|
---|
403 | */
|
---|
404 | startRender: function ( rows, group ) {
|
---|
405 | return group;
|
---|
406 | }
|
---|
407 | };
|
---|
408 |
|
---|
409 |
|
---|
410 | RowGroup.version = "1.1.1";
|
---|
411 |
|
---|
412 |
|
---|
413 | $.fn.dataTable.RowGroup = RowGroup;
|
---|
414 | $.fn.DataTable.RowGroup = RowGroup;
|
---|
415 |
|
---|
416 |
|
---|
417 | DataTable.Api.register( 'rowGroup()', function () {
|
---|
418 | return this;
|
---|
419 | } );
|
---|
420 |
|
---|
421 | DataTable.Api.register( 'rowGroup().disable()', function () {
|
---|
422 | return this.iterator( 'table', function (ctx) {
|
---|
423 | if ( ctx.rowGroup ) {
|
---|
424 | ctx.rowGroup.enable( false );
|
---|
425 | }
|
---|
426 | } );
|
---|
427 | } );
|
---|
428 |
|
---|
429 | DataTable.Api.register( 'rowGroup().enable()', function ( opts ) {
|
---|
430 | return this.iterator( 'table', function (ctx) {
|
---|
431 | if ( ctx.rowGroup ) {
|
---|
432 | ctx.rowGroup.enable( opts === undefined ? true : opts );
|
---|
433 | }
|
---|
434 | } );
|
---|
435 | } );
|
---|
436 |
|
---|
437 | DataTable.Api.register( 'rowGroup().dataSrc()', function ( val ) {
|
---|
438 | if ( val === undefined ) {
|
---|
439 | return this.context[0].rowGroup.dataSrc();
|
---|
440 | }
|
---|
441 |
|
---|
442 | return this.iterator( 'table', function (ctx) {
|
---|
443 | if ( ctx.rowGroup ) {
|
---|
444 | ctx.rowGroup.dataSrc( val );
|
---|
445 | }
|
---|
446 | } );
|
---|
447 | } );
|
---|
448 |
|
---|
449 |
|
---|
450 | // Attach a listener to the document which listens for DataTables initialisation
|
---|
451 | // events so we can automatically initialise
|
---|
452 | $(document).on( 'preInit.dt.dtrg', function (e, settings, json) {
|
---|
453 | if ( e.namespace !== 'dt' ) {
|
---|
454 | return;
|
---|
455 | }
|
---|
456 |
|
---|
457 | var init = settings.oInit.rowGroup;
|
---|
458 | var defaults = DataTable.defaults.rowGroup;
|
---|
459 |
|
---|
460 | if ( init || defaults ) {
|
---|
461 | var opts = $.extend( {}, defaults, init );
|
---|
462 |
|
---|
463 | if ( init !== false ) {
|
---|
464 | new RowGroup( settings, opts );
|
---|
465 | }
|
---|
466 | }
|
---|
467 | } );
|
---|
468 |
|
---|
469 |
|
---|
470 | return RowGroup;
|
---|
471 |
|
---|
472 | }));
|
---|