LaravelTest
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 ){25if ( typeof define === 'function' && define.amd ) {26// AMD27define( ['jquery', 'datatables.net'], function ( $ ) {28return factory( $, window, document );29} );30}31else if ( typeof exports === 'object' ) {32// CommonJS33module.exports = function (root, $) {34if ( ! root ) {35root = window;36}37
38if ( ! $ || ! $.fn.dataTable ) {39$ = require('datatables.net')(root, $).$;40}41
42return factory( $, root, root.document );43};44}45else {46// Browser47factory( jQuery, window, document );48}49}(function( $, window, document, undefined ) {50'use strict';51var DataTable = $.fn.dataTable;52
53
54var RowGroup = function ( dt, opts ) {55// Sanity check that we are using DataTables 1.10 or newer56if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {57throw 'RowGroup requires DataTables 1.10.8 or newer';58}59
60// User and defaults configuration object61this.c = $.extend( true, {},62DataTable.defaults.rowGroup,63RowGroup.defaults,64opts
65);66
67// Internal settings68this.s = {69dt: new DataTable.Api( dt )70};71
72// DOM items73this.dom = {74
75};76
77// Check if row grouping has already been initialised on this table78var settings = this.s.dt.settings()[0];79var existing = settings.rowGroup;80if ( existing ) {81return existing;82}83
84settings.rowGroup = this;85this._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*/
99dataSrc: function ( val )100{101if ( val === undefined ) {102return this.c.dataSrc;103}104
105var dt = this.s.dt;106
107this.c.dataSrc = val;108
109$(dt.table().node()).triggerHandler( 'rowgroup-datasrc.dt', [ dt, val ] );110
111return this;112},113
114/**115* Disable - need to call draw after this is executed
116* @returns RowGroup
117*/
118disable: function ()119{120this.c.enable = false;121return this;122},123
124/**125* Enable - need to call draw after this is executed
126* @returns RowGroup
127*/
128enable: function ( flag )129{130if ( flag === false ) {131return this.disable();132}133
134this.c.enable = true;135return this;136},137
138/**139* Get enabled flag
140* @returns boolean
141*/
142enabled: function ()143{144return this.c.enable;145},146
147
148/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *149* Constructor
150*/
151_constructor: function ()152{153var that = this;154var dt = this.s.dt;155var hostSettings = dt.settings()[0];156
157dt.on( 'draw.dtrg', function (e, s) {158if ( that.c.enable && hostSettings === s ) {159that._draw();160}161} );162
163dt.on( 'column-visibility.dt.dtrg responsive-resize.dt.dtrg', function () {164that._adjustColspan();165} );166
167dt.on( 'destroy', function () {168dt.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{193return this.s.dt.columns().visible().reduce( function (a, b) {194return 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{207var dt = this.s.dt;208var groupedRows = this._group( 0, dt.rows( { page: 'current' } ).indexes() );209
210this._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 ) {233var fns = Array.isArray( this.c.dataSrc ) ? this.c.dataSrc : [ this.c.dataSrc ];234var fn = DataTable.ext.oApi._fnGetObjectDataFn( fns[ level ] );235var dt = this.s.dt;236var group, last;237var data = [];238var that = this;239
240for ( var i=0, ien=rows.length ; i<ien ; i++ ) {241var rowIndex = rows[i];242var rowData = dt.row( rowIndex ).data();243var group = fn( rowData );244
245if ( group === null || group === undefined ) {246group = that.c.emptyDataGroup;247}248
249if ( last === undefined || group !== last ) {250data.push( {251dataPoint: group,252rows: []253} );254
255last = group;256}257
258data[ data.length-1 ].rows.push( rowIndex );259}260
261if ( fns[ level+1 ] !== undefined ) {262for ( var i=0, ien=data.length ; i<ien ; i++ ) {263data[i].children = this._group( level+1, data[i].rows );264}265}266
267return 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{278var dt = this.s.dt;279var display;280
281for ( var i=0, ien=groups.length ; i<ien ; i++ ) {282var group = groups[i];283var groupName = group.dataPoint;284var row;285var rows = group.rows;286
287if ( this.c.startRender ) {288display = this.c.startRender.call( this, dt.rows(rows), groupName, level );289row = this._rowWrap( display, this.c.startClassName, level );290
291if ( row ) {292row.insertBefore( dt.row( rows[0] ).node() );293}294}295
296if ( this.c.endRender ) {297display = this.c.endRender.call( this, dt.rows(rows), groupName, level );298row = this._rowWrap( display, this.c.endClassName, level );299
300if ( row ) {301row.insertAfter( dt.row( rows[ rows.length-1 ] ).node() );302}303}304
305if ( group.children ) {306this._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{322var row;323
324if ( display === null || display === '' ) {325display = this.c.emptyDataGroup;326}327
328if ( display === undefined || display === null ) {329return null;330}331
332if ( typeof display === 'object' && display.nodeName && display.nodeName.toLowerCase() === 'tr') {333row = $(display);334}335else if (display instanceof $ && display.length && display[0].nodeName.toLowerCase() === 'tr') {336row = display;337}338else {339row = $('<tr/>')340.append(341$('<td/>')342.attr( 'colspan', this._colspan() )343.append( display )344);345}346
347return row348.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*/
362RowGroup.defaults = {363/**364* Class to apply to grouping rows - applied to both the start and
365* end grouping rows.
366* @type string
367*/
368className: 'dtrg-group',369
370/**371* Data property from which to read the grouping information
372* @type string|integer|array
373*/
374dataSrc: 0,375
376/**377* Text to show if no data is found for a group
378* @type string
379*/
380emptyDataGroup: 'No group',381
382/**383* Initial enablement state
384* @boolean
385*/
386enable: true,387
388/**389* Class name to give to the end grouping row
390* @type string
391*/
392endClassName: 'dtrg-end',393
394/**395* End grouping label function
396* @function
397*/
398endRender: null,399
400/**401* Class name to give to the start grouping row
402* @type string
403*/
404startClassName: 'dtrg-start',405
406/**407* Start grouping label function
408* @function
409*/
410startRender: function ( rows, group ) {411return group;412}413};414
415
416RowGroup.version = "1.1.3";417
418
419$.fn.dataTable.RowGroup = RowGroup;420$.fn.DataTable.RowGroup = RowGroup;421
422
423DataTable.Api.register( 'rowGroup()', function () {424return this;425} );426
427DataTable.Api.register( 'rowGroup().disable()', function () {428return this.iterator( 'table', function (ctx) {429if ( ctx.rowGroup ) {430ctx.rowGroup.enable( false );431}432} );433} );434
435DataTable.Api.register( 'rowGroup().enable()', function ( opts ) {436return this.iterator( 'table', function (ctx) {437if ( ctx.rowGroup ) {438ctx.rowGroup.enable( opts === undefined ? true : opts );439}440} );441} );442
443DataTable.Api.register( 'rowGroup().enabled()', function () {444var ctx = this.context;445
446return ctx.length && ctx[0].rowGroup ?447ctx[0].rowGroup.enabled() :448false;449} );450
451DataTable.Api.register( 'rowGroup().dataSrc()', function ( val ) {452if ( val === undefined ) {453return this.context[0].rowGroup.dataSrc();454}455
456return this.iterator( 'table', function (ctx) {457if ( ctx.rowGroup ) {458ctx.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) {467if ( e.namespace !== 'dt' ) {468return;469}470
471var init = settings.oInit.rowGroup;472var defaults = DataTable.defaults.rowGroup;473
474if ( init || defaults ) {475var opts = $.extend( {}, defaults, init );476
477if ( init !== false ) {478new RowGroup( settings, opts );479}480}481} );482
483
484return RowGroup;485
486}));487