<%@ 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 + "\n<tr>";
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对象