LaravelTest
235 строк · 6.0 Кб
1/*!
2* Column visibility buttons for Buttons and DataTables.
3* 2016 SpryMedia Ltd - datatables.net/license
4*/
5
6(function( factory ){7if ( typeof define === 'function' && define.amd ) {8// AMD9define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {10return factory( $, window, document );11} );12}13else if ( typeof exports === 'object' ) {14// CommonJS15module.exports = function (root, $) {16if ( ! root ) {17root = window;18}19
20if ( ! $ || ! $.fn.dataTable ) {21$ = require('datatables.net')(root, $).$;22}23
24if ( ! $.fn.dataTable.Buttons ) {25require('datatables.net-buttons')(root, $);26}27
28return factory( $, root, root.document );29};30}31else {32// Browser33factory( jQuery, window, document );34}35}(function( $, window, document, undefined ) {36'use strict';37var DataTable = $.fn.dataTable;38
39
40$.extend( DataTable.ext.buttons, {41// A collection of column visibility buttons42colvis: function ( dt, conf ) {43var node = null;44var buttonConf = {45extend: 'collection',46init: function ( dt, n ) {47node = n;48},49text: function ( dt ) {50return dt.i18n( 'buttons.colvis', 'Column visibility' );51},52className: 'buttons-colvis',53closeButton: false,54buttons: [ {55extend: 'columnsToggle',56columns: conf.columns,57columnText: conf.columnText58} ]59};60
61// Rebuild the collection with the new column structure if columns are reordered62dt.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {63// console.log(node);64// console.log('node', dt.button(null, node).node());65dt.button(null, dt.button(null, node).node()).collectionRebuild([{66extend: 'columnsToggle',67columns: conf.columns,68columnText: conf.columnText69}]);70});71
72return buttonConf;73},74
75// Selected columns with individual buttons - toggle column visibility76columnsToggle: function ( dt, conf ) {77var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {78return {79extend: 'columnToggle',80columns: idx,81columnText: conf.columnText82};83} ).toArray();84
85return columns;86},87
88// Single button to toggle column visibility89columnToggle: function ( dt, conf ) {90return {91extend: 'columnVisibility',92columns: conf.columns,93columnText: conf.columnText94};95},96
97// Selected columns with individual buttons - set column visibility98columnsVisibility: function ( dt, conf ) {99var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {100return {101extend: 'columnVisibility',102columns: idx,103visibility: conf.visibility,104columnText: conf.columnText105};106} ).toArray();107
108return columns;109},110
111// Single button to set column visibility112columnVisibility: {113columns: undefined, // column selector114text: function ( dt, button, conf ) {115return conf._columnText( dt, conf );116},117className: 'buttons-columnVisibility',118action: function ( e, dt, button, conf ) {119var col = dt.columns( conf.columns );120var curr = col.visible();121
122col.visible( conf.visibility !== undefined ?123conf.visibility :124! (curr.length ? curr[0] : false )125);126},127init: function ( dt, button, conf ) {128var that = this;129button.attr( 'data-cv-idx', conf.columns );130
131dt
132.on( 'column-visibility.dt'+conf.namespace, function (e, settings) {133if ( ! settings.bDestroying && settings.nTable == dt.settings()[0].nTable ) {134that.active( dt.column( conf.columns ).visible() );135}136} )137.on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {138// Button has been removed from the DOM139if ( conf.destroying ) {140return;141}142
143if ( dt.columns( conf.columns ).count() !== 1 ) {144return;145}146
147// This button controls the same column index but the text for the column has148// changed149that.text( conf._columnText( dt, conf ) );150
151// Since its a different column, we need to check its visibility152that.active( dt.column( conf.columns ).visible() );153} );154
155this.active( dt.column( conf.columns ).visible() );156},157destroy: function ( dt, button, conf ) {158dt
159.off( 'column-visibility.dt'+conf.namespace )160.off( 'column-reorder.dt'+conf.namespace );161},162
163_columnText: function ( dt, conf ) {164// Use DataTables' internal data structure until this is presented165// is a public API. The other option is to use166// `$( column(col).node() ).text()` but the node might not have been167// populated when Buttons is constructed.168var idx = dt.column( conf.columns ).index();169var title = dt.settings()[0].aoColumns[ idx ].sTitle;170
171if (! title) {172title = dt.column(idx).header().innerHTML;173}174
175title = title176.replace(/\n/g," ") // remove new lines177.replace(/<br\s*\/?>/gi, " ") // replace line breaks with spaces178.replace(/<select(.*?)<\/select>/g, "") // remove select tags, including options text179.replace(/<!\-\-.*?\-\->/g, "") // strip HTML comments180.replace(/<.*?>/g, "") // strip HTML181.replace(/^\s+|\s+$/g,""); // trim182
183return conf.columnText ?184conf.columnText( dt, idx, title ) :185title;186}187},188
189
190colvisRestore: {191className: 'buttons-colvisRestore',192
193text: function ( dt ) {194return dt.i18n( 'buttons.colvisRestore', 'Restore visibility' );195},196
197init: function ( dt, button, conf ) {198conf._visOriginal = dt.columns().indexes().map( function ( idx ) {199return dt.column( idx ).visible();200} ).toArray();201},202
203action: function ( e, dt, button, conf ) {204dt.columns().every( function ( i ) {205// Take into account that ColReorder might have disrupted our206// indexes207var idx = dt.colReorder && dt.colReorder.transpose ?208dt.colReorder.transpose( i, 'toOriginal' ) :209i;210
211this.visible( conf._visOriginal[ idx ] );212} );213}214},215
216
217colvisGroup: {218className: 'buttons-colvisGroup',219
220action: function ( e, dt, button, conf ) {221dt.columns( conf.show ).visible( true, false );222dt.columns( conf.hide ).visible( false, false );223
224dt.columns.adjust();225},226
227show: [],228
229hide: []230}231} );232
233
234return DataTable.Buttons;235}));236