buttons.colVis.js (5156B)
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 112 dt 113 .on( 'column-visibility.dt'+conf.namespace, function (e, settings) { 114 if ( ! settings.bDestroying ) { 115 that.active( dt.column( conf.columns ).visible() ); 116 } 117 } ) 118 .on( 'column-reorder.dt'+conf.namespace, function (e, settings, details) { 119 // Don't rename buttons based on column name if the button 120 // controls more than one column! 121 if ( dt.columns( conf.columns ).count() !== 1 ) { 122 return; 123 } 124 125 if ( typeof conf.columns === 'number' ) { 126 conf.columns = details.mapping[ conf.columns ]; 127 } 128 129 var col = dt.column( conf.columns ); 130 131 that.text( conf._columnText( dt, conf ) ); 132 that.active( col.visible() ); 133 } ); 134 135 this.active( dt.column( conf.columns ).visible() ); 136 }, 137 destroy: function ( dt, button, conf ) { 138 dt 139 .off( 'column-visibility.dt'+conf.namespace ) 140 .off( 'column-reorder.dt'+conf.namespace ); 141 }, 142 143 _columnText: function ( dt, conf ) { 144 // Use DataTables' internal data structure until this is presented 145 // is a public API. The other option is to use 146 // `$( column(col).node() ).text()` but the node might not have been 147 // populated when Buttons is constructed. 148 var idx = dt.column( conf.columns ).index(); 149 var title = dt.settings()[0].aoColumns[ idx ].sTitle 150 .replace(/\n/g," ") // remove new lines 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 }));