[7304c7f] | 1 | /*!
|
---|
| 2 | * Column visibility buttons for Buttons and DataTables.
|
---|
| 3 | * 2016 SpryMedia Ltd - datatables.net/license
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | (function( factory ){
|
---|
| 7 | if ( typeof define === 'function' && define.amd ) {
|
---|
| 8 | // AMD
|
---|
| 9 | define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
|
---|
| 10 | return factory( $, window, document );
|
---|
| 11 | } );
|
---|
| 12 | }
|
---|
| 13 | else if ( typeof exports === 'object' ) {
|
---|
| 14 | // CommonJS
|
---|
| 15 | module.exports = function (root, $) {
|
---|
| 16 | if ( ! root ) {
|
---|
| 17 | root = window;
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | if ( ! $ || ! $.fn.dataTable ) {
|
---|
| 21 | $ = require('datatables.net')(root, $).$;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | if ( ! $.fn.dataTable.Buttons ) {
|
---|
| 25 | require('datatables.net-buttons')(root, $);
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | return factory( $, root, root.document );
|
---|
| 29 | };
|
---|
| 30 | }
|
---|
| 31 | else {
|
---|
| 32 | // Browser
|
---|
| 33 | factory( jQuery, window, document );
|
---|
| 34 | }
|
---|
| 35 | }(function( $, window, document, undefined ) {
|
---|
| 36 | 'use strict';
|
---|
| 37 | var DataTable = $.fn.dataTable;
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | $.extend( DataTable.ext.buttons, {
|
---|
| 41 | // A collection of column visibility buttons
|
---|
| 42 | colvis: function ( dt, conf ) {
|
---|
| 43 | return {
|
---|
| 44 | extend: 'collection',
|
---|
| 45 | text: function ( dt ) {
|
---|
| 46 | return dt.i18n( 'buttons.colvis', 'Column visibility' );
|
---|
| 47 | },
|
---|
| 48 | className: 'buttons-colvis',
|
---|
| 49 | buttons: [ {
|
---|
| 50 | extend: 'columnsToggle',
|
---|
| 51 | columns: conf.columns,
|
---|
| 52 | columnText: conf.columnText
|
---|
| 53 | } ]
|
---|
| 54 | };
|
---|
| 55 | },
|
---|
| 56 |
|
---|
| 57 | // Selected columns with individual buttons - toggle column visibility
|
---|
| 58 | columnsToggle: function ( dt, conf ) {
|
---|
| 59 | var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
|
---|
| 60 | return {
|
---|
| 61 | extend: 'columnToggle',
|
---|
| 62 | columns: idx,
|
---|
| 63 | columnText: conf.columnText
|
---|
| 64 | };
|
---|
| 65 | } ).toArray();
|
---|
| 66 |
|
---|
| 67 | return columns;
|
---|
| 68 | },
|
---|
| 69 |
|
---|
| 70 | // Single button to toggle column visibility
|
---|
| 71 | columnToggle: function ( dt, conf ) {
|
---|
| 72 | return {
|
---|
| 73 | extend: 'columnVisibility',
|
---|
| 74 | columns: conf.columns,
|
---|
| 75 | columnText: conf.columnText
|
---|
| 76 | };
|
---|
| 77 | },
|
---|
| 78 |
|
---|
| 79 | // Selected columns with individual buttons - set column visibility
|
---|
| 80 | columnsVisibility: function ( dt, conf ) {
|
---|
| 81 | var columns = dt.columns( conf.columns ).indexes().map( function ( idx ) {
|
---|
| 82 | return {
|
---|
| 83 | extend: 'columnVisibility',
|
---|
| 84 | columns: idx,
|
---|
| 85 | visibility: conf.visibility,
|
---|
| 86 | columnText: conf.columnText
|
---|
| 87 | };
|
---|
| 88 | } ).toArray();
|
---|
| 89 |
|
---|
| 90 | return columns;
|
---|
| 91 | },
|
---|
| 92 |
|
---|
| 93 | // Single button to set column visibility
|
---|
| 94 | columnVisibility: {
|
---|
| 95 | columns: undefined, // column selector
|
---|
| 96 | text: function ( dt, button, conf ) {
|
---|
| 97 | return conf._columnText( dt, conf );
|
---|
| 98 | },
|
---|
| 99 | className: 'buttons-columnVisibility',
|
---|
| 100 | action: function ( e, dt, button, conf ) {
|
---|
| 101 | var col = dt.columns( conf.columns );
|
---|
| 102 | var curr = col.visible();
|
---|
| 103 |
|
---|
| 104 | col.visible( conf.visibility !== undefined ?
|
---|
| 105 | conf.visibility :
|
---|
| 106 | ! (curr.length ? curr[0] : false )
|
---|
| 107 | );
|
---|
| 108 | },
|
---|
| 109 | init: function ( dt, button, conf ) {
|
---|
| 110 | var that = this;
|
---|
| 111 | button.attr( 'data-cv-idx', conf.columns );
|
---|
| 112 |
|
---|
| 113 | dt
|
---|
| 114 | .on( 'column-visibility.dt'+conf.namespace, function (e, settings) {
|
---|
| 115 | if ( ! settings.bDestroying && settings.nTable == dt.settings()[0].nTable ) {
|
---|
| 116 | that.active( dt.column( conf.columns ).visible() );
|
---|
| 117 | }
|
---|
| 118 | } )
|
---|
| 119 | .on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) {
|
---|
| 120 | if ( dt.columns( conf.columns ).count() !== 1 ) {
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | // This button controls the same column index but the text for the column has
|
---|
| 125 | // changed
|
---|
| 126 | button.text( conf._columnText( dt, conf ) );
|
---|
| 127 |
|
---|
| 128 | // Since its a different column, we need to check its visibility
|
---|
| 129 | that.active( dt.column( conf.columns ).visible() );
|
---|
| 130 | } );
|
---|
| 131 |
|
---|
| 132 | this.active( dt.column( conf.columns ).visible() );
|
---|
| 133 | },
|
---|
| 134 | destroy: function ( dt, button, conf ) {
|
---|
| 135 | dt
|
---|
| 136 | .off( 'column-visibility.dt'+conf.namespace )
|
---|
| 137 | .off( 'column-reorder.dt'+conf.namespace );
|
---|
| 138 | },
|
---|
| 139 |
|
---|
| 140 | _columnText: function ( dt, conf ) {
|
---|
| 141 | // Use DataTables' internal data structure until this is presented
|
---|
| 142 | // is a public API. The other option is to use
|
---|
| 143 | // `$( column(col).node() ).text()` but the node might not have been
|
---|
| 144 | // populated when Buttons is constructed.
|
---|
| 145 | var idx = dt.column( conf.columns ).index();
|
---|
| 146 | var title = dt.settings()[0].aoColumns[ idx ].sTitle
|
---|
| 147 | .replace(/\n/g," ") // remove new lines
|
---|
| 148 | .replace(/<br\s*\/?>/gi, " ") // replace line breaks with spaces
|
---|
| 149 | .replace(/<select(.*?)<\/select>/g, "") // remove select tags, including options text
|
---|
| 150 | .replace(/<!\-\-.*?\-\->/g, "") // strip HTML comments
|
---|
| 151 | .replace(/<.*?>/g, "") // strip HTML
|
---|
| 152 | .replace(/^\s+|\s+$/g,""); // trim
|
---|
| 153 |
|
---|
| 154 | return conf.columnText ?
|
---|
| 155 | conf.columnText( dt, idx, title ) :
|
---|
| 156 | title;
|
---|
| 157 | }
|
---|
| 158 | },
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | colvisRestore: {
|
---|
| 162 | className: 'buttons-colvisRestore',
|
---|
| 163 |
|
---|
| 164 | text: function ( dt ) {
|
---|
| 165 | return dt.i18n( 'buttons.colvisRestore', 'Restore visibility' );
|
---|
| 166 | },
|
---|
| 167 |
|
---|
| 168 | init: function ( dt, button, conf ) {
|
---|
| 169 | conf._visOriginal = dt.columns().indexes().map( function ( idx ) {
|
---|
| 170 | return dt.column( idx ).visible();
|
---|
| 171 | } ).toArray();
|
---|
| 172 | },
|
---|
| 173 |
|
---|
| 174 | action: function ( e, dt, button, conf ) {
|
---|
| 175 | dt.columns().every( function ( i ) {
|
---|
| 176 | // Take into account that ColReorder might have disrupted our
|
---|
| 177 | // indexes
|
---|
| 178 | var idx = dt.colReorder && dt.colReorder.transpose ?
|
---|
| 179 | dt.colReorder.transpose( i, 'toOriginal' ) :
|
---|
| 180 | i;
|
---|
| 181 |
|
---|
| 182 | this.visible( conf._visOriginal[ idx ] );
|
---|
| 183 | } );
|
---|
| 184 | }
|
---|
| 185 | },
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | colvisGroup: {
|
---|
| 189 | className: 'buttons-colvisGroup',
|
---|
| 190 |
|
---|
| 191 | action: function ( e, dt, button, conf ) {
|
---|
| 192 | dt.columns( conf.show ).visible( true, false );
|
---|
| 193 | dt.columns( conf.hide ).visible( false, false );
|
---|
| 194 |
|
---|
| 195 | dt.columns.adjust();
|
---|
| 196 | },
|
---|
| 197 |
|
---|
| 198 | show: [],
|
---|
| 199 |
|
---|
| 200 | hide: []
|
---|
| 201 | }
|
---|
| 202 | } );
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 | return DataTable.Buttons;
|
---|
| 206 | }));
|
---|