提交 | 用户 | 时间
|
58d006
|
1 |
/*! |
A |
2 |
* Print button for Buttons and DataTables. |
|
3 |
* 2015 SpryMedia Ltd - datatables.net/license |
|
4 |
*/ |
|
5 |
|
|
6 |
(function( factory ){ |
|
7 |
if ( typeof define === 'function' && define.amd ) { |
|
8 |
// AMD |
|
9 |
define( ['jquery', 'datatables.net', 'datatables.net-buttons'], function ( $ ) { |
|
10 |
return factory( $, window, document ); |
|
11 |
} ); |
|
12 |
} |
|
13 |
else if ( typeof exports === 'object' ) { |
|
14 |
// CommonJS |
|
15 |
module.exports = function (root, $) { |
|
16 |
if ( ! root ) { |
|
17 |
root = window; |
|
18 |
} |
|
19 |
|
|
20 |
if ( ! $ || ! $.fn.dataTable ) { |
|
21 |
$ = require('datatables.net')(root, $).$; |
|
22 |
} |
|
23 |
|
|
24 |
if ( ! $.fn.dataTable.Buttons ) { |
|
25 |
require('datatables.net-buttons')(root, $); |
|
26 |
} |
|
27 |
|
|
28 |
return factory( $, root, root.document ); |
|
29 |
}; |
|
30 |
} |
|
31 |
else { |
|
32 |
// Browser |
|
33 |
factory( jQuery, window, document ); |
|
34 |
} |
|
35 |
}(function( $, window, document, undefined ) { |
|
36 |
'use strict'; |
|
37 |
var DataTable = $.fn.dataTable; |
|
38 |
|
|
39 |
|
|
40 |
var _link = document.createElement( 'a' ); |
|
41 |
|
|
42 |
/** |
|
43 |
* Convert a `link` tag's URL from a relative to an absolute address so it will |
|
44 |
* work correctly in the popup window which has no base URL. |
|
45 |
* |
|
46 |
* @param {node} el Element to convert |
|
47 |
*/ |
|
48 |
var _relToAbs = function( el ) { |
|
49 |
var url; |
|
50 |
var clone = $(el).clone()[0]; |
|
51 |
var linkHost; |
|
52 |
|
|
53 |
if ( clone.nodeName.toLowerCase() === 'link' ) { |
|
54 |
_link.href = clone.href; |
|
55 |
linkHost = _link.host; |
|
56 |
|
|
57 |
// IE doesn't have a trailing slash on the host |
|
58 |
// Chrome has it on the pathname |
|
59 |
if ( linkHost.indexOf('/') === -1 && _link.pathname.indexOf('/') !== 0) { |
|
60 |
linkHost += '/'; |
|
61 |
} |
|
62 |
|
|
63 |
clone.href = _link.protocol+"//"+linkHost+_link.pathname+_link.search; |
|
64 |
} |
|
65 |
|
|
66 |
return clone.outerHTML; |
|
67 |
}; |
|
68 |
|
|
69 |
|
|
70 |
DataTable.ext.buttons.print = { |
|
71 |
className: 'buttons-print', |
|
72 |
|
|
73 |
text: function ( dt ) { |
|
74 |
return dt.i18n( 'buttons.print', 'Print' ); |
|
75 |
}, |
|
76 |
|
|
77 |
action: function ( e, dt, button, config ) { |
|
78 |
var data = dt.buttons.exportData( config.exportOptions ); |
|
79 |
var addRow = function ( d, tag ) { |
|
80 |
var str = '<tr>'; |
|
81 |
|
|
82 |
for ( var i=0, ien=d.length ; i<ien ; i++ ) { |
|
83 |
str += '<'+tag+'>'+d[i]+'</'+tag+'>'; |
|
84 |
} |
|
85 |
|
|
86 |
return str + '</tr>'; |
|
87 |
}; |
|
88 |
|
|
89 |
// Construct a table for printing |
|
90 |
var html = '<table class="'+dt.table().node().className+'">'; |
|
91 |
|
|
92 |
if ( config.header ) { |
|
93 |
html += '<thead>'+ addRow( data.header, 'th' ) +'</thead>'; |
|
94 |
} |
|
95 |
|
|
96 |
html += '<tbody>'; |
|
97 |
for ( var i=0, ien=data.body.length ; i<ien ; i++ ) { |
|
98 |
html += addRow( data.body[i], 'td' ); |
|
99 |
} |
|
100 |
html += '</tbody>'; |
|
101 |
|
|
102 |
if ( config.footer ) { |
|
103 |
html += '<thead>'+ addRow( data.footer, 'th' ) +'</thead>'; |
|
104 |
} |
|
105 |
|
|
106 |
// Open a new window for the printable table |
|
107 |
var win = window.open( '', '' ); |
|
108 |
var title = config.title.replace( '*', $('title').text() ); |
|
109 |
|
|
110 |
win.document.close(); |
|
111 |
|
|
112 |
// Inject the title and also a copy of the style and link tags from this |
|
113 |
// document so the table can retain its base styling. Note that we have |
|
114 |
// to use string manipulation as IE won't allow elements to be created |
|
115 |
// in the host document and then appended to the new window. |
|
116 |
var head = '<title>'+title+'</title>'; |
|
117 |
$('style, link').each( function () { |
|
118 |
head += _relToAbs( this ); |
|
119 |
} ); |
|
120 |
|
|
121 |
$(win.document.head).html( head ); |
|
122 |
|
|
123 |
// Inject the table and other surrounding information |
|
124 |
$(win.document.body).html( |
|
125 |
'<h1>'+title+'</h1>'+ |
|
126 |
'<div>'+config.message+'</div>'+ |
|
127 |
html |
|
128 |
); |
|
129 |
|
|
130 |
if ( config.customize ) { |
|
131 |
config.customize( win ); |
|
132 |
} |
|
133 |
|
|
134 |
setTimeout( function () { |
|
135 |
if ( config.autoPrint ) { |
|
136 |
win.print(); // blocking - so close will not |
|
137 |
win.close(); // execute until this is done |
|
138 |
} |
|
139 |
}, 250 ); |
|
140 |
}, |
|
141 |
|
|
142 |
title: '*', |
|
143 |
|
|
144 |
message: '', |
|
145 |
|
|
146 |
exportOptions: {}, |
|
147 |
|
|
148 |
header: true, |
|
149 |
|
|
150 |
footer: false, |
|
151 |
|
|
152 |
autoPrint: true, |
|
153 |
|
|
154 |
customize: null |
|
155 |
}; |
|
156 |
|
|
157 |
|
|
158 |
return DataTable.Buttons; |
|
159 |
})); |