bootstrap-table-print.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. (function ($) {
  2. 'use strict';
  3. var sprintf = $.fn.bootstrapTable.utils.sprintf;
  4. function printPageBuilderDefault(table) {
  5. return '<html><head>' +
  6. '<style type="text/css" media="print">' +
  7. ' @page { size: auto; margin: 25px 0 25px 0; }' +
  8. '</style>' +
  9. '<style type="text/css" media="all">' +
  10. 'table{border-collapse: collapse; font-size: 12px; }\n' +
  11. 'table, th, td {border: 1px solid grey}\n' +
  12. 'th, td {text-align: center; vertical-align: middle;}\n' +
  13. 'p {font-weight: bold; margin-left:20px }\n' +
  14. 'table { width:94%; margin-left:3%; margin-right:3%}\n' +
  15. 'div.bs-table-print { text-align:center;}\n' +
  16. '</style></head><title>Print Table</title><body>' +
  17. '<p>Printed on: ' + new Date + ' </p>' +
  18. '<div class="bs-table-print">' + table + "</div></body></html>";
  19. }
  20. $.extend($.fn.bootstrapTable.defaults, {
  21. showPrint: false,
  22. printAsFilteredAndSortedOnUI: true, //boolean, when true - print table as sorted and filtered on UI.
  23. //Please note that if true is set, along with explicit predefined print options for filtering and sorting (printFilter, printSortOrder, printSortColumn)- then they will be applied on data already filtered and sorted by UI controls.
  24. //For printing data as filtered and sorted on UI - do not set these 3 options:printFilter, printSortOrder, printSortColumn
  25. printSortColumn: undefined , //String, set column field name to be sorted by
  26. printSortOrder: 'asc', //String: 'asc' , 'desc' - relevant only if printSortColumn is set
  27. printPageBuilder: function(table){return printPageBuilderDefault(table)} // function, receive html <table> element as string, returns html string for printing. by default delegates to function printPageBuilderDefault(table). used for styling and adding header or footer
  28. });
  29. $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
  30. printFilter: undefined, //set value to filter by in print page
  31. printIgnore: false, //boolean, set true to ignore this column in the print page
  32. printFormatter:undefined //function(value, row, index), formats the cell value for this column in the printed table. Function behaviour is similar to the 'formatter' column option
  33. });
  34. $.extend($.fn.bootstrapTable.defaults.icons, {
  35. print: 'glyphicon-print icon-share'
  36. });
  37. var BootstrapTable = $.fn.bootstrapTable.Constructor,
  38. _initToolbar = BootstrapTable.prototype.initToolbar;
  39. BootstrapTable.prototype.initToolbar = function () {
  40. this.showToolbar = this.options.showPrint;
  41. _initToolbar.apply(this, Array.prototype.slice.apply(arguments));
  42. if (this.options.showPrint) {
  43. var that = this,
  44. $btnGroup = this.$toolbar.find('>.btn-group'),
  45. $print = $btnGroup.find('button.bs-print');
  46. if (!$print.length) {
  47. $print = $([
  48. '<button class="bs-print btn btn-default' + sprintf(' btn-%s"', this.options.iconSize) + ' name="print" title="print" type="button">',
  49. sprintf('<i class="%s %s"></i> ', this.options.iconsPrefix, this.options.icons.print),
  50. '</button>'].join('')).appendTo($btnGroup);
  51. $print.click(function () {
  52. function formatValue(row, i, column ) {
  53. var value = row[column.field];
  54. if (typeof column.printFormatter === 'function') {
  55. return column.printFormatter.apply(column, [value, row, i]);
  56. }
  57. else {
  58. return value || "-";
  59. }
  60. }
  61. function buildTable(data,columns) {
  62. var out = "<table><thead><tr>";
  63. for(var h = 0; h < columns.length; h++) {
  64. if(!columns[h].printIgnore) {
  65. out += ("<th>"+columns[h].title+"</th>");
  66. }
  67. }
  68. out += "</tr></thead><tbody>";
  69. for(var i = 0; i < data.length; i++) {
  70. out += "<tr>";
  71. for(var j = 0; j < columns.length; j++) {
  72. if(!columns[j].printIgnore) {
  73. out += ("<td>"+ formatValue(data[i], i, columns[j])+"</td>");
  74. }
  75. }
  76. out += "</tr>";
  77. }
  78. out += "</tbody></table>";
  79. return out;
  80. }
  81. function sortRows(data,colName,sortOrder) {
  82. if(!colName){
  83. return data;
  84. }
  85. var reverse = sortOrder != 'asc';
  86. reverse = -((+reverse) || -1);
  87. return data.sort(function (a, b) {
  88. return reverse * (a[colName].localeCompare(b[colName]));
  89. });
  90. }
  91. function filterRow(row,filters) {
  92. for (var index = 0; index < filters.length; ++index) {
  93. if(row[filters[index].colName]!=filters[index].value) {
  94. return false;
  95. }
  96. }
  97. return true;
  98. }
  99. function filterRows(data,filters) {
  100. return data.filter(function (row) {
  101. return filterRow(row,filters)
  102. });
  103. }
  104. function getColumnFilters(columns) {
  105. return !columns || !columns[0] ? [] : columns[0].filter(function (col) {
  106. return col.printFilter;
  107. }).map(function (col) {
  108. return {colName:col.field, value:col.printFilter};
  109. });
  110. }
  111. var doPrint = function (data) {
  112. data=filterRows(data,getColumnFilters(that.options.columns));
  113. data=sortRows(data,that.options.printSortColumn,that.options.printSortOrder);
  114. var table=buildTable(data,that.options.columns[0]);
  115. var newWin = window.open("");
  116. newWin.document.write(that.options.printPageBuilder.call(this, table));
  117. newWin.print();
  118. newWin.close();
  119. };
  120. doPrint(that.options.printAsFilteredAndSortedOnUI? that.getData() : that.options.data.slice(0));
  121. });
  122. }
  123. }
  124. };
  125. })(jQuery);