7
if ( typeof define === 'function' && define.amd ) {
9
define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) {
10
return factory( $, window, document );
13
else if ( typeof exports === 'object' ) {
15
module.exports = function (root, $) {
20
if ( ! $ || ! $.fn.dataTable ) {
21
$ = require('datatables.net')(root, $).$;
24
if ( ! $.fn.dataTable.Buttons ) {
25
require('datatables.net-buttons')(root, $);
28
return factory( $, root, root.document );
33
factory( jQuery, window, document );
35
}(function( $, window, document, undefined ) {
37
var DataTable = $.fn.dataTable;
40
var _link = document.createElement( 'a' );
48
var _styleToAbs = function( el ) {
50
var clone = $(el).clone()[0];
53
if ( clone.nodeName.toLowerCase() === 'link' ) {
54
clone.href = _relToAbs( clone.href );
57
return clone.outerHTML;
66
var _relToAbs = function( href ) {
70
var linkHost = _link.host;
74
if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) {
78
return _link.protocol+"//"+linkHost+_link.pathname+_link.search;
82
DataTable.ext.buttons.print = {
83
className: 'buttons-print',
85
text: function ( dt ) {
86
return dt.i18n( 'buttons.print', 'Print' );
89
action: function ( e, dt, button, config ) {
90
var data = dt.buttons.exportData(
91
$.extend( {decodeEntities: false}, config.exportOptions )
93
var exportInfo = dt.buttons.exportInfo( config );
94
var columnClasses = dt
95
.columns( config.exportOptions.columns )
97
.map( function (idx) {
98
return dt.settings()[0].aoColumns[dt.column(idx).index()].sClass;
102
var addRow = function ( d, tag ) {
105
for ( var i=0, ien=d.length ; i<ien ; i++ ) {
107
var dataOut = d[i] === null || d[i] === undefined ?
110
var classAttr = columnClasses[i] ?
111
'class="'+columnClasses[i]+'"' :
114
str += '<'+tag+' '+classAttr+'>'+dataOut+'</'+tag+'>';
117
return str + '</tr>';
121
var html = '<table class="'+dt.table().node().className+'">';
123
if ( config.header ) {
124
html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>';
128
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) {
129
html += addRow( data.body[i], 'td' );
133
if ( config.footer && data.footer ) {
134
html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>';
139
var win = window.open( '', '' );
143
dt.i18n( 'buttons.printErrorTitle', 'Unable to open print view' ),
144
dt.i18n( 'buttons.printErrorMsg', 'Please allow popups in your browser for this site to be able to view the print view.' ),
151
win.document.close();
157
var head = '<title>'+exportInfo.title+'</title>';
158
$('style, link').each( function () {
159
head += _styleToAbs( this );
163
win.document.head.innerHTML = head;
166
$(win.document.head).html( head );
170
win.document.body.innerHTML =
171
'<h1>'+exportInfo.title+'</h1>'+
172
'<div>'+(exportInfo.messageTop || '')+'</div>'+
174
'<div>'+(exportInfo.messageBottom || '')+'</div>';
176
$(win.document.body).addClass('dt-print-view');
178
$('img', win.document.body).each( function ( i, img ) {
179
img.setAttribute( 'src', _relToAbs( img.getAttribute('src') ) );
182
if ( config.customize ) {
183
config.customize( win, config, dt );
187
var autoPrint = function () {
188
if ( config.autoPrint ) {
194
if ( navigator.userAgent.match(/Trident\/\d.\d/) ) {
198
win.setTimeout( autoPrint, 1000 );
220
return DataTable.Buttons;