(原創)easyui基於web的列印實現
<%@ page language="java"pageEncoding="UTF-8"%>
<object id="WebBrowser"classid="ClSID:8856F961-340A-11D0-A96B-00C04Fd705A2"width="0" height="0">
</object>
<script language="javascript"type="text/javascript">
var tableString = "<linkrel='stylesheet' type='text/css' href='Lodop/print.css' /><tablecellspacing='0;' id='PrintBody'>"
function doPrint()
{
tableString += "<scriptlanguage='javascript'>window.print();</s"+"cript>";
// tableString.insertAdjacentHTML("beforeBegin","<scriptlanguage='javascript'>window.print();</s"+"cript>")
document.open('','','height=500,width=611,scrollbars=yes,status =yes');
document.write(tableString);
document.close();
}
// strPrintName 列印任務名
// printDatagrid 要列印的datagrid
function CreateFormPage(strPrintName, printDatagrid) {
var frozenColumns = printDatagrid.datagrid("options").frozenColumns;// 得到frozenColumns物件
var columns = printDatagrid.datagrid("options").columns;// 得到columns物件
// 載入title
tableString = tableString +
if(frozenColumns != undefined && frozenColumns != '') {
for(var i = 0;i<frozenColumns[0].length; i++) {
if(frozenColumns[0][i].hidden != true) {
tableString = tableString + "\n<th width= '" +frozenColumns[0][i].width+"'>" + frozenColumns[0][i].title + "</th>";
}
}
}
if(columns != undefined && columns != '') {
for(var i = 0;i<columns[0].length; i++) {
if(columns[0][i].hidden != true) {
tableString = tableString + "\n<th width= '" +columns[0][i].width+"'>" + columns[0][i].title + "</th>";
}
}
}
tableString = tableString + "\n</tr>";
// 載入內容
var rows = printDatagrid.datagrid("getRows"); // 這段程式碼是獲取當前頁的所有行。
for(var j = 0; j <rows.length;j++) {
tableString = tableString + "\n<tr>";
if(frozenColumns != undefined && frozenColumns != '') {
for(var i = 0;i<frozenColumns[0].length; i++) {
if(frozenColumns[0][i].hidden != true) {
tableString = tableString + "\n<td >" + rows[j][frozenColumns[0][i].field] + "</td>";
}
}
}
if(columns != undefined && columns != '') {
for(var i = 0;i<columns[0].length; i++) {
if(columns[0][i].hidden != true) {
tableString = tableString + "\n<td >" + rows[j][columns[0][i].field] + "</td>";
}
}
}
tableString = tableString + "\n</tr>";
}
tableString = tableString + "\n</table>";
doPrint();
}
</script>
使用方法: jsp頁面中
<jsp:include page="../../../Lodop/print2.jsp"></jsp:include>
Js頁面中
function prn_preview() {
CreateFormPage("核銷賬款明細", stockDocumentDatagrid);
};
第一個引數是單據名稱; 第二個是要列印的datagrid物件
作者: 張振斌時間:2013-3-18 QQ:739934487