自己造的flex的datagrid分页

首先是java的dao代码:
	@Override
	public List getMailList(Mail mail,final int currentIndex,final int pageSize) {
		log.debug("get Mail List");
		final String hql="FROM Mail as mail WHERE mail.touser='"+mail.getTouser()+"' order by mail.senddate desc";
		try{
			return getHibernateTemplate().executeFind(new HibernateCallback(){
				@Override
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query=session.createQuery(hql);
					query.setMaxResults(pageSize);
					query.setFirstResult(currentIndex);
					List l=query.list();
					return l;
				}
				
			});
		}catch(RuntimeException e){
			log.error("get fail!");
			throw e;
		}


flex代码:
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
		 xmlns:s="library://ns.adobe.com/flex/spark" 
		 xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="group1_creationCompleteHandler(event)">
	<s:layout>
		<s:BasicLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.managers.PopUpManager;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.remoting.RemoteObject;
			
			import spark.events.TextOperationEvent;
			[Bindable]
			private  var pageSize:int=new int();;
			[Bindable]
			private var resultCount:int=new int();;
			[Bindable]
			private var allowResults:int=5;
			[Bindable]
			private  var currentPage:int=1;
			[Bindable]
			private var recMailXml:XML=<SENDMAIL label="全部"><THREEDAYS label="近三天邮件"></THREEDAYS><SEVENDAYS label="近一周邮件"></SEVENDAYS><THRITYDAYS label="近一个月邮件"></THRITYDAYS></SENDMAIL>;
			[Bindable]
			public static var imgUrl:String="images/png/128/mail_receive.png";
			
			protected function curPageIndex_changeHandler(event:TextOperationEvent):void
			{
				// 跳转到指定页
				var rePageTemp:int=int(curPageIndex.text);
				if(rePageTemp!=currentPage){
				
					if(rePageTemp>0 && rePageTemp<=pageSize){
						var re:RemoteObject=new RemoteObject("mailService");
						re.getMailList(index.loginObj.name,rePageTemp,allowResults);
						re.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
							mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
						});
						currentPage=rePageTemp;
						
					}else{
						Alert.show("输入的页数不存在!");
					}
				}
			}
			protected function firstPage_clickHandler(event:MouseEvent):void
			{
				// 第一页
				if(currentPage>1){
					var re:RemoteObject=new RemoteObject("mailService");
					re.getMailList(index.loginObj.name,1,allowResults);
					re.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
						mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
						
					});
					currentPage=1;
				}
			}
			protected function prePage_clickHandler(event:MouseEvent):void
			{
				// 上一页
				if(currentPage>1){
					var re:RemoteObject=new RemoteObject("mailService");
					re.getMailList(index.loginObj.name,(currentPage-1),allowResults);
					re.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
						mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
						
					});
					currentPage=currentPage-1;
				}
			}
			protected function nextPage_clickHandler(event:MouseEvent):void
			{
				// 下一页
				if(currentPage<pageSize){
					var re:RemoteObject=new RemoteObject("mailService");
					re.getMailList(index.loginObj.name,(currentPage+1),allowResults);
					re.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
						mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
					
					});
					currentPage=currentPage+1;
				}
			}
			protected function lastPage_clickHandler(event:MouseEvent):void
			{
				// 最后一页
				if(currentPage<pageSize){
					var r:RemoteObject
					var re:RemoteObject=new RemoteObject("mailService");
					re.getMailList(index.loginObj.name,pageSize,allowResults);
					re.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
						mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
						
					});
					currentPage=pageSize;
				}
			}
			protected function group1_creationCompleteHandler(event:FlexEvent):void
			{
				imgMailState.source=imgUrl;
			}
			protected function tree1_clickHandler(event:MouseEvent):void
			{
		
				setCurrentState("MailList");
				var mailRemoteObj:RemoteObject=new RemoteObject("mailService");
				mailRemoteObj.getMailList(index.loginObj.name,1,5);
				mailRemoteObj.addEventListener(ResultEvent.RESULT,function(resultEvent:ResultEvent):void{
					mailListGrid.dataProvider=ArrayCollection(resultEvent.result);
				});
				currentPage=1;
				var re:RemoteObject=new RemoteObject("mailService");
				re.getMailListCount(index.loginObj.name);
				re.addEventListener(ResultEvent.RESULT,function(resultEvent2:ResultEvent):void{
					resultCount=int(resultEvent2.result);
					var count:int=(resultCount/allowResults)+1;
					pageSize=count;
				});
			}
			protected function mailListGrid_clickHandler(event:MouseEvent):void
			{
				var mailInf:mailInfo=new mailInfo();
				mailInf.mailObject=event.currentTarget.selectedItem;
				PopUpManager.addPopUp(mailInf,this,true);
				PopUpManager.centerPopUp(mailInf);
				var re:RemoteObject=new RemoteObject("mailService");
				re.readMail(mailInf.mailObject.id);
			}

		]]>
	</fx:Script>
	<s:states>
		<s:State name="State1"/>
		<s:State name="MailList"/>
	</s:states>
	<fx:Declarations>
	</fx:Declarations>
	
	<s:HGroup width="98%" height="98%" verticalAlign="middle" textAlign="left" horizontalCenter="0" verticalCenter="0">
		<mx:Tree  height="95%" dataProvider="{recMailXml}" labelField="@label" width="180" borderVisible="false" click="tree1_clickHandler(event)"></mx:Tree>
		<mx:VRule height="95%"/>
		<s:Group width="100%" height="95%">
			<s:VGroup horizontalAlign="right" includeIn="MailList">
				<mx:DataGrid id="mailListGrid" width="450" verticalAlign="middle" textAlign="center"  height="158" click="mailListGrid_clickHandler(event)">
					<mx:columns>
						<mx:DataGridColumn headerText="发送时间" dataField="senddate"/>
						<mx:DataGridColumn headerText="发件人" dataField="sender"/>
						<mx:DataGridColumn headerText="标题" dataField="title"/>
						<mx:DataGridColumn headerText="状态" dataField="readState"/>
					</mx:columns>
				</mx:DataGrid>
				<s:HGroup verticalAlign="middle">
					<s:Label text="共"/>
					<s:Label text="{resultCount}" color="#FC0303"/>
					<s:Label text="条记录,当前第"/>
					<s:Label text="{currentPage}" color="#FC0303"/>
					<s:Label text="页,共"/>
					<s:Label text="{pageSize}" color="#FC0303"/>
					<s:Label text="页。跳转到第"/>
					<s:TextInput id="curPageIndex" width="30" height="15" change="curPageIndex_changeHandler(event)" enabled="true" text="{currentPage}" paddingLeft="0" paddingTop="0" paddingBottom="0" textAlign="center" paddingRight="0"/>
					<s:Label text="页。"/>
					<mx:Image id="firstPage" source="images/png/16/arrow_stop_180.png" scaleContent="false" width="16" height="16" click="firstPage_clickHandler(event)" buttonMode="true"/>
					<mx:Image id="prePage" source="images/png/16/arrow_skip_180.png" scaleContent="false" width="16" height="16" click="prePage_clickHandler(event)" buttonMode="true"/>
					<mx:Image id="nextPage" source="images/png/16/arrow_skip.png" scaleContent="false" width="16" height="16" click="nextPage_clickHandler(event)" buttonMode="true"/>
					<mx:Image id="lastPage" source="images/png/16/arrow_stop.png" width="16" height="16" click="lastPage_clickHandler(event)" buttonMode="true"/>
				</s:HGroup>
			</s:VGroup>
			<mx:Image id="imgMailState" source="{imgUrl}" horizontalCenter="0" verticalCenter="0" width="128" height="128" includeIn="State1"/>
		</s:Group>
	</s:HGroup>
	
	
</s:Group>


自己造的flex的datagrid分页

你可能感兴趣的:(DAO,xml,Flex)