GPQAPP

Форк
0
/
dataTables.rowGroup.js 
486 строк · 10.3 Кб
1
/*! RowGroup 1.1.3
2
 * ©2017-2021 SpryMedia Ltd - datatables.net/license
3
 */
4

5
/**
6
 * @summary     RowGroup
7
 * @description RowGrouping for DataTables
8
 * @version     1.1.3
9
 * @file        dataTables.rowGroup.js
10
 * @author      SpryMedia Ltd (www.sprymedia.co.uk)
11
 * @contact     datatables.net
12
 * @copyright   Copyright 2017-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

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
	 * Get enabled flag
140
	 * @returns boolean
141
	 */
142
	enabled: function ()
143
	{
144
		return this.c.enable;
145
	},
146

147

148
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
149
	 * Constructor
150
	 */
151
	_constructor: function ()
152
	{
153
		var that = this;
154
		var dt = this.s.dt;
155
		var hostSettings = dt.settings()[0];
156

157
		dt.on( 'draw.dtrg', function (e, s) {
158
			if ( that.c.enable && hostSettings === s ) {
159
				that._draw();
160
			}
161
		} );
162

163
		dt.on( 'column-visibility.dt.dtrg responsive-resize.dt.dtrg', function () {
164
			that._adjustColspan();
165
		} );
166

167
		dt.on( 'destroy', function () {
168
			dt.off( '.dtrg' );
169
		} );
170
	},
171

172

173
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
174
	 * Private methods
175
	 */
176

177
	/**
178
	 * Adjust column span when column visibility changes
179
	 * @private
180
	 */
181
	_adjustColspan: function ()
182
	{
183
		$( 'tr.'+this.c.className, this.s.dt.table().body() ).find('td:visible')
184
			.attr( 'colspan', this._colspan() );
185
	},
186

187
	/**
188
	 * Get the number of columns that a grouping row should span
189
	 * @private
190
	 */
191
	_colspan: function ()
192
	{
193
		return this.s.dt.columns().visible().reduce( function (a, b) {
194
			return a + b;
195
		}, 0 );
196
	},
197

198

199
	/**
200
	 * Update function that is called whenever we need to draw the grouping rows.
201
	 * This is basically a bootstrap for the self iterative _group and _groupDisplay
202
	 * methods
203
	 * @private
204
	 */
205
	_draw: function ()
206
	{
207
		var dt = this.s.dt;
208
		var groupedRows = this._group( 0, dt.rows( { page: 'current' } ).indexes() );
209

210
		this._groupDisplay( 0, groupedRows );
211
	},
212

213
	/**
214
	 * Get the grouping information from a data set (index) of rows
215
	 * @param {number} level Nesting level
216
	 * @param {DataTables.Api} rows API of the rows to consider for this group
217
	 * @returns {object[]} Nested grouping information - it is structured like this:
218
	 *	{
219
	 *		dataPoint: 'Edinburgh',
220
	 *		rows: [ 1,2,3,4,5,6,7 ],
221
	 *		children: [ {
222
	 *			dataPoint: 'developer'
223
	 *			rows: [ 1, 2, 3 ]
224
	 *		},
225
	 *		{
226
	 *			dataPoint: 'support',
227
	 *			rows: [ 4, 5, 6, 7 ]
228
	 *		} ]
229
	 *	}
230
	 * @private
231
	 */
232
	_group: function ( level, rows ) {
233
		var fns = Array.isArray( this.c.dataSrc ) ? this.c.dataSrc : [ this.c.dataSrc ];
234
		var fn = DataTable.ext.oApi._fnGetObjectDataFn( fns[ level ] );
235
		var dt = this.s.dt;
236
		var group, last;
237
		var data = [];
238
		var that = this;
239

240
		for ( var i=0, ien=rows.length ; i<ien ; i++ ) {
241
			var rowIndex = rows[i];
242
			var rowData = dt.row( rowIndex ).data();
243
			var group = fn( rowData );
244

245
			if ( group === null || group === undefined ) {
246
				group = that.c.emptyDataGroup;
247
			}
248
			
249
			if ( last === undefined || group !== last ) {
250
				data.push( {
251
					dataPoint: group,
252
					rows: []
253
				} );
254

255
				last = group;
256
			}
257

258
			data[ data.length-1 ].rows.push( rowIndex );
259
		}
260

261
		if ( fns[ level+1 ] !== undefined ) {
262
			for ( var i=0, ien=data.length ; i<ien ; i++ ) {
263
				data[i].children = this._group( level+1, data[i].rows );
264
			}
265
		}
266

267
		return data;
268
	},
269

270
	/**
271
	 * Row group display - insert the rows into the document
272
	 * @param {number} level Nesting level
273
	 * @param {object[]} groups Takes the nested array from `_group`
274
	 * @private
275
	 */
276
	_groupDisplay: function ( level, groups )
277
	{
278
		var dt = this.s.dt;
279
		var display;
280
	
281
		for ( var i=0, ien=groups.length ; i<ien ; i++ ) {
282
			var group = groups[i];
283
			var groupName = group.dataPoint;
284
			var row;
285
			var rows = group.rows;
286

287
			if ( this.c.startRender ) {
288
				display = this.c.startRender.call( this, dt.rows(rows), groupName, level );
289
				row = this._rowWrap( display, this.c.startClassName, level );
290

291
				if ( row ) {
292
					row.insertBefore( dt.row( rows[0] ).node() );
293
				}
294
			}
295

296
			if ( this.c.endRender ) {
297
				display = this.c.endRender.call( this, dt.rows(rows), groupName, level );
298
				row = this._rowWrap( display, this.c.endClassName, level );
299

300
				if ( row ) {
301
					row.insertAfter( dt.row( rows[ rows.length-1 ] ).node() );
302
				}
303
			}
304

305
			if ( group.children ) {
306
				this._groupDisplay( level+1, group.children );
307
			}
308
		}
309
	},
310

311
	/**
312
	 * Take a rendered value from an end user and make it suitable for display
313
	 * as a row, by wrapping it in a row, or detecting that it is a row.
314
	 * @param {node|jQuery|string} display Display value
315
	 * @param {string} className Class to add to the row
316
	 * @param {array} group
317
	 * @param {number} group level
318
	 * @private
319
	 */
320
	_rowWrap: function ( display, className, level )
321
	{
322
		var row;
323
		
324
		if ( display === null || display === '' ) {
325
			display = this.c.emptyDataGroup;
326
		}
327

328
		if ( display === undefined || display === null ) {
329
			return null;
330
		}
331
		
332
		if ( typeof display === 'object' && display.nodeName && display.nodeName.toLowerCase() === 'tr') {
333
			row = $(display);
334
		}
335
		else if (display instanceof $ && display.length && display[0].nodeName.toLowerCase() === 'tr') {
336
			row = display;
337
		}
338
		else {
339
			row = $('<tr/>')
340
				.append(
341
					$('<td/>')
342
						.attr( 'colspan', this._colspan() )
343
						.append( display  )
344
				);
345
		}
346

347
		return row
348
			.addClass( this.c.className )
349
			.addClass( className )
350
			.addClass( 'dtrg-level-'+level );
351
	}
352
} );
353

354

355
/**
356
 * RowGroup default settings for initialisation
357
 *
358
 * @namespace
359
 * @name RowGroup.defaults
360
 * @static
361
 */
362
RowGroup.defaults = {
363
	/**
364
	 * Class to apply to grouping rows - applied to both the start and
365
	 * end grouping rows.
366
	 * @type string
367
	 */
368
	className: 'dtrg-group',
369

370
	/**
371
	 * Data property from which to read the grouping information
372
	 * @type string|integer|array
373
	 */
374
	dataSrc: 0,
375

376
	/**
377
	 * Text to show if no data is found for a group
378
	 * @type string
379
	 */
380
	emptyDataGroup: 'No group',
381

382
	/**
383
	 * Initial enablement state
384
	 * @boolean
385
	 */
386
	enable: true,
387

388
	/**
389
	 * Class name to give to the end grouping row
390
	 * @type string
391
	 */
392
	endClassName: 'dtrg-end',
393

394
	/**
395
	 * End grouping label function
396
	 * @function
397
	 */
398
	endRender: null,
399

400
	/**
401
	 * Class name to give to the start grouping row
402
	 * @type string
403
	 */
404
	startClassName: 'dtrg-start',
405

406
	/**
407
	 * Start grouping label function
408
	 * @function
409
	 */
410
	startRender: function ( rows, group ) {
411
		return group;
412
	}
413
};
414

415

416
RowGroup.version = "1.1.3";
417

418

419
$.fn.dataTable.RowGroup = RowGroup;
420
$.fn.DataTable.RowGroup = RowGroup;
421

422

423
DataTable.Api.register( 'rowGroup()', function () {
424
	return this;
425
} );
426

427
DataTable.Api.register( 'rowGroup().disable()', function () {
428
	return this.iterator( 'table', function (ctx) {
429
		if ( ctx.rowGroup ) {
430
			ctx.rowGroup.enable( false );
431
		}
432
	} );
433
} );
434

435
DataTable.Api.register( 'rowGroup().enable()', function ( opts ) {
436
	return this.iterator( 'table', function (ctx) {
437
		if ( ctx.rowGroup ) {
438
			ctx.rowGroup.enable( opts === undefined ? true : opts );
439
		}
440
	} );
441
} );
442

443
DataTable.Api.register( 'rowGroup().enabled()', function () {
444
	var ctx = this.context;
445

446
	return ctx.length && ctx[0].rowGroup ?
447
		ctx[0].rowGroup.enabled() :
448
		false;
449
} );
450

451
DataTable.Api.register( 'rowGroup().dataSrc()', function ( val ) {
452
	if ( val === undefined ) {
453
		return this.context[0].rowGroup.dataSrc();
454
	}
455

456
	return this.iterator( 'table', function (ctx) {
457
		if ( ctx.rowGroup ) {
458
			ctx.rowGroup.dataSrc( val );
459
		}
460
	} );
461
} );
462

463

464
// Attach a listener to the document which listens for DataTables initialisation
465
// events so we can automatically initialise
466
$(document).on( 'preInit.dt.dtrg', function (e, settings, json) {
467
	if ( e.namespace !== 'dt' ) {
468
		return;
469
	}
470

471
	var init = settings.oInit.rowGroup;
472
	var defaults = DataTable.defaults.rowGroup;
473

474
	if ( init || defaults ) {
475
		var opts = $.extend( {}, defaults, init );
476

477
		if ( init !== false ) {
478
			new RowGroup( settings, opts  );
479
		}
480
	}
481
} );
482

483

484
return RowGroup;
485

486
}));
487

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.