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 onclick="$(\‘.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" onclick="$(\‘.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 = ‘‘;

        // 载入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;
    }

    </script>
</body>
</html>

原文地址:https://www.cnblogs.com/ymnets/p/9574131.html

时间: 2024-08-03 07:10:06

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

构建ASP.NET MVC5+EF6+EasyUI 1.5+Unity4.x注入的后台管理系统(1)-前言与目录(持续更新中...)

前言: 起初写这个框架的时候,可以说在当时来说并不是很流行的设计模式,那是在2012年,面向对象的编程大家都很熟悉, 但是“注入.控制反转(DI,IOC,依赖注入).AOP切面编程”新兴名词 很多人并不知道特别是从事.NET开发的人,至少在当时 是这么样的,但是在今天它们却是非常流行的技术指标,很多大牛也承认,这是主流的开发模式,你们可以从招聘网的技术岗位看出. 我从事过MVC2.0到5.0的相关开发工作,见证了MVC的成熟演变过程,就像本框架一样,设计模式未曾改变,但是代码一直在重 构.我也坚

构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(51)-系统升级

系统很久没有更新内容了,最近花了2个月的时间每天一点点,从原有系统 MVC4+EF5+UNITY2.X+Quartz 2.0+easyui 1.3.4无缝接入 MVC5+EF6+Unity4.x+Quartz 2.3 +easyui 1.4.3. 并以easyui 1.4.3的gray皮肤为基础,升级10个扁平化皮肤 皮肤查看地址 更新的主要目的:新的MVC5特性和更好的性能 记录一下升级过程. 1.除了web层,其他全部提取. 2.新建解决方案.以前命名空间为App.现在更名为Apps. 3.

构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(66)-MVC WebApi 用户验证 (2)

前言: 构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(65)-MVC WebApi 用户验证 (1) 回顾上一节,我们利用webapi简单的登录并进行了同域访问与跨域访问来获得Token,您可以跳转到上一节下载代码来一起动手. 继续上一篇的文章,我们接下来演示利用拿到的Token来访问接口,管理接口,利用系统权限管理接口,对每个接口进行授权(管理接口为选读部分,因为你需要阅读最开始权限管理部分(18-27节),才能阅读这部分) 开发环境: V

ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-前言与目录(持续更新中...)

开发工具:VS2015(2012以上)+SQL2008R2以上数据库  您可以有偿获取一份最新源码联系QQ:729994997 价格 666RMB  升级后界面效果如下: 日程管理   http://www.cnblogs.com/ymnets/p/7094914.html 任务调度系统界面 http://www.cnblogs.com/ymnets/p/5065154.html 系统权限全套完整图  http://www.cnblogs.com/ymnets/p/5065201.html 系统

构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(1)-前言与目录(持续更新中...)

开篇:从50开始系统已经由MVC4+EF5+UNITY2.X+Quartz 2.0+easyui 1.3.4无缝接入 MVC5+EF6+Unity4.x+Quartz 2.3 +easyui 1.4.3. 从50节起为MVC5+EF6+Unity4.x+Quartz 2.3 +easyui 1.4.3.的特性文章 所以你们也要更新你们的环境 功能不变属于无缝接入,最大改变只在UI,初学同学,直接使用MVC5 开发工具:VS2013+SQL2012 相关代码:演示地址暂时关闭   第2讲源码下载 

ASP.NET MVC5+EF6+EasyUI 后台管理系统-关于WebApi的用法

1:ASP.NET MVC5+EF6+EasyUI 后台管理系统(1)-WebApi与Unity注入 使用Unity是为了使用我们后台的BLL和DAL层 2:ASP.NET MVC5+EF6+EasyUI 后台管理系统(2)-WebApi与Unity注入-配置文件 3:ASP.NET MVC5+EF6+EasyUI 后台管理系统(3)-MVC WebApi 用户验证 (1) 4:ASP.NET MVC5+EF6+EasyUI 后台管理系统(4)-MVC WebApi 用户验证 (2) 以往我们讲

ASP.NET MVC5 + EF6 + Bootstrap3 (7) Bootstrap的栅格系统

文章来源: Slark.NET-博客园http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-grid.html 上一节:ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用 源码下载:点我下载 要做一个完整的系统,除了需要MVC这样的B/S框架及EF这样的数据库访问技术之外,一个简洁.美观.大方的UI框架也是必不可少的. 话不多说,有请今天的主角登场!! 看看它的自我介绍,是不是很屌.没错,这个介绍一点都不夸

ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用

文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-ef-6-get-started-model.html 上一节:ASP.NET MVC5 + EF6 入门教程 (5) Model和Entity Framework 源码下载:点我下载 一.Razor简介 在解决方案资源管理器中查看Views文件夹下的文件,如下图所示. 文件的后缀名都是.cshtml.这是什么文件呢?顾名思义,cshtml = cs + html,就是一个包含C S

构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(54)-工作流设计-所有流程监控

系列目录 先补充一个平面化登陆页面代码,自己更换喜欢的颜色背景 @using Apps.Common; @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta content="IE=11.0000" http-equiv="X-UA-Compatible"> <meta http-equiv="Content-Type" content

构建ASP.NET MVC5+EF6+EasyUI 1.4.3+Unity4.x注入的后台管理系统(60)-系统总结

前言: 起初写这个框架的时候,可以说在当时来说并不是很流行的设计模式,那是在2012年,面向对象的编程大家都很熟悉, 但是“注入.控制反转(DI,IOC,依赖注入).AOP切面编程”新兴名词 很多人并不知道特别是从事.NET开发的人,至少在当时 是这么样的,但是在今天它们却是非常流行的技术指标,很多大牛也承认,这是主流的开发模式,你们可以从招聘网的技术岗 位看出. 嘿嘿... 我从事过MVC2.0到5.0的相关开发工作,见证了MVC的成熟演变过程,就像本框架一样,设计模式未曾改变,但是代码一直在