buttons.colVis.js 4.8 KB

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