在新窗口打印时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();
});

最新文章

  1. WebForm 常用控件
  2. NOIP2013 提高组day2 2 花匠 动规 找拐点 树状数组
  3. CentOS 简单命令
  4. C++之内部类(嵌套类)与外部类及友元
  5. c# 基础连接已经关闭: 连接被意外关闭,错误的解决
  6. Sqoop是一款开源的工具,主要用于在HADOOP(Hive)与传统的数据库(mysql、oracle...)间进行数据的传递
  7. CH Round #57 - Story of the OI Class 凯撒密码
  8. HDU 6024(中国大学生程序设计竞赛女生专场1002)
  9. 【Qt编程】Qt学习之窗口间的相互切换
  10. 在C#/.NET应用程序开发中创建一个基于Topshelf的应用程序守护进程(服务)
  11. ThinkPHP5配置redis缓存
  12. HTML XML 介绍
  13. OO Summary Ⅲ
  14. python 几个重要的概念
  15. Unity3D学习笔记(六):三角函数和点乘
  16. Eclipse常用且不易记快捷键
  17. Gradle sync failed: Read timed out
  18. 跟我学算法- tensorflow VGG模型进行测试
  19. Uber优步北京第一组奖励政策
  20. numpy及scipy的使用

热门文章

  1. Content Provider
  2. Office 如何复印身份证正反面到一张纸上
  3. RTP 时间戳的处理
  4. java中的初始化块
  5. 算法笔记_059:蓝桥杯练习 Anagrams问题(Java)
  6. mybatis 表情存储报错问题解决
  7. hibernate 关系映射之 主键关联一对一
  8. 网站缓存技术(Redis、Memcached、Ehcache)
  9. 使用Nexus创建私服
  10. RPM卸载