1 | (function (global, factory) {
|
2 | if (typeof define === "function" && define.amd) {
|
3 | define([], factory);
|
4 | } else if (typeof exports !== "undefined") {
|
5 | factory();
|
6 | } else {
|
7 | var mod = {
|
8 | exports: {}
|
9 | };
|
10 | factory();
|
11 | global.bootstrapTablePrint = mod.exports;
|
12 | }
|
13 | })(this, function () {
|
14 | 'use strict';
|
15 |
|
16 | (function ($) {
|
17 | 'use strict';
|
18 |
|
19 | var sprintf = $.fn.bootstrapTable.utils.sprintf;
|
20 |
|
21 | function printPageBuilderDefault(table) {
|
22 | return '<html><head>' + '<style type="text/css" media="print">' + ' @page { size: auto; margin: 25px 0 25px 0; }' + '</style>' + '<style type="text/css" media="all">' + 'table{border-collapse: collapse; font-size: 12px; }\n' + 'table, th, td {border: 1px solid grey}\n' + 'th, td {text-align: center; vertical-align: middle;}\n' + 'p {font-weight: bold; margin-left:20px }\n' + 'table { width:94%; margin-left:3%; margin-right:3%}\n' + 'div.bs-table-print { text-align:center;}\n' + '</style></head><title>Print Table</title><body>' + '<p>Printed on: ' + new Date() + ' </p>' + '<div class="bs-table-print">' + table + "</div></body></html>";
|
23 | }
|
24 | $.extend($.fn.bootstrapTable.defaults, {
|
25 | showPrint: false,
|
26 | printAsFilteredAndSortedOnUI: true,
|
27 |
|
28 |
|
29 |
|
30 | printSortColumn: undefined,
|
31 | printSortOrder: 'asc',
|
32 | printPageBuilder: function printPageBuilder(table) {
|
33 | return printPageBuilderDefault(table);
|
34 | }
|
35 | });
|
36 | $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
|
37 | printFilter: undefined,
|
38 | printIgnore: false,
|
39 | printFormatter: undefined
|
40 |
|
41 | });
|
42 | $.extend($.fn.bootstrapTable.defaults.icons, {
|
43 | print: 'glyphicon-print icon-share'
|
44 | });
|
45 |
|
46 | var BootstrapTable = $.fn.bootstrapTable.Constructor,
|
47 | _initToolbar = BootstrapTable.prototype.initToolbar;
|
48 |
|
49 | BootstrapTable.prototype.initToolbar = function () {
|
50 | this.showToolbar = this.showToolbar || this.options.showPrint;
|
51 |
|
52 | _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
|
53 |
|
54 | if (this.options.showPrint) {
|
55 | var that = this,
|
56 | $btnGroup = this.$toolbar.find('>.btn-group'),
|
57 | $print = $btnGroup.find('button.bs-print');
|
58 |
|
59 | if (!$print.length) {
|
60 | $print = $(['<button class="bs-print btn btn-default' + sprintf(' btn-%s"', this.options.iconSize) + ' name="print" title="print" type="button">', sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.print), '</button>'].join('')).appendTo($btnGroup);
|
61 |
|
62 | $print.click(function () {
|
63 | function formatValue(row, i, column) {
|
64 | var value = row[column.field];
|
65 | if (typeof column.printFormatter === 'function') {
|
66 | return column.printFormatter.apply(column, [value, row, i]);
|
67 | } else {
|
68 | return typeof value === 'undefined' ? "-" : value;
|
69 | }
|
70 | }
|
71 |
|
72 | function buildTable(data, columnsArray) {
|
73 | var html = ['<table><thead>'];
|
74 | for (var k = 0; k < columnsArray.length; k++) {
|
75 | var columns = columnsArray[k];
|
76 | html.push('<tr>');
|
77 | for (var h = 0; h < columns.length; h++) {
|
78 | if (!columns[h].printIgnore) {
|
79 | html.push('<th', sprintf(' rowspan="%s"', columns[h].rowspan), sprintf(' colspan="%s"', columns[h].colspan), sprintf('>%s</th>', columns[h].title));
|
80 | }
|
81 | }
|
82 | html.push('</tr>');
|
83 | }
|
84 | html.push('</thead><tbody>');
|
85 | for (var i = 0; i < data.length; i++) {
|
86 | html.push('<tr>');
|
87 | for (var l = 0; l < columnsArray.length; l++) {
|
88 | var columns = columnsArray[l];
|
89 | for (var j = 0; j < columns.length; j++) {
|
90 | if (!columns[j].printIgnore && columns[j].field) {
|
91 | html.push('<td>', formatValue(data[i], i, columns[j]), '</td>');
|
92 | }
|
93 | }
|
94 | }
|
95 | html.push('</tr>');
|
96 | }
|
97 | html.push('</tbody></table>');
|
98 | return html.join('');
|
99 | }
|
100 | function sortRows(data, colName, sortOrder) {
|
101 | if (!colName) {
|
102 | return data;
|
103 | }
|
104 | var reverse = sortOrder != 'asc';
|
105 | reverse = -(+reverse || -1);
|
106 | return data.sort(function (a, b) {
|
107 | return reverse * a[colName].localeCompare(b[colName]);
|
108 | });
|
109 | }
|
110 | function filterRow(row, filters) {
|
111 | for (var index = 0; index < filters.length; ++index) {
|
112 | if (row[filters[index].colName] != filters[index].value) {
|
113 | return false;
|
114 | }
|
115 | }
|
116 | return true;
|
117 | }
|
118 | function filterRows(data, filters) {
|
119 | return data.filter(function (row) {
|
120 | return filterRow(row, filters);
|
121 | });
|
122 | }
|
123 | function getColumnFilters(columns) {
|
124 | return !columns || !columns[0] ? [] : columns[0].filter(function (col) {
|
125 | return col.printFilter;
|
126 | }).map(function (col) {
|
127 | return { colName: col.field, value: col.printFilter };
|
128 | });
|
129 | }
|
130 | var doPrint = function doPrint(data) {
|
131 | data = filterRows(data, getColumnFilters(that.options.columns));
|
132 | data = sortRows(data, that.options.printSortColumn, that.options.printSortOrder);
|
133 | var table = buildTable(data, that.options.columns);
|
134 | var newWin = window.open("");
|
135 | newWin.document.write(that.options.printPageBuilder.call(this, table));
|
136 | newWin.print();
|
137 | newWin.close();
|
138 | };
|
139 | doPrint(that.options.printAsFilteredAndSortedOnUI ? that.getData() : that.options.data.slice(0));
|
140 | });
|
141 | }
|
142 | }
|
143 | };
|
144 | })(jQuery);
|
145 | }); |
\ | No newline at end of file |