⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 reportprinter.js

📁 报表打印例子报表打印例子报表打印例子报表打印例子报表打印例子
💻 JS
📖 第 1 页 / 共 2 页
字号:
report_template_url = 'public/js/reportTemplate.htm';
var pxPerMM = 0;  

function ReportPainter(printer){
	this.rHeader = printer.rHeader;
	this.pHeader = printer.pHeader;
	this.pFoot = printer.pFoot;
	this.rFoot = printer.rFoot;
	this.params = printer.params;
	this.columns = printer.columns;
	this.data = printer.data;
	this.context = printer.context;
	
	oThis = this;
	this.canvas = null;
	this.pIndexPointer= 0;
	this.tableStr = null;
	this.tableHead = "";
	this.pageWidth = null;
	this.pageHeight = null;


	var _list_sumrow_str = null;
	var _list_rowStr = new Object();

	this.createRowId = function(){
		for(var i=0;i<oThis.data.length;i++){
			oThis.data[i].l_r_id = i;
		}
	}	

	this.paint = function(canvas){
		oThis.createRowId();
		oThis.pageWidth = pxPerMM * (oThis.params.pageWidth - oThis.params.marginLeft - oThis.params.marginRight);
		oThis.pageHeight = pxPerMM * (oThis.params.pageHeight - oThis.params.marginTop - oThis.params.marginBottom);
		var page = oThis.createPage();
		canvas.appendChild(page);
		oThis.paintReportHead(page);		
		oThis.paintPageHead(page);			
		oThis.paintPageFoot(page);			
		oThis.paintReportFoot(page);		
		oThis.paintGridHead(page);			
		var rowIndex = 0;
		while(rowIndex>-1){
			if(rowIndex!=0){
				page = oThis.createPage();
				oThis.paintPageHead(page);			
				oThis.paintPageFoot(page);			
				oThis.paintGridHead(page);			
				canvas.appendChild(page);
			}
			rowIndex  = oThis.paintRowsInCurrentPage(page,rowIndex);
		}
		if(oThis.data!=null && oThis.data.length>0 && oThis.context.sum!=null && oThis.context.sum.length>0){
			var s = oThis.paintSumRowToPage(page);
			if(s<0){
				page = oThis.createPage();
				oThis.paintPageHead(page);			
				oThis.paintPageFoot(page);			
				oThis.paintGridHead(page);			
				canvas.appendChild(page);
				oThis.paintSumRowToPage(page);
			}
		}
	}

	var visibleRowsAmount = null;
	this.getVisibleRowsAmount = function(page){
		if(visibleRowsAmount==null){
			visibleRowsAmount = oThis.divide(oThis.getVisibleHeight(page),20);
		}
		return visibleRowsAmount;
	}

	this.getVisibleHeight = function(page){
		var canvas = page.all("canvas_"+page.index);
		var body = canvas.all("body_"+page.index);
		var foot = canvas.all("foot_"+page.index);
		var bottom = foot.offsetTop + foot.offsetHeight;
		return oThis.pageHeight-bottom;
	}

	this.divide = function(a,b){
		var r = ""+a/b;
		var s = r.split(".");
		return parseInt(s[0]);
	}

	this.getAllowHeight = function(){
		if(oThis.params.orientation==1){
			return oThis.pageHeight;
		}else{
			return oThis.pageWidth;
		}
	}

	this.getAllowWidth = function(){
		if(oThis.params.orientation==1){
			return oThis.pageWidth;
		}else{
			return oThis.pageHeight;
		}
	}

	var _report_current_max = 0;
	var _report_current_min = 0;

	this.paintRowsInCurrentPage = function(page,from,length){		
		if(length==null){
			length = oThis.getVisibleRowsAmount(page);
			_report_current_max = length;
			_report_current_min = 0;
		}
		if(from+length >  oThis.data.length){
			length = oThis.data.length - from;
		}
		oThis.paintRows(page,from,length);
		var canvas = page.all("canvas_"+page.index);
		var body = canvas.all("body_"+page.index);
		var foot = canvas.all("foot_"+page.index);
		var bottom = foot.offsetTop + foot.offsetHeight;
		var allowHeight = oThis.getAllowHeight();
		
		if(allowHeight-bottom<0){
			_report_current_max = length;
		}else{	//本页还有打印的空间
			if(length+from<oThis.data.length){  //还有数据可以打印
				_report_current_min = length;
			}else{
				return -1;
			}
		}
		length = oThis.divide((_report_current_max+_report_current_min)/2,1);
		if((length == _report_current_min) ||(length == _report_current_min)){
			return (from+length);
		}else{
			return this.paintRowsInCurrentPage(page,from,length);
		}
	}

	this.paintSumRowToPage = function(page){
		var canvas = page.all("canvas_"+page.index);
		var body = canvas.all("body_"+page.index);
		var sb = new Array();
		sb[sb.length] = '<table class="grid" id="dataTable" cellspacing=0 cellpadding=0 border=0 style="table-layout:fixed;border-top:0;">';
		sb[sb.length] = oThis.getColsStr();
		
		sb[sb.length] = oThis.paintSumRow();
		
		sb[sb.length] = '</table>';
		body.innerHTML = body.innerHTML + sb.join("");
		var foot = canvas.all("foot_"+page.index);
		var bottom = foot.offsetTop + foot.offsetHeight;
		var allowHeight = oThis.getAllowHeight();
		if(allowHeight-bottom<0){
			return -1;
		}
		return 1;
	}

	this.paintRows = function(page,from,length){		
		var canvas = page.all("canvas_"+page.index);
		var body = canvas.all("body_"+page.index);
		try{
			body.all("dataTable").removeNode(true);
		}catch(e){
		}
		var sb = new Array();
		sb[sb.length] = '<table class="grid" id="dataTable" cellspacing=0 cellpadding=0 border=0 style="table-layout:fixed;border-top:0;">';
		sb[sb.length] = oThis.getColsStr();
		
		for(var i=0;i<length;i++){
			var rowData = oThis.data[from+i];
			rowData._index = from+i;
			sb[sb.length] = oThis.paintRow(rowData);
		}
		sb[sb.length] = '</table>';
		body.innerHTML = body.innerHTML + sb.join("");
	}

	this.headDepth = 0;
	this.getHeadDepth = function(){
		if(oThis.headDepth == 0 ){
			var max = 0;
			for(var i=0;i<oThis.columns.length;i++){
				var column = oThis.columns[i];
				var cDepth = oThis.calcColDepth(column,1);
				if(cDepth>max){
					max = cDepth;
				}
			}
			oThis.headDepth = max;
		}
		return oThis.headDepth;
	}

	this.calcColDepth = function(column,currentDepth){
		if(column.children!=null && column.children.length>0){
			var max = currentDepth;
			for(var i=0;i<column.children.length;i++){
				var child = column.children[i];
				var cDepth = oThis.calcColDepth(child,currentDepth+1);
				if(cDepth>max){
					max = cDepth;
				}
			}
			column.colSpan = column.children.length;
			column.depth = currentDepth;
			return max;
		}else{
			column.colSpan = 1;
			column.depth = currentDepth;
			return currentDepth;
		}
	}
	this.getOrderedColumns = function(){
		return oThis.columns;
	}
	this.paintGridHead = function(page){
		this.headDepth = 0;
		var canvas = page.all("canvas_"+page.index);
		var body = canvas.all("body_"+page.index);
		var depth = oThis.getHeadDepth();
		var colGroup = document.createElement("colgroup");
		colGroup.id = "colgroup_" + page.index;
		var headTable= document.createElement('<table class="grid" cellspacing=0 cellpadding=0 border=0 style="table-layout:fixed;">');
		body.appendChild(headTable);
		var rows = [];
		
		for(var i=0;i<depth;i++){
			var row = headTable.insertRow();
			rows[i] = row;
		}
		var start = 0;
		if(oThis.context.sequence){
			var cell = rows[0].insertCell(start);
			cell.innerText = "序号";
			cell.className = "header";
			cell.rowSpan = rows.length;
			start++;
			
			var col = document.createElement("col");
			col.width = 40;
			colGroup.appendChild(col);
		}
		
		for(var i=0;i<oThis.columns.length;i++){			
			var column = oThis.columns[i];
			
			if(false==column._printAble){
				continue;
			}
			var cell = rows[0].insertCell(start++);
			cell.column = column;
			cell.className = "header";
			if(column.children==null || column.children.length<1){
				cell.rowSpan = rows.length;
				var width = oThis.getColumnWidth(column);
				cell.innerHTML = column.caption;

				var col = document.createElement("col");
				col.width = width;
				colGroup.appendChild(col);
			}else{
				cell.rowSpan = 1;
				cell.colSpan = column.children.length;
				cell.column = column;
				cell.innerHTML = column.caption;
				var width =oThis.getColumnWidth(column);
				for(var j=0;j<column.children.length;j++){
					var cColumn = column.children[j];
					cColumn.parent = column;
					var cCell = rows[1].insertCell();
					cCell.rowSpan = 1;
					cCell.className = "header";
					var width = oThis.getColumnWidth(cColumn);
					cCell.column = cColumn; 
					cCell.innerHTML = cColumn.caption;

					var col = document.createElement("col");
					col.width = width;
					col.id = "col_"+cColumn.name;
					colGroup.appendChild(col);
				}
			}
		}
		headTable.appendChild(colGroup);
		return headTable.outerHTML;
	}

	this.paintReportHead = function(page){
		if(oThis.rHeader == null)
			return ;
		var canvas = page.all("canvas_"+page.index);
		var head = canvas.all("head_"+page.index);
		head.innerHTML = head.innerHTML + oThis.rHeader;
	}

	this.paintPageHead = function(page){
		if(oThis.pHeader == null)
			return ;
		var canvas = page.all("canvas_"+page.index);
		var head = canvas.all("head_"+page.index);
		head.innerHTML = head.innerHTML + oThis.pHeader;
	}

	this.paintPageFoot = function(page){
		if(oThis.pFoot == null)
			return ;
		var canvas = page.all("canvas_"+page.index);
		var foot = canvas.all("foot_"+page.index);
		foot.innerHTML = foot.innerHTML + oThis.pFoot;
	}
	
	this.paintReportFoot = function(page){
		if(oThis.rFoot == null)
			return ;
		var canvas = page.all("canvas_"+page.index);
		var foot = canvas.all("foot_"+page.index);
		foot.innerHTML = foot.innerHTML + oThis.rFoot;
	}

	this.createPage = function(){
		var id = oThis.pIndexPointer;
		oThis.pIndexPointer ++;
		var page = document.createElement("div");
		page.id = "page_"+id;
		if(oThis.params.orientation==1){
			page.style.width = oThis.pageWidth;
			page.style.height = oThis.pageHeight;
		}else{
			page.style.width = oThis.pageHeight;
			page.style.height = oThis.pageWidth;
		}
		page.style.overflow = "hidden";
		page.index = id;
		var canvas = document.createElement("div");
		canvas.id = "canvas_"+id;
		with(canvas.style){
			marginLeft = oThis.params.marginLeft;
			marginRight = oThis.params.marginRight;
			marginTop = oThis.params.marginTop;
			marginBottom = oThis.params.marginBottom;
		}
		page.appendChild(canvas);

		var pageHead = document.createElement("div");
		var pageFoot = document.createElement("div");
		pageHead.id = "head_"+id;
		pageFoot.id = "foot_"+id;
		var pageBody = document.createElement("div");
		pageBody.id = "body_"+id;
		canvas.appendChild(pageHead);
		canvas.appendChild(pageBody);
		canvas.appendChild(pageFoot);
		return page;
	}

	this.getColumnWidth = function(column){
		if(column==null)
			return 0;
		if(column.width == null){
			if(column.children!=null && column.children.length>0){
				var w = 0;
				for(var i=0;i<column.children.length;i++){
					var c = column.children[i];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -