You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.2 KiB

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