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.

buttons.colVis.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. } else if (typeof exports === 'object') {
  12. // CommonJS
  13. module.exports = function (root, $) {
  14. if (!root) {
  15. root = window;
  16. }
  17. if (!$ || !$.fn.dataTable) {
  18. $ = require('datatables.net')(root, $).$;
  19. }
  20. if (!$.fn.dataTable.Buttons) {
  21. require('datatables.net-buttons')(root, $);
  22. }
  23. return factory($, root, root.document);
  24. };
  25. } else {
  26. // Browser
  27. factory(jQuery, window, document);
  28. }
  29. }(function ($, window, document, undefined) {
  30. 'use strict';
  31. var DataTable = $.fn.dataTable;
  32. $.extend(DataTable.ext.buttons, {
  33. // A collection of column visibility buttons
  34. colvis: function (dt, conf) {
  35. return {
  36. extend: 'collection',
  37. text: function (dt) {
  38. return dt.i18n('buttons.colvis', 'Column visibility');
  39. },
  40. className: 'buttons-colvis',
  41. buttons: [{
  42. extend: 'columnsToggle',
  43. columns: conf.columns,
  44. columnText: conf.columnText
  45. }]
  46. };
  47. },
  48. // Selected columns with individual buttons - toggle column visibility
  49. columnsToggle: function (dt, conf) {
  50. var columns = dt.columns(conf.columns).indexes().map(function (idx) {
  51. return {
  52. extend: 'columnToggle',
  53. columns: idx,
  54. columnText: conf.columnText
  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. columnText: conf.columnText
  65. };
  66. },
  67. // Selected columns with individual buttons - set column visibility
  68. columnsVisibility: function (dt, conf) {
  69. var columns = dt.columns(conf.columns).indexes().map(function (idx) {
  70. return {
  71. extend: 'columnVisibility',
  72. columns: idx,
  73. visibility: conf.visibility,
  74. columnText: conf.columnText
  75. };
  76. }).toArray();
  77. return columns;
  78. },
  79. // Single button to set column visibility
  80. columnVisibility: {
  81. columns: undefined, // column selector
  82. text: function (dt, button, conf) {
  83. return conf._columnText(dt, conf);
  84. },
  85. className: 'buttons-columnVisibility',
  86. action: function (e, dt, button, conf) {
  87. var col = dt.columns(conf.columns);
  88. var curr = col.visible();
  89. col.visible(conf.visibility !== undefined ?
  90. conf.visibility :
  91. !(curr.length ? curr[0] : false)
  92. );
  93. },
  94. init: function (dt, button, conf) {
  95. var that = this;
  96. button.attr('data-cv-idx', conf.columns);
  97. dt
  98. .on('column-visibility.dt' + conf.namespace, function (e, settings) {
  99. if (!settings.bDestroying && settings.nTable == dt.settings()[0].nTable) {
  100. that.active(dt.column(conf.columns).visible());
  101. }
  102. })
  103. .on('column-reorder.dt' + conf.namespace, function (e, settings, details) {
  104. if (dt.columns(conf.columns).count() !== 1) {
  105. return;
  106. }
  107. // This button controls the same column index but the text for the column has
  108. // changed
  109. that.text(conf._columnText(dt, conf));
  110. // Since its a different column, we need to check its visibility
  111. that.active(dt.column(conf.columns).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, conf) {
  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(conf.columns).index();
  126. var title = dt.settings()[0].aoColumns[idx].sTitle;
  127. if (!title) {
  128. title = dt.column(idx).header().innerHTML;
  129. }
  130. title = title
  131. .replace(/\n/g, " ") // remove new lines
  132. .replace(/<br\s*\/?>/gi, " ") // replace line breaks with spaces
  133. .replace(/<select(.*?)<\/select>/g, "") // remove select tags, including options text
  134. .replace(/<!\-\-.*?\-\->/g, "") // strip HTML comments
  135. .replace(/<.*?>/g, "") // strip HTML
  136. .replace(/^\s+|\s+$/g, ""); // trim
  137. return conf.columnText ?
  138. conf.columnText(dt, idx, title) :
  139. title;
  140. }
  141. },
  142. colvisRestore: {
  143. className: 'buttons-colvisRestore',
  144. text: function (dt) {
  145. return dt.i18n('buttons.colvisRestore', 'Restore visibility');
  146. },
  147. init: function (dt, button, conf) {
  148. conf._visOriginal = dt.columns().indexes().map(function (idx) {
  149. return dt.column(idx).visible();
  150. }).toArray();
  151. },
  152. action: function (e, dt, button, conf) {
  153. dt.columns().every(function (i) {
  154. // Take into account that ColReorder might have disrupted our
  155. // indexes
  156. var idx = dt.colReorder && dt.colReorder.transpose ?
  157. dt.colReorder.transpose(i, 'toOriginal') :
  158. i;
  159. this.visible(conf._visOriginal[idx]);
  160. });
  161. }
  162. },
  163. colvisGroup: {
  164. className: 'buttons-colvisGroup',
  165. action: function (e, dt, button, conf) {
  166. dt.columns(conf.show).visible(true, false);
  167. dt.columns(conf.hide).visible(false, false);
  168. dt.columns.adjust();
  169. },
  170. show: [],
  171. hide: []
  172. }
  173. });
  174. return DataTable.Buttons;
  175. }));