| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
(function($, DataTable) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
|
| 10 |
var _link = document.createElement( 'a' ); |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var _relToAbs = function( el ) { |
| 19 |
var url; |
| 20 |
var clone = $(el).clone()[0]; |
| 21 |
var linkHost; |
| 22 |
|
| 23 |
if ( clone.nodeName.toLowerCase() === 'link' ) { |
| 24 |
_link.href = clone.href; |
| 25 |
linkHost = _link.host; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) { |
| 30 |
linkHost += '/'; |
| 31 |
} |
| 32 |
|
| 33 |
clone.href = _link.protocol+"//"+linkHost+_link.pathname+_link.search; |
| 34 |
} |
| 35 |
|
| 36 |
return clone.outerHTML; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
DataTable.ext.buttons.print = { |
| 41 |
className: 'buttons-print', |
| 42 |
|
| 43 |
text: function ( dt ) { |
| 44 |
return dt.i18n( 'buttons.print', 'Print' ); |
| 45 |
}, |
| 46 |
|
| 47 |
action: function ( e, dt, button, config ) { |
| 48 |
var data = dt.buttons.exportData( config.exportOptions ); |
| 49 |
var addRow = function ( d, tag ) { |
| 50 |
var str = '<tr>'; |
| 51 |
|
| 52 |
for ( var i=0, ien=d.length ; i<ien ; i++ ) { |
| 53 |
str += '<'+tag+'>'+d[i]+'</'+tag+'>'; |
| 54 |
} |
| 55 |
|
| 56 |
return str + '</tr>'; |
| 57 |
}; |
| 58 |
|
| 59 |
|
| 60 |
var html = '<table class="'+dt.table().node().className+'">'; |
| 61 |
|
| 62 |
if ( config.header ) { |
| 63 |
html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>'; |
| 64 |
} |
| 65 |
|
| 66 |
html += '<tbody>'; |
| 67 |
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) { |
| 68 |
html += addRow( data.body[i], 'td' ); |
| 69 |
} |
| 70 |
html += '</tbody>'; |
| 71 |
|
| 72 |
if ( config.footer ) { |
| 73 |
html += '<thead>'+ addRow( data.footer, 'th' ) +'</thead>'; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
var win = window.open( '', '' ); |
| 78 |
var title = config.title.replace( '*', $('title').text() ); |
| 79 |
|
| 80 |
win.document.close(); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
var head = '<title>'+title+'</title>'; |
| 87 |
$('style, link').each( function () { |
| 88 |
head += _relToAbs( this ); |
| 89 |
} ); |
| 90 |
|
| 91 |
$(win.document.head).html( head ); |
| 92 |
|
| 93 |
|
| 94 |
$(win.document.body).html( |
| 95 |
'<h1>'+title+'</h1>'+ |
| 96 |
'<div>'+config.message+'</div>'+ |
| 97 |
html |
| 98 |
); |
| 99 |
|
| 100 |
if ( config.customize ) { |
| 101 |
config.customize( win ); |
| 102 |
} |
| 103 |
|
| 104 |
setTimeout( function () { |
| 105 |
if ( config.autoPrint ) { |
| 106 |
win.print(); |
| 107 |
win.close(); |
| 108 |
} |
| 109 |
}, 250 ); |
| 110 |
}, |
| 111 |
|
| 112 |
title: '*', |
| 113 |
|
| 114 |
message: '', |
| 115 |
|
| 116 |
exportOptions: {}, |
| 117 |
|
| 118 |
header: true, |
| 119 |
|
| 120 |
footer: false, |
| 121 |
|
| 122 |
autoPrint: true, |
| 123 |
|
| 124 |
customize: null |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
})(jQuery, jQuery.fn.dataTable); |