buttons.print.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*!
  2. * Print button 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. var _link = document.createElement( 'a' );
  35. /**
  36. * Convert a `link` tag's URL from a relative to an absolute address so it will
  37. * work correctly in the popup window which has no base URL.
  38. *
  39. * @param {node} el Element to convert
  40. */
  41. var _relToAbs = function( el ) {
  42. var url;
  43. var clone = $(el).clone()[0];
  44. var linkHost;
  45. if ( clone.nodeName.toLowerCase() === 'link' ) {
  46. _link.href = clone.href;
  47. linkHost = _link.host;
  48. // IE doesn't have a trailing slash on the host
  49. // Chrome has it on the pathname
  50. if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) {
  51. linkHost += '/';
  52. }
  53. clone.href = _link.protocol+"//"+linkHost+_link.pathname+_link.search;
  54. }
  55. return clone.outerHTML;
  56. };
  57. DataTable.ext.buttons.print = {
  58. className: 'buttons-print',
  59. text: function ( dt ) {
  60. return dt.i18n( 'buttons.print', 'Print' );
  61. },
  62. action: function ( e, dt, button, config ) {
  63. var data = dt.buttons.exportData( config.exportOptions );
  64. var addRow = function ( d, tag ) {
  65. var str = '<tr>';
  66. for ( var i=0, ien=d.length ; i<ien ; i++ ) {
  67. str += '<'+tag+'>'+d[i]+'</'+tag+'>';
  68. }
  69. return str + '</tr>';
  70. };
  71. // Construct a table for printing
  72. var html = '<table class="'+dt.table().node().className+'">';
  73. if ( config.header ) {
  74. html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>';
  75. }
  76. html += '<tbody>';
  77. for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
  78. html += addRow( data.body[i], 'td' );
  79. }
  80. html += '</tbody>';
  81. if ( config.footer && data.footer ) {
  82. html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>';
  83. }
  84. // Open a new window for the printable table
  85. var win = window.open( '', '' );
  86. var title = config.title;
  87. if ( typeof title === 'function' ) {
  88. title = title();
  89. }
  90. if ( title.indexOf( '*' ) !== -1 ) {
  91. title= title.replace( '*', $('title').text() );
  92. }
  93. win.document.close();
  94. // Inject the title and also a copy of the style and link tags from this
  95. // document so the table can retain its base styling. Note that we have
  96. // to use string manipulation as IE won't allow elements to be created
  97. // in the host document and then appended to the new window.
  98. var head = '<title>'+title+'</title>';
  99. $('style, link').each( function () {
  100. head += _relToAbs( this );
  101. } );
  102. try {
  103. win.document.head.innerHTML = head; // Work around for Edge
  104. }
  105. catch (e) {
  106. $(win.document.head).html( head ); // Old IE
  107. }
  108. // Inject the table and other surrounding information
  109. win.document.body.innerHTML =
  110. '<h1>'+title+'</h1>'+
  111. '<div>'+
  112. (typeof config.message === 'function' ?
  113. config.message( dt, button, config ) :
  114. config.message
  115. )+
  116. '</div>'+
  117. html;
  118. if ( config.customize ) {
  119. config.customize( win );
  120. }
  121. setTimeout( function () {
  122. if ( config.autoPrint ) {
  123. win.print(); // blocking - so close will not
  124. win.close(); // execute until this is done
  125. }
  126. }, 250 );
  127. },
  128. title: '*',
  129. message: '',
  130. exportOptions: {},
  131. header: true,
  132. footer: false,
  133. autoPrint: true,
  134. customize: null
  135. };
  136. return DataTable.Buttons;
  137. }));