BootStrap带样式打印
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了BootStrap带样式打印,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2617字,纯文字阅读大概需要4分钟。
内容图文
在新窗口打印时bootstrap表格的样式出不来,因为打印时没有加载CSS样式。
我在jquery.PrintArea.js的基础上改造了下打印的方法:
(function ($) { var printAreaCount = 0; $.fn.printArea = function () { var ele = $(this); var idPrefix = "printArea_"; removePrintArea(idPrefix + printAreaCount); printAreaCount++; var iframeId = idPrefix + printAreaCount; var iframeStyle = ‘position:absolute;width:0px;height:0px;left:-500px;top:-500px;‘; iframe = document.createElement(‘IFRAME‘); $(iframe).attr({ style: iframeStyle, id: iframeId }); document.body.appendChild(iframe); var doc = iframe.contentWindow.document; $(document).find("link").filter(function () { return $(this).attr("rel").toLowerCase() == "stylesheet"; }).each( function () { doc.write(‘<link type="text/css" rel="stylesheet" href="‘ + $(this).attr("href") + ‘" >‘); }); doc.write(‘<div class="‘ + $(ele).attr("class") + ‘">‘ + $(ele).html() + ‘</div>‘); doc.close(); var frameWindow = iframe.contentWindow; frameWindow.close(); frameWindow.focus(); frameWindow.print(); } var removePrintArea = function (id) { $("iframe#" + id).remove(); }; $.fn.printAreas = function () { var ele = $(this); winname = window.open(‘‘, "_blank", ‘‘); var doc = winname.document; $(document).find("link").filter(function () { return $(this).attr("rel").toLowerCase() == "stylesheet"; }).each( function () { doc.write(‘<link type="text/css" rel="stylesheet" href="‘ + $(this).attr("href") + ‘" >‘); }); doc.write($(ele).html()); doc.close(); winname.print(); } })(jQuery);
前台调用的时候,把table放在一个div里,打印div即可:
<div class="ibox-content" id="divprint"> <table id="tablePrint" class="footable table table-stripped toggle-arrow-tiny" data-page-size="8"> <thead> <tr> <th class="text-center">项目</th> <th class="text-center">部门</th> <th class="text-center">营业收入</th> <th class="text-center">利润</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td class="text-center">@item.ProjectName</td> <td class="text-center">@item.DepartNameS</td> <td class="text-right"><a onclick="loadDetail(‘@item.ProjectId‘,‘@item.DepartId‘)">@item.InComeS</a></td> <td class="text-right"><a onclick="loadProftsDetail(‘@item.ProjectId‘,‘@item.DepartId‘)">@item.ProfitS</a></td> </tr> } </tbody> </table> <div class="dataTables_paginate paging_bootstrap pagination"> </div> </div>
js调用:
// 打印 $("#print").click(function () $("#divprint").printAreas(); });
原文:http://www.cnblogs.com/kennyliu/p/5009416.html
内容总结
以上是互联网集市为您收集整理的BootStrap带样式打印全部内容,希望文章能够帮你解决BootStrap带样式打印所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。