在使用easyui插件的时候,使用最多的应该是datagrid插件。有时候根据客户需求,可能需要将datagrid内容进行打印,这时候如果直接调用window.print,可能由于easyui的datagrid存在滚动条而导致滚动条其他部分打印不出来的问题。通过百度有编“easyui基于web的打印实现”可以基本实现打印,但是还是存在一些问题,比如多表头情况打印不了,某列有调用脚本的,将会出现undefined等等。

通过修改该文的代码,就可以实现直接使用脚本打印easyui的datagrid内容,代码如下:

print.js

// strPrintName 打印任务名
// printDatagrid 要打印的datagrid
function CreateFormPage(strPrintName, printDatagrid) {
var tableString = '<table cellspacing="0" class="pb">';
var frozenColumns = printDatagrid.datagrid("options").frozenColumns; // 得到frozenColumns对象
var columns = printDatagrid.datagrid("options").columns; // 得到columns对象
var nameList = ''; // 载入title
if (typeof columns != 'undefined' && columns != '') {
$(columns).each(function (index) {
tableString += '\n<tr>';
if (typeof frozenColumns != 'undefined' && typeof frozenColumns[index] != 'undefined') {
for (var i = 0; i < frozenColumns[index].length; ++i) {
if (!frozenColumns[index][i].hidden) {
tableString += '\n<th width="' + frozenColumns[index][i].width + '"';
if (typeof frozenColumns[index][i].rowspan != 'undefined' && frozenColumns[index][i].rowspan > 1) {
tableString += ' rowspan="' + frozenColumns[index][i].rowspan + '"';
}
if (typeof frozenColumns[index][i].colspan != 'undefined' && frozenColumns[index][i].colspan > 1) {
tableString += ' colspan="' + frozenColumns[index][i].colspan + '"';
}
if (typeof frozenColumns[index][i].field != 'undefined' && frozenColumns[index][i].field != '') {
nameList += ',{"f":"' + frozenColumns[index][i].field + '", "a":"' + frozenColumns[index][i].align + '"}';
}
tableString += '>' + frozenColumns[0][i].title + '</th>';
}
}
}
for (var i = 0; i < columns[index].length; ++i) {
if (!columns[index][i].hidden) {
tableString += '\n<th width="' + columns[index][i].width + '"';
if (typeof columns[index][i].rowspan != 'undefined' && columns[index][i].rowspan > 1) {
tableString += ' rowspan="' + columns[index][i].rowspan + '"';
}
if (typeof columns[index][i].colspan != 'undefined' && columns[index][i].colspan > 1) {
tableString += ' colspan="' + columns[index][i].colspan + '"';
}
if (typeof columns[index][i].field != 'undefined' && columns[index][i].field != '') {
nameList += ',{"f":"' + columns[index][i].field + '", "a":"' + columns[index][i].align + '"}';
}
tableString += '>' + columns[index][i].title + '</th>';
}
}
tableString += '\n</tr>';
});
}
// 载入内容
var rows = printDatagrid.datagrid("getRows"); // 这段代码是获取当前页的所有行
var nl = eval('([' + nameList.substring(1) + '])');
for (var i = 0; i < rows.length; ++i) {
tableString += '\n<tr>';
$(nl).each(function (j) {
var e = nl[j].f.lastIndexOf('_0'); tableString += '\n<td';
if (nl[j].a != 'undefined' && nl[j].a != '') {
tableString += ' style="text-align:' + nl[j].a + ';"';
}
tableString += '>';
if (e + 2 == nl[j].f.length) {
tableString += rows[i][nl[j].f.substring(0, e)];
}
else
tableString += rows[i][nl[j].f];
tableString += '</td>';
});
tableString += '\n</tr>';
}
tableString += '\n</table>'; window.showModalDialog("/print.htm", tableString,
"location:No;status:No;help:No;dialogWidth:800px;dialogHeight:600px;scroll:auto;");
}

print.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>数据打印</title>
<style type="text/css">
body{background:white;margin:0px;padding:0px;font-size:13px;text-align:left;}
.pb{font-size:13px;border-collapse:collapse;}
.pb th{font-weight:bold;text-align:center;border:1px solid #333333;padding:2px;}
.pb td{border:1px solid #333333;padding:2px;}
  </style>
</head>
<body>
<script type="text/javascript">
document.write(window.dialogArguments);
window.print();
</script>
</body>
</html>

调用方式如:

<a href="javascript:void(0);" onclick="CreateFormPage('打印测试', $('#grid'));">打印</a>

easyui的datagrid打印(转)的更多相关文章

  1. EasyUI的DataGrid 打印导出

    EasyUI的DataGrid 打印导出   一直在Winform和WPF的项目,偶尔接触Web都是网上下个模板修修改改就成了,学习的不太深入. 今日遇到一个打印导出datagrid的问题,做桌面项目 ...

  2. easyUI 中datagrid 返回列隐藏方法

    easyui的datagrid方法返回的列,有的值不需要显示可以使用hidden(属性进行隐藏) columns : [ [{ field : 'bailClass', title : '类别', w ...

  3. EasyUI 中 DataGrid 控件 列 如何绑定对象中的属性

    EasyUI 中 DataGrid 控件 是我们经常用到的控件之一, 但是 DataGrid 控件 在绑定显示列时却不支持对象属性绑定. 模型如下: public class Manager impl ...

  4. easyui的datagrid行的某一列添加链接

    通过formatter方法给easyui 的datagrid 每行增加操作链接. 效果图 jsp代码: <th field="url" width="100&quo ...

  5. EasyUI的datagrid分页

    EasyUI的datagrid分页 前台代码: <script type="text/javascript"> $(function () { //查询 search( ...

  6. easyui使用datagrid时列名包含特殊字符导致表头与数据错位的问题

    做一个用easyui的datagrid显示数据的功能时发现表格的列头与数据错位了,而且这个现象不总是能重现,一直没搞清楚原因.后来偶然在控制台看出了一点端倪: 推测表头或者单元格的class名应该是用 ...

  7. 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能

    我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...

  8. 修改easyui中datagrid表头和数据不能分开对齐的BUG。

    easyui的datagrid中表头和列只能同时全部向左对齐,全部向右对齐或者居中对齐. 有时候有需求,数据向左或向右,表头居中对齐. 在不修改源码的情况下.下面的代码可以实现该功能. 把下面代码放在 ...

  9. JQuery EasyUI的datagrid的使用方式总结

    JQuery EasyUI的datagrid的使用方式总结第一步:添加样式和js脚本在前台添加展示数据表格的table元素 例如: <div> <table id="tt& ...

随机推荐

  1. ubuntu 下安装32位库 ia32-libs方法

    这两天在整Linux系统,要配置JDK,SDK环境,但是SDK配置使用的时候,提示没有权限. 需要安装32位库来解决,还有就是在修改文件的时候提示不能修改.唉...反正就是没有办法正常操作啦 后来根据 ...

  2. 使用angularJS遇见的一些问题的解决方案

    1. angularJS的$http.post请求,SpringMVC后台接收不到参数值的解决方案 问题一般为:400 Required String parameter 'rPassword' is ...

  3. javascript遍历算法与技巧

    转载自脚本之家,以下为链接 http://www.jb51.net/Special/281.htm

  4. Docker学习<一>--初体验Windows环境下安装

    背景 今天想试用spring boot与jwt协议的实现,配套就需要使用redis,但redis似乎windows环境版本部署起来不是那么舒心,果断尝试使用docker. 下载 下载地址: 稳定版:h ...

  5. CR LF的由来

    学习Esperanto时用到一款叫做Kajero的软件,支持世界语特殊字符编辑. 在Option菜单中有个选项,End of line 列出了四种换行方式 这四种都是由基本CR和LF组成.那么CR和L ...

  6. PHP 汉字转拼音类

    本文转载自:http://www.epubit.com.cn/article/867 <?php function Pinyin($_String, $_Code='gb2312') { $_D ...

  7. 基于Xenomai和工控机的实时测控系统的研究

    http://www.docin.com/p-1006254643-f6.html

  8. easyui datagrid加载json

      服务端: string pseries = context.Request["ajaxSearch"].ToString().Trim(); var jsonMap = new ...

  9. 远程访问MySQL遇到的一些问题

    一.access deny GRANT ALL PRIVILEGES ON *.* TO '用户名'@'IP地址' IDENTIFIED BY '密码' with grant option;

  10. 清除路由器NAT地址转换

    首先当你的NAT网络地址转换成功搭建起来,并且测试过网络通信时,此时NAT地址转换表上面是存在转换信息的,你可以通过在特权模式下输入命令"show ip nat translation&qu ...