使用JS实现打印功能2

来源:互联网 发布:mac解压rar文件的软件 编辑:程序博客网 时间:2024/05/23 14:04

界面HTML调用打印方法:

 <div class="ptdiv"> <a href="javascript:void(0);" onclick="CreateFormPage('公司日报表' + $('#txtbegin').val() + '至' + $('#txtend').val(), $('#grid'));"> 打印</a></div>

CreateFormPage方法实现:

// strPrintName 打印任务名// printDatagrid 要打印的datagridfunction CreateFormPage(strPrintName, printDatagrid, mergeColumn) {    var tableString = ChangeToTable(printDatagrid);    var obj = window;    obj.name = strPrintName;    obj.value = tableString;    obj.mergeColumn = mergeColumn;    window.open('/print.htm', '', 'height=600,width=1100,toolbar=no,menubar=no, resizable=no,location=no,status=no');}
ChangeToTable(printDatagrid)方法实现:

function ChangeToTable(printDatagrid) {    var tableString = '<table cellspacing="0" class="pb">';    var frozenColumns = printDatagrid.datagrid("options").frozenColumns;  // 得到frozenColumns对象    var columns = printDatagrid.datagrid("options").columns;    // 得到columns对象    var nameList = new Array();    // 载入title    if (typeof columns != 'undefined' && columns != '') {        tableString += '\n<thead>';        $(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.push(frozenColumns[index][i]);                        }                        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.push(columns[index][i]);                    }                    tableString += '>' + columns[index][i].title + '</th>';                }            }            tableString += '\n</tr>';        });        tableString += '\n</thead>';    }    // 载入内容    var rows = printDatagrid.datagrid("getRows"); // 这段代码是获取当前页的所有行    tableString += '\n<tbody>';    for (var i = 0; i < rows.length; ++i) {        tableString += '\n<tr>';        for (var j = 0; j < nameList.length; ++j) {            var e = nameList[j].field.lastIndexOf('_0');            tableString += '\n<td';            if (nameList[j].align != 'undefined' && nameList[j].align != '') {                tableString += ' style="text-align:' + nameList[j].align + ';"';            }            tableString += '>';            if (e + 2 == nameList[j].field.length) {                tableString += rows[i][nameList[j].field.substring(0, e)];            }            else                tableString += rows[i][nameList[j].field];            tableString += '</td>';        }        tableString += '\n</tr>';    }    //合计    var footerRows = printDatagrid.datagrid("getFooterRows");    if (typeof footerRows != 'undefined') {        for (var i = 0; i < footerRows.length; ++i) {            tableString += '\n<tr>';            for (var j = 0; j < nameList.length; ++j) {                var e = nameList[j].field.lastIndexOf('_0');                tableString += '\n<td';                if (nameList[j].align != 'undefined' && nameList[j].align != '') {                    tableString += ' style="text-align:' + nameList[j].align + ';"';                }                tableString += '>';                if (e + 2 == nameList[j].field.length) {                    tableString += typeof footerRows[i][nameList[j].field.substring(0, e)] == "undefined" ? "" : footerRows[i][nameList[j].field.substring(0, e)];                }                else                    tableString += typeof footerRows[i][nameList[j].field] == "undefined" ? "" : footerRows[i][nameList[j].field];                tableString += '</td>';            }            tableString += '\n</tr>';        }    }    tableString += '\n</tbody>';    tableString += '\n</table>';    return tableString;}
打印界面print.html:

<head>    <title>数据打印</title>    <link rel="Stylesheet" type="text/css" href="/styles/print.css" />    <script type="text/javascript" src="/scripts/jquery.min.js"></script></head><body>    <div id="printBody">    </div>    <script type="text/javascript">        document.title = window.opener.name + " -- 数据打印";        document.write('<p>' + window.opener.name + '</p>' + window.opener.value);        var merge = window.opener.mergeColumn;        var t = 1;        var no;        if (typeof merge == 'number') {            $('.pb>tbody tr').each(function () {                var o = $($(this).children('td')[merge]);                if (typeof no == 'undefined') {                    no = o;                } else if (no.text() == o.text() || o.text() == '') {                    ++t;                    o.remove();                } else {                    if (t > 1) no.attr('rowspan', t);                    t = 1;                    no = o;                }            });            if (t > 1) no.attr('rowspan', t);        }        window.print();    </script></body>



0 0
原创粉丝点击