jquery.printTable.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. $.fn.printArea = function (opt) {
  2. var document = window.document;
  3. opt = $.extend({
  4. preview: false, // 是否预览
  5. table: false, // 是否打印table
  6. usePageStyle: true // 是否使用页面中的样式
  7. }, opt);
  8. var content,
  9. iframe,
  10. win,
  11. links = document.getElementsByTagName("link"),
  12. html = '<!doctype html><html><head><meta charset="utf-8"><title></title>';
  13. // 自动添加样式
  14. for (var i = 0, len = links.length; i < len; i++) {
  15. if (links[i].rel === 'stylesheet') {
  16. //if (opt.usePageStyle || links[i].href.indexOf('learun-report.css') !== -1) {
  17. // html += links[i].outerHTML;
  18. //}
  19. if (opt.usePageStyle || links[i].href.indexOf('.css') !== -1) {
  20. html += links[i].outerHTML;
  21. }
  22. }
  23. }
  24. content = opt.table ? '' : this[0].outerHTML;
  25. html += '</head><body>' + content + '</body></html>';
  26. // 构造iframe
  27. var _self = $(this).clone(), timer, firstCall, win, $html = $(html);
  28. iframe = document.createElement("iframe");
  29. iframe.id = "printProxyIframe";
  30. iframe.frameBorder = 0;
  31. iframe.setAttribute("style", 'position:absolute;z-index:100;left:0;top:0;width:100%;height:100%;background:#fff;' + (opt.preview ? '' : 'visibility:hidden;'));
  32. iframe.onload = function () {
  33. win = iframe.contentWindow;
  34. win.canAccess = true;
  35. }
  36. iframe.src = "javascript:void((function(){document.open();document.domain='" + document.domain + "';document.close()})())";
  37. document.body.appendChild(iframe);
  38. var timer = setInterval(function () {
  39. if (iframe.contentWindow.canAccess) {
  40. clearInterval(timer);
  41. //iframe.contentWindow.document.body.innerHTML = '这是新设置的页面内容';
  42. // 重新构造jqgrid渲染的table为单个table
  43. //win.document.write(html);
  44. win.onafterprint = function () {
  45. win.onafterprint = null;
  46. iframe.parentNode.removeChild(iframe);
  47. };
  48. if (opt.table) {
  49. var $printArea = _self.find('.printArea');
  50. $.each($printArea, function (i, item) {
  51. var $_area = $(item)
  52. if ($_area.find('.ui-jqgrid').length == 0) {
  53. var $tb = $_area.find("table.form").eq(0).clone().removeAttr("style").attr("class", "ui-table-print");
  54. $tb.find("th").css("width", "auto");
  55. //$tb.find("td").css("width", "auto");
  56. $(win.document.body).append($tb);
  57. } else {
  58. var $tb = $_area.find("table.ui-jqgrid-htable").eq(0).clone().removeAttr("style").attr("class", "ui-table-print");
  59. var $data = $_area.find("table.ui-jqgrid-btable").eq(0).find("tbody").clone();
  60. var $title = $_area.find("div.grid-title");
  61. var $subtitle = $_area.find("div.grid-subtitle");
  62. var $summary = $_area.find("table.ui-jqgrid-ftable").find("tbody").clone();
  63. if ($title.length) {
  64. $('<caption/>').prependTo($tb).append($title.clone()).append($subtitle.clone());
  65. }
  66. $tb.find("th").css("width", "auto");
  67. $summary.find("td").css("width", "auto");
  68. $data.children().eq(0).remove();
  69. $tb.append($data).append($summary);
  70. $(win.document.body).append($html).append($tb);
  71. }
  72. });
  73. //var $tb = _self.find("table.ui-jqgrid-htable").eq(0).clone().removeAttr("style").attr("class", "ui-table-print");
  74. //var $data = _self.find("table.ui-jqgrid-btable").eq(0).find("tbody").clone();
  75. //var $title = _self.find("div.grid-title");
  76. //var $subtitle = _self.find("div.grid-subtitle");
  77. //var $summary = _self.find("table.ui-jqgrid-ftable").find("tbody").clone();
  78. //if ($title.length) {
  79. // $('<caption/>').prependTo($tb).append($title.clone()).append($subtitle.clone());
  80. //}
  81. //$tb.find("th").css("width", "auto");
  82. //$summary.find("td").css("width", "auto");
  83. //$data.children().eq(0).remove();
  84. //$tb.append($data).append($summary);
  85. //$(win.document.body).append($html).append($tb);
  86. }
  87. // 开始打印
  88. if (timer) {
  89. clearTimeout(timer);
  90. }
  91. timer = setTimeout(function () {
  92. win.focus();
  93. win.print();
  94. }, 100);
  95. if (!opt.preview) {
  96. // 自销毁
  97. setTimeout(function () {
  98. iframe.parentNode && iframe.parentNode.removeChild(iframe);
  99. }, 1000);
  100. }
  101. }
  102. }, 100);
  103. return this;
  104. };
  105. $.fn.printTable = function (opt) {
  106. opt = opt || {};
  107. opt.table = true;
  108. opt.usePageStyle = false;
  109. return this.printArea(opt);
  110. };