buttons.server-side.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. (function ($, DataTable) {
  2. "use strict";
  3. var _buildParams = function (dt, action, onlyVisibles) {
  4. var params = dt.ajax.params();
  5. params.action = action;
  6. params._token = $('meta[name="csrf-token"]').attr('content');
  7. if (onlyVisibles) {
  8. params.visible_columns = _getVisibleColumns();
  9. } else {
  10. params.visible_columns = null;
  11. }
  12. return params;
  13. };
  14. var _getVisibleColumns = function () {
  15. var visible_columns = [];
  16. $.each(DataTable.settings[0].aoColumns, function (key, col) {
  17. if (col.bVisible) {
  18. visible_columns.push(col.name);
  19. }
  20. });
  21. return visible_columns;
  22. };
  23. var _downloadFromUrl = function (url, params) {
  24. var postUrl = url + '/export';
  25. var xhr = new XMLHttpRequest();
  26. xhr.open('POST', postUrl, true);
  27. xhr.responseType = 'arraybuffer';
  28. xhr.onload = function () {
  29. if (this.status === 200) {
  30. var filename = "";
  31. var disposition = xhr.getResponseHeader('Content-Disposition');
  32. if (disposition && disposition.indexOf('attachment') !== -1) {
  33. var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
  34. var matches = filenameRegex.exec(disposition);
  35. if (matches != null && matches[1]) filename = matches[1].replace(/['"]/g, '');
  36. }
  37. var type = xhr.getResponseHeader('Content-Type');
  38. var blob = new Blob([this.response], {type: type});
  39. if (typeof window.navigator.msSaveBlob !== 'undefined') {
  40. // IE workaround for "HTML7007: One or more blob URLs were revoked by closing the blob for which they were created. These URLs will no longer resolve as the data backing the URL has been freed."
  41. window.navigator.msSaveBlob(blob, filename);
  42. } else {
  43. var URL = window.URL || window.webkitURL;
  44. var downloadUrl = URL.createObjectURL(blob);
  45. if (filename) {
  46. // use HTML5 a[download] attribute to specify filename
  47. var a = document.createElement("a");
  48. // safari doesn't support this yet
  49. if (typeof a.download === 'undefined') {
  50. window.location = downloadUrl;
  51. } else {
  52. a.href = downloadUrl;
  53. a.download = filename;
  54. document.body.appendChild(a);
  55. a.click();
  56. }
  57. } else {
  58. window.location = downloadUrl;
  59. }
  60. setTimeout(function () {
  61. URL.revokeObjectURL(downloadUrl);
  62. }, 100); // cleanup
  63. }
  64. }
  65. };
  66. xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  67. xhr.send($.param(params));
  68. };
  69. var _buildUrl = function(dt, action) {
  70. var url = dt.ajax.url() || '';
  71. var params = dt.ajax.params();
  72. params.action = action;
  73. if (url.indexOf('?') > -1) {
  74. return url + '&' + $.param(params);
  75. }
  76. return url + '?' + $.param(params);
  77. };
  78. DataTable.ext.buttons.excel = {
  79. className: 'buttons-excel',
  80. text: function (dt) {
  81. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.excel', 'Excel');
  82. },
  83. action: function (e, dt, button, config) {
  84. var url = _buildUrl(dt, 'excel');
  85. window.location = url;
  86. }
  87. };
  88. DataTable.ext.buttons.postExcel = {
  89. className: 'buttons-excel',
  90. text: function (dt) {
  91. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.excel', 'Excel');
  92. },
  93. action: function (e, dt, button, config) {
  94. var url = dt.ajax.url() || window.location.href;
  95. var params = _buildParams(dt, 'excel');
  96. _downloadFromUrl(url, params);
  97. }
  98. };
  99. DataTable.ext.buttons.postExcelVisibleColumns = {
  100. className: 'buttons-excel',
  101. text: function (dt) {
  102. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.excel', 'Excel (only visible columns)');
  103. },
  104. action: function (e, dt, button, config) {
  105. var url = dt.ajax.url() || window.location.href;
  106. var params = _buildParams(dt, 'excel', true);
  107. _downloadFromUrl(url, params);
  108. }
  109. };
  110. DataTable.ext.buttons.export = {
  111. extend: 'collection',
  112. className: 'buttons-export',
  113. text: function (dt) {
  114. return '<i class="fa fa-download"></i> ' + dt.i18n('buttons.export', 'Export') + '&nbsp;<span class="caret"/>';
  115. },
  116. buttons: ['csv', 'excel', 'pdf']
  117. };
  118. DataTable.ext.buttons.csv = {
  119. className: 'buttons-csv',
  120. text: function (dt) {
  121. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.csv', 'CSV');
  122. },
  123. action: function (e, dt, button, config) {
  124. var url = _buildUrl(dt, 'csv');
  125. window.location = url;
  126. }
  127. };
  128. DataTable.ext.buttons.postCsvVisibleColumns = {
  129. className: 'buttons-csv',
  130. text: function (dt) {
  131. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.csv', 'CSV (only visible columns)');
  132. },
  133. action: function (e, dt, button, config) {
  134. var url = dt.ajax.url() || window.location.href;
  135. var params = _buildParams(dt, 'csv', true);
  136. _downloadFromUrl(url, params);
  137. }
  138. };
  139. DataTable.ext.buttons.postCsv = {
  140. className: 'buttons-csv',
  141. text: function (dt) {
  142. return '<i class="fa fa-file-excel-o"></i> ' + dt.i18n('buttons.csv', 'CSV');
  143. },
  144. action: function (e, dt, button, config) {
  145. var url = dt.ajax.url() || window.location.href;
  146. var params = _buildParams(dt, 'csv');
  147. _downloadFromUrl(url, params);
  148. }
  149. };
  150. DataTable.ext.buttons.pdf = {
  151. className: 'buttons-pdf',
  152. text: function (dt) {
  153. return '<i class="fa fa-file-pdf-o"></i> ' + dt.i18n('buttons.pdf', 'PDF');
  154. },
  155. action: function (e, dt, button, config) {
  156. var url = _buildUrl(dt, 'pdf');
  157. window.location = url;
  158. }
  159. };
  160. DataTable.ext.buttons.postPdf = {
  161. className: 'buttons-pdf',
  162. text: function (dt) {
  163. return '<i class="fa fa-file-pdf-o"></i> ' + dt.i18n('buttons.pdf', 'PDF');
  164. },
  165. action: function (e, dt, button, config) {
  166. var url = dt.ajax.url() || window.location.href;
  167. var params = _buildParams(dt, 'pdf');
  168. _downloadFromUrl(url, params);
  169. }
  170. };
  171. DataTable.ext.buttons.print = {
  172. className: 'buttons-print',
  173. text: function (dt) {
  174. return '<i class="fa fa-print"></i> ' + dt.i18n('buttons.print', 'Print');
  175. },
  176. action: function (e, dt, button, config) {
  177. var url = _buildUrl(dt, 'print');
  178. window.location = url;
  179. }
  180. };
  181. DataTable.ext.buttons.reset = {
  182. className: 'buttons-reset',
  183. text: function (dt) {
  184. return '<i class="fa fa-undo"></i> ' + dt.i18n('buttons.reset', 'Reset');
  185. },
  186. action: function (e, dt, button, config) {
  187. dt.search('');
  188. dt.columns().search('');
  189. dt.draw();
  190. }
  191. };
  192. DataTable.ext.buttons.reload = {
  193. className: 'buttons-reload',
  194. text: function (dt) {
  195. return '<i class="fa fa-refresh"></i> ' + dt.i18n('buttons.reload', 'Reload');
  196. },
  197. action: function (e, dt, button, config) {
  198. dt.draw(false);
  199. }
  200. };
  201. DataTable.ext.buttons.create = {
  202. className: 'buttons-create',
  203. text: function (dt) {
  204. return '<i class="fa fa-plus"></i> ' + dt.i18n('buttons.create', 'Create');
  205. },
  206. action: function (e, dt, button, config) {
  207. window.location = window.location.href.replace(/\/+$/, "") + '/create';
  208. }
  209. };
  210. if (typeof DataTable.ext.buttons.copyHtml5 !== 'undefined') {
  211. $.extend(DataTable.ext.buttons.copyHtml5, {
  212. text: function (dt) {
  213. return '<i class="fa fa-copy"></i> ' + dt.i18n('buttons.copy', 'Copy');
  214. }
  215. });
  216. }
  217. if (typeof DataTable.ext.buttons.colvis !== 'undefined') {
  218. $.extend(DataTable.ext.buttons.colvis, {
  219. text: function (dt) {
  220. return '<i class="fa fa-eye"></i> ' + dt.i18n('buttons.colvis', 'Column visibility');
  221. }
  222. });
  223. }
  224. })(jQuery, jQuery.fn.dataTable);