|
|
|
|
|
|
|
|
|
|
|
(function( factory ){ |
|
if ( typeof define === 'function' && define.amd ) { |
|
|
|
define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) { |
|
return factory( $, window, document ); |
|
} ); |
|
} |
|
else if ( typeof exports === 'object' ) { |
|
|
|
module.exports = function (root, $) { |
|
if ( ! root ) { |
|
root = window; |
|
} |
|
|
|
if ( ! $ || ! $.fn.dataTable ) { |
|
$ = require('datatables.net')(root, $).$; |
|
} |
|
|
|
if ( ! $.fn.dataTable.Buttons ) { |
|
require('datatables.net-buttons')(root, $); |
|
} |
|
|
|
return factory( $, root, root.document ); |
|
}; |
|
} |
|
else { |
|
|
|
factory( jQuery, window, document ); |
|
} |
|
}(function( $, window, document, undefined ) { |
|
'use strict'; |
|
var DataTable = $.fn.dataTable; |
|
|
|
|
|
var _link = document.createElement( 'a' ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _relToAbs = function( el ) { |
|
var url; |
|
var clone = $(el).clone()[0]; |
|
var linkHost; |
|
|
|
if ( clone.nodeName.toLowerCase() === 'link' ) { |
|
_link.href = clone.href; |
|
linkHost = _link.host; |
|
|
|
|
|
|
|
if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) { |
|
linkHost += '/'; |
|
} |
|
|
|
clone.href = _link.protocol+"//"+linkHost+_link.pathname+_link.search; |
|
} |
|
|
|
return clone.outerHTML; |
|
}; |
|
|
|
|
|
DataTable.ext.buttons.print = { |
|
className: 'buttons-print', |
|
|
|
text: function ( dt ) { |
|
return dt.i18n( 'buttons.print', 'Print' ); |
|
}, |
|
|
|
action: function ( e, dt, button, config ) { |
|
var data = dt.buttons.exportData( config.exportOptions ); |
|
var addRow = function ( d, tag ) { |
|
var str = '<tr>'; |
|
|
|
for ( var i=0, ien=d.length ; i<ien ; i++ ) { |
|
str += '<'+tag+'>'+d[i]+'</'+tag+'>'; |
|
} |
|
|
|
return str + '</tr>'; |
|
}; |
|
|
|
|
|
var html = '<table class="'+dt.table().node().className+'">'; |
|
|
|
if ( config.header ) { |
|
html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>'; |
|
} |
|
|
|
html += '<tbody>'; |
|
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) { |
|
html += addRow( data.body[i], 'td' ); |
|
} |
|
html += '</tbody>'; |
|
|
|
if ( config.footer && data.footer ) { |
|
html += '<tfoot>'+ addRow( data.footer, 'th' ) +'</tfoot>'; |
|
} |
|
|
|
|
|
var win = window.open( '', '' ); |
|
var title = config.title; |
|
|
|
if ( typeof title === 'function' ) { |
|
title = title(); |
|
} |
|
|
|
if ( title.indexOf( '*' ) !== -1 ) { |
|
title= title.replace( '*', $('title').text() ); |
|
} |
|
|
|
win.document.close(); |
|
|
|
|
|
|
|
|
|
|
|
var head = '<title>'+title+'</title>'; |
|
$('style, link').each( function () { |
|
head += _relToAbs( this ); |
|
} ); |
|
|
|
|
|
win.document.head.innerHTML = head; |
|
|
|
|
|
win.document.body.innerHTML = |
|
'<h1>'+title+'</h1>'+ |
|
'<div>'+config.message+'</div>'+ |
|
html; |
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( config.customize ) { |
|
config.customize( win ); |
|
} |
|
|
|
setTimeout( function () { |
|
if ( config.autoPrint ) { |
|
win.print(); |
|
win.close(); |
|
} |
|
}, 250 ); |
|
}, |
|
|
|
title: '*', |
|
|
|
message: '', |
|
|
|
exportOptions: {}, |
|
|
|
header: true, |
|
|
|
footer: false, |
|
|
|
autoPrint: true, |
|
|
|
customize: null |
|
}; |
|
|
|
|
|
return DataTable.Buttons; |
|
})); |
|
|