Flex实践——Datagrid的打印预览与打印

DataGridPrint.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				height="450" 
				width="550">
	<mx:states>
		<mx:State name="printView">
			<mx:RemoveChild target="{myForm}"/>
			<mx:AddChild position="lastChild">
				<mx:Panel width="388" height="303" layout="absolute">
					<mx:Image id="img" x="10" y="10"/>
				</mx:Panel>
			</mx:AddChild>
			<mx:AddChild position="lastChild">
				<mx:Button label="Back" click="currentState=&quot;&quot;"/>
			</mx:AddChild>
		</mx:State>
	</mx:states>
	
	<mx:Script>
		<![CDATA[
			import mx.printing.FlexPrintJob;
			import MyPrintView;
			import mx.graphics.ImageSnapshot;
			import mx.core.UIComponent;
			private function print(u:UIComponent):void{
				currentState="printView";
				var bmp:BitmapData = ImageSnapshot.captureBitmapData(u);
				var i:Bitmap = new Bitmap(bmp);                
				img.source = i;
				img.scaleContent = true;
			}
			
			public function doPrint():void {
				// Create a FlexPrintJob instance.
				var printJob:FlexPrintJob = new FlexPrintJob();
				
				// Start the print job.
				if(printJob.start()) {
					// Create a MyPrintView control as a child 
					// of the current view.
					var formPrintView:MyPrintView = new MyPrintView();
					addChild(formPrintView);   
					// Set the print control's data grid data provider to be 
					// the displayed data grid's data provider.
					formPrintView.myDataGrid.dataProvider = 
						myDataGrid.dataProvider;   
					// Add the SimplePrintview control to the print job.
					// For comparison, try setting the 
					// second parameter to "none".
					printJob.addObject(formPrintView);
					
					// Send the job to the printer.
					printJob.send();
					
					// Remove the print-specific control to free memory.
					removeChild(formPrintView);
				}
			}
		]]>
	</mx:Script>
	
	<!-- The form to display-->
	<mx:Form id="myForm">
		<mx:FormHeading label="Contact Information"/>
		<mx:FormItem label="Name: ">
			<mx:TextInput id="custName" 
						  width="200" 
						  text="Samuel Smith"
						  fontWeight="bold"/>
		</mx:FormItem>
		<mx:FormItem label="Phone: ">
			<mx:TextInput id="custPhone" 
						  width="200" 
						  text="617-555-1212"
						  fontWeight="bold"/>
		</mx:FormItem>
		<mx:FormItem label="Email: ">
			<mx:TextInput id="custEmail" 
						  width="200" 
						  text="sam@sam.com"
						  fontWeight="bold"/>
		</mx:FormItem>
		
		<mx:FormHeading label="Product Information"/>
		<mx:DataGrid id="myDataGrid" width="300">
			<mx:dataProvider>
				<mx:Object Product="Flash" Code="1000"/>
				<mx:Object Product="Flex" Code="2000"/>
				<mx:Object Product="ColdFusion" Code="3000"/>
				<mx:Object Product="JRun" Code="4000"/>
			</mx:dataProvider>
		</mx:DataGrid>
		<mx:Button label="PrintView" click="print(myDataGrid)"/>
		<mx:FormItem label="Label">
		</mx:FormItem>
		<mx:Button id="myButton" 
				   label="Print" 
				   click="doPrint();"/>
	</mx:Form>
	</mx:Application>

 

Component:
MyPrintView. mxml(这是一个组件)

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" 
		 backgroundColor="#FFFFFF" 
		 height="250" width="450" 
		 paddingTop="50" paddingLeft="50" paddingRight="50">
	
	<!-- The controls to print, a PrintDataGrid control. -->
	<mx:PrintDataGrid id="myDataGrid" width="100%">
		<mx:columns>
			<mx:DataGridColumn dataField="Product"/>
			<mx:DataGridColumn dataField="Code"/>
		</mx:columns>
	</mx:PrintDataGrid>
</mx:VBox>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值