ASP.NET MVC5+EF6+EasyUI 后台管理系统(92)-打印EasyUI 的datagrid表格

前言

应用系统有时候需要打印Datagrid的表格内容,我们本节就来学习打印datagrid内容

打印主要使用:web打印(我们之前有讲过web打印插件jqprint) + 将datagrid重新编制成可以打印的html格式

一、建立一个普通的例子

我们使用官方下载的demo下的datagrid basic.html代码就好

引入Jqgrid打印插件,并增加一个按钮来触发打印事件

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Basic DataGrid - jQuery EasyUI Demo</title><link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="../../themes/icon.css"><link rel="stylesheet" type="text/css" href="../demo.css"><script type="text/javascript" src="../../jquery.min.js"></script><script type="text/javascript" src="../../jquery.easyui.min.js"></script><script src="jquery.jqprint-0.3.js"></script>
</head>
<body><h2>Basic DataGrid</h2><p>The DataGrid is created from markup, no JavaScript code needed.</p><div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div><div style="margin:20px 0;"><a href="#" id="btnPrint" class="easyui-linkbutton" data-options="iconCls:'icon-print'" style="width:80px">Print</a></div><table id="List" class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"data-options="singleSelect:true,collapsible:true,method:'get'"><thead><tr><th data-options="field:'itemid',width:80">Item ID</th><th data-options="field:'productid',width:100">Product</th><th data-options="field:'listprice',width:80,align:'right'">List Price</th><th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th><th data-options="field:'attr1',width:250">Attribute</th><th data-options="field:'status',width:60,align:'center'">Status</th></tr></thead></table><script type="text/javascript">$(function () {//由于本地无法直接读取json文件,所以将数据提取出来赋值var obj = {"total": 28, "rows": [{ "productid": "FI-SW-01", "productname": "Koi", "unitcost": 10.00, "status": "P", "listprice": 36.50, "attr1": "Large", "itemid": "EST-1" },{ "productid": "K9-DL-01", "productname": "Dalmation", "unitcost": 12.00, "status": "P", "listprice": 18.50, "attr1": "Spotted Adult Female", "itemid": "EST-10" },{ "productid": "RP-SN-01", "productname": "Rattlesnake", "unitcost": 12.00, "status": "P", "listprice": 38.50, "attr1": "Venomless", "itemid": "EST-11" },{ "productid": "RP-SN-01", "productname": "Rattlesnake", "unitcost": 12.00, "status": "P", "listprice": 26.50, "attr1": "Rattleless", "itemid": "EST-12" },{ "productid": "RP-LI-02", "productname": "Iguana", "unitcost": 12.00, "status": "P", "listprice": 35.50, "attr1": "Green Adult", "itemid": "EST-13" },{ "productid": "FL-DSH-01", "productname": "Manx", "unitcost": 12.00, "status": "P", "listprice": 158.50, "attr1": "Tailless", "itemid": "EST-14" },{ "productid": "FL-DSH-01", "productname": "Manx", "unitcost": 12.00, "status": "P", "listprice": 83.50, "attr1": "With tail", "itemid": "EST-15" },{ "productid": "FL-DLH-02", "productname": "Persian", "unitcost": 12.00, "status": "P", "listprice": 23.50, "attr1": "Adult Female", "itemid": "EST-16" },{ "productid": "FL-DLH-02", "productname": "Persian", "unitcost": 12.00, "status": "P", "listprice": 89.50, "attr1": "Adult Male", "itemid": "EST-17" },{ "productid": "AV-CB-01", "productname": "Amazon Parrot", "unitcost": 92.00, "status": "P", "listprice": 63.50, "attr1": "Adult Male", "itemid": "EST-18" }]};
//加载数据$(
'#List').datagrid('loadData', obj);});</script> </body> </html>

 

 

二、将datagrid数据分解成HTML Table表格

function CreateFormPage(printDatagrid) {var tableString = '<div class="mvctool bgb"><a οnclick="$(\'.dg-pb\').jqprint();" class="btn btn-default"><span class="fa fa-print"></span>&nbsp;打印</a></div><table cellspacing="0" class="dg-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) {if (rows[i][nl[j].f.substring(0, e)] != null) {tableString += rows[i][nl[j].f.substring(0, e)];} else {tableString += "";}}else { if (rows[i][nl[j].f] != null) {tableString += rows[i][nl[j].f];} else {tableString += "";}}tableString += '</td>';});tableString += '\n</tr>';}tableString += '\n</table>';return tableString;
}

代码看起来有点复杂,但是不难看懂,提取datagrid的title和历遍数据得重新写入一个新的table

三、添加打印事件

     $("#btnPrint").click(function () {var tablestr = CreateFormPage($("#List"));$("#modalwindow").html(tablestr);$("#modalwindow").window({ title: "打印", width:500, height: 400, iconCls: 'fa fa-plus' }).window('open');});

四、预览结果

总结:

不是很美观,那么加入一段CSS吧

/*datagrid print*/
.dg-pb{font-size:13px;border-collapse:collapse;}
.dg-pb th{font-weight:bold;text-align:center;border:1px solid #333333;padding:2px;}
.dg-pb td{border:1px solid #333333;padding:2px;}

 

再次在预览的结果点击打印调出打印机

 本节完整代码下载

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Basic DataGrid - jQuery EasyUI Demo</title><link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css"><link rel="stylesheet" type="text/css" href="../../themes/icon.css"><link rel="stylesheet" type="text/css" href="../demo.css"><script type="text/javascript" src="../../jquery.min.js"></script><script type="text/javascript" src="../../jquery.easyui.min.js"></script><script src="jquery.jqprint-0.3.js"></script>
</head><body><style>/*datagrid print*/
.dg-pb{font-size:13px;border-collapse:collapse;}
.dg-pb th{font-weight:bold;text-align:center;border:1px solid #333333;padding:2px;}
.dg-pb td{border:1px solid #333333;padding:2px;}</style><h2>Basic DataGrid</h2><p>The DataGrid is created from markup, no JavaScript code needed.</p><div id="modalwindow" class="easyui-window" style="width:800px; height:400px;" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div><div style="margin:20px 0;"><a href="#" id="btnPrint" class="easyui-linkbutton" data-options="iconCls:'icon-print'" style="width:80px">Print</a></div><table id="List" class="easyui-datagrid" title="Basic DataGrid" style="width:700px;height:250px"data-options="singleSelect:true,collapsible:true,method:'get'"><thead><tr><th data-options="field:'itemid',width:80">Item ID</th><th data-options="field:'productid',width:100">Product</th><th data-options="field:'listprice',width:80,align:'right'">List Price</th><th data-options="field:'unitcost',width:80,align:'right'">Unit Cost</th><th data-options="field:'attr1',width:250">Attribute</th><th data-options="field:'status',width:60,align:'center'">Status</th></tr></thead></table><script type="text/javascript">$(function () {var obj = {"total": 28, "rows": [{ "productid": "FI-SW-01", "productname": "Koi", "unitcost": 10.00, "status": "P", "listprice": 36.50, "attr1": "Large", "itemid": "EST-1" },{ "productid": "K9-DL-01", "productname": "Dalmation", "unitcost": 12.00, "status": "P", "listprice": 18.50, "attr1": "Spotted Adult Female", "itemid": "EST-10" },{ "productid": "RP-SN-01", "productname": "Rattlesnake", "unitcost": 12.00, "status": "P", "listprice": 38.50, "attr1": "Venomless", "itemid": "EST-11" },{ "productid": "RP-SN-01", "productname": "Rattlesnake", "unitcost": 12.00, "status": "P", "listprice": 26.50, "attr1": "Rattleless", "itemid": "EST-12" },{ "productid": "RP-LI-02", "productname": "Iguana", "unitcost": 12.00, "status": "P", "listprice": 35.50, "attr1": "Green Adult", "itemid": "EST-13" },{ "productid": "FL-DSH-01", "productname": "Manx", "unitcost": 12.00, "status": "P", "listprice": 158.50, "attr1": "Tailless", "itemid": "EST-14" },{ "productid": "FL-DSH-01", "productname": "Manx", "unitcost": 12.00, "status": "P", "listprice": 83.50, "attr1": "With tail", "itemid": "EST-15" },{ "productid": "FL-DLH-02", "productname": "Persian", "unitcost": 12.00, "status": "P", "listprice": 23.50, "attr1": "Adult Female", "itemid": "EST-16" },{ "productid": "FL-DLH-02", "productname": "Persian", "unitcost": 12.00, "status": "P", "listprice": 89.50, "attr1": "Adult Male", "itemid": "EST-17" },{ "productid": "AV-CB-01", "productname": "Amazon Parrot", "unitcost": 92.00, "status": "P", "listprice": 63.50, "attr1": "Adult Male", "itemid": "EST-18" }]};$('#List').datagrid('loadData', obj);$("#btnPrint").click(function () {var tablestr = CreateFormPage($("#List"));$("#modalwindow").html(tablestr);$("#modalwindow").window({ title: "打印", width:700, height: 400, iconCls: 'fa fa-plus' }).window('open');});});function CreateFormPage(printDatagrid) {var tableString = '<div class="mvctool bgb"> <a href="#" id="btnPrint" class="easyui-linkbutton" data-options="iconCls:\'icon-print\'" style="width:80px" οnclick="$(\'.dg-pb\').jqprint();">打印</a></div><table cellspacing="0" class="dg-pb">';var frozenColumns = printDatagrid.datagrid("options").frozenColumns;  // 得到frozenColumns对象var columns = printDatagrid.datagrid("options").columns;    // 得到columns对象var nameList = '';// 载入titleif (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) {if (rows[i][nl[j].f.substring(0, e)] != null) {tableString += rows[i][nl[j].f.substring(0, e)];} else {tableString += "";}}else {if (rows[i][nl[j].f] != null) {tableString += rows[i][nl[j].f];} else {tableString += "";}}tableString += '</td>';});tableString += '\n</tr>';}tableString += '\n</table>';return tableString;}</script>
</body>
</html>
View Code

 

转载于:https://www.cnblogs.com/ymnets/p/9574131.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/266145.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

全国计算机等级考试题库二级C操作题100套(第68套)

第68套&#xff1a; 给定程序中&#xff0c;函数fun的功能是:求ss所指字符串数组中长度最长的字符串所 在的行下标,作为函数值返回&#xff0c;并把其串长放在形参n所指变量中。ss所指字符串数组中共有M个字符串&#xff0c;且串长<N。 请在程序的下划线处填入正确的内容并…

速读训练软件_记忆宫殿记忆力训练教程-第八天

第四部分 右脑开发及训练前面我们已经知道了右脑的记忆能力是左脑的100万倍&#xff0c;这可不是个小数字&#xff0c;如果我们能利用右脑来处理眼睛所收集到的信息&#xff0c;我们的记忆能力就会达到每分钟数万字的超级速度右脑不同于左脑的四种能力为&#xff1a;1、共振能力…

uniapp背景图片android不显示,uni-app网络图片在app不显示,小程序显示

引用的网络图片&#xff0c;ios显示空白&#xff0c;banner自动切换的图片content:,state:0,imgUrls: ["https://m.360buyimg.com/n12/jfs/t11317/108/1080677336/325163/f4c2a03a/59fd8b17Nbe2fcca3.jpg!q70.jpg","https://m.360buyimg.com/n12/jfs/t11575/282…

Razor语法

原文 http://www.cnblogs.com/lmfeng/archive/2013/03/28/2986073.html Razor语法概要&#xff1a; 1、Razor是以为标识符 2、是以{}作为作用域的标识 3、可以按照一定规则将HTML和C#混合编写 4、若输出要使用进行转义 { Layout "~/_SiteLayout.cshtml"; Page.Titl…

mysql 随机选取一条记录

要从tablename表中随机提取一条记录&#xff0c;大家一般的写法就是&#xff1a;SELECT * FROM tablename ORDER BY RAND() LIMIT 1。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 mysql> SELECT RAND(); ------------------- |…

全国计算机等级考试题库二级C操作题100套(第69套)

第69套&#xff1a; 给定程序中&#xff0c;函数fun的功能是将a和b所指的两个字符串转换成面值相同的整数&#xff0c;并进行相加作为函数值返回&#xff0c;规定字符串中只含9个以下数字字符。 例如&#xff0c;主函数中输入字符串&#xff1a;32486和12345&#xff0c;在主函…

python去除图片上的文字_Python图像处理之识别图像中的文字(实例讲解)

①安装PIL&#xff1a;pip install Pillow(之前的博客中有写过)②安装pytesser3&#xff1a;pip install pytesser3③安装pytesseract&#xff1a;pip install pytesseract④安装autopy3&#xff1a;先安装wheel&#xff1a;pip install wheel下载autopy3-0.51.1-cp36-cp36m-wi…

android基于蓝牙实验,基于Android智能蓝牙的血糖实时监测系统的设计与实现

摘要&#xff1a;糖尿病是目前严重危害人类健康的一种疾病,掌握血糖的变化是现阶段临床上预防和控制糖尿病最重要也是最有效的方法。目前市场上已有的血糖仪大部分是以电化学方法为基础,将检测结果保存到检测仪器中,不利于对数据进行相关分析。论文基于嵌入式检测技术与Android…

[Vue]组件——通过$emit为组件自定义事件

1.在定义组件时调用内建的 $emit 方法并传入事件的名字&#xff0c;来向父级组件触发一个事件enlarge-text&#xff1a; Vue.component(blog-post, {props: [post],template: <div class"blog-post"><h3>{{ post.title }}</h3><button v-on:cli…

全国计算机等级考试题库二级C操作题100套(第70套)

第70套&#xff1a; 给定程序中&#xff0c;函数fun的功能是&#xff1a;计算形参x所指数组中N个数的平均值&#xff08;规定所有数均为正数&#xff09;,作为函数值返回&#xff1b;并将大于平均值的数放在形参y所指数组中, 在主函数中输出。 例如&#xff0c;有10个正数&…

浅谈mysql的子查询

2019独角兽企业重金招聘Python工程师标准>>> mysql的子查询的优化一直不是很友好&#xff0c;一直有受业界批评比较多,也是我在sql优化中遇到过最多的问题之一&#xff0c;mysql在处理子查询的时候&#xff0c;会将子查询改写,通常情况下&#xff0c;我们希望由内到…

选择符合语义的html标签,最容易犯的HTML标签错误写法

我们最好开始注意了,因为HTML Police会走遍你的代码然后挑出你所有没有语义的标签,这份列表包含了10个最经常犯得HTML标签错误,记下他们,能够让我们避免犯此常见错误,让我们的HTML标签符合语义,和标准的要求.罪行1:把块级元素放入了行内元素内HTML 元素的表现方式不外乎就两种:…

python装饰器参数讲解_python装饰器的详细解析

写在前面:python装饰器(fuctional decorators)就是用于拓展原来函数功能的一种函数&#xff0c;目的是在不改变原函数名(或类名)的情况下&#xff0c;给函数增加新的功能。 这个函数的特殊之处在于它的返回值也是一个函数&#xff0c;这个函数是内嵌“原“”函数的函数。一般而…

Linux进程编程(PS: exec族函数、system、popen函数)

目录1.进程相关概念程序和进程查看系统中的进程ps指令top指令进程标识符 使用getpid()获取父进程&#xff0c;子进程2.创建进程fork进程创建发生了什么——C程序的存储空间如何分配3.创建进程vfork(区别fork)4.进程退出正常退出异常退出5.父进程等待子进程退出父进程收集子进程…

亚马逊云主机AWS-EC2建站简介

其实挺多是参考互联网的&#xff0c;所以直接贴网址了。 【申请帐号】要信用卡号码的 http://bbs.admin5.com/thread-9440113-1-1.html 【地区测速】http://www.cloudping.info/ 【安装系统及SSH】要看清免费的项目 http://blog.chinaunix.net/uid-26726420-id-3196899.html 【…

简练软考知识点整理-控制成本过程

控制成本是监督项目状态&#xff0c;以更新项目成本&#xff0c;管理成本基准变更的过程。本过程的主要作用是&#xff0c;发现实际与计划的差异&#xff0c;以便采取纠正措施&#xff0c;降低风险。要更新预算&#xff0c;就需要了解截至目前的实际成本。只有经过实施整体变更…

求一列数据中的波峰_PowerQuery:横向/纵向追加数据

上一篇文章都是在原表数据基础上的分分合合&#xff0c;但做数据分析的时候还经常需要在原有数据的基础上增加一些辅助数据&#xff0c;比如加入新列、新行&#xff0c;或者从其他表中添加进来更多维度的数据&#xff0c;这些就是数据丰富的过程。01添加列Power Query中添加列有…

二级菜单HTML原理,CSS多级菜单的实例代码讲解

这是一个相当炫的功能&#xff0c;让网页看起来像桌面程序&#xff0c;如window的开始菜单。实现原理基本和纯CSS相册差不多&#xff0c;但要注意的事项比较多&#xff0c;让我们一步步来吧。先来一个非常简单的一级菜单与悬停效果。菜单一菜单二菜单三菜单四结构很熟悉吧&…

HTML对字体的操作详解

摘自&#xff1a;HTML对字体的所有操作详解&#xff08;经典&#xff09; 作者&#xff1a;HeroKern 发布时间&#xff1a; 2016-01-31 21:15:31 网址&#xff1a;https://blog.csdn.net/qq_21792169/article/details/50615919/?utm_termhtml%E6%A0%87%E8%AE%B0%E5%AD%97%E4%B…

Discuz初探

根目录文件 admin.php 后台入口文件 api.php 合作API输出 archiver 论坛Archiver阅读模式&#xff08;无图版&#xff09; connect.php 云平台接口文件 cp.php 多应用服务入口文件(加载userapp.php) crossdomain.xml fa…