EasyUI 打印当前页

function CommonPrint(printDatagrid, type) {
    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].field != "id" && frozenColumns[index][i].field != "cid" && frozenColumns[index][i].field != "pid") {
                        //  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].field != "id" && columns[index][i].field != "cid" && columns[index][i].field != "pid") {
                    //  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>';


    var tt = /\&/g;   //匹配&号
    var tt2 = /\?/g;   //匹配问号

    if (type == 1) {



        if (navigator.userAgent.indexOf("Chrome") > 0) {
            $('#ShowOpenWindow').html("");
            $('#ShowOpenWindow').append("<div><iframe src='../Purchase/Cargoprint.aspx?revalue=" + tableString.replace(tt, "").replace(tt2, "") + "' style='width:99%;margin: 0px; height: 400px;' ></iframe></div>");
            $('#ShowOpenWindow').window('refresh').window('open');
            $('#ShowOpenWindow').window('close');
        }
        else {
            window.showModalDialog("../print.html", tableString,
         "location:No;status:No;help:No;dialogWidth:1200px;dialogHeight:600px;scroll:auto;");
        }



    }
    else {

        var f = $('<form action="/export.aspx" method="post" id="fm1"></form>');
        var i = $('<input type="hidden" id="txtContent" name="txtContent" />');
        var l = $('<input type="hidden" id="txtName" name="txtName" />');
        i.val(tableString);
        i.appendTo(f);
        l.val(encodeURIComponent(ordernumber));
        l.appendTo(f);
        f.appendTo(document.body).submit();
        document.body.removeChild(f);
    }
}

  

posted @ 2015-12-30 17:07  放哨De老鼠  阅读(383)  评论(0编辑  收藏  举报