`
darrenzhu
  • 浏览: 804272 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Flex服务端分页

阅读更多
Structure:
model
    event
        PaginationEvent.as
    vo
        IPageable.as
        PaginatedResults.as
view
    paginationBarImg(folder)
    PaginationBar.mxml
    PaginationImgBar.mxml


Souce code:
package model.event
{
	import flash.events.Event;
	
	public class PaginationEvent extends Event
	{
		public static var PAGE_CHANGED:String="pageChanged";
		public static var DATA_SORTED:String="dataSorted";
		public static var DATA_FILTERED:String="dataFiltered";
		
		public var searchCriteria:Object;
		public var pageNumber:int;
		public var recordsPerPage:int;
		public var sortable:Boolean = false;
		public var sortField:String = "";
		public var isAscendSort:Boolean = true;
		public var filterField:String = "";
		public var filterValue:String = "";
		
		public function PaginationEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false){
			super(type, bubbles, cancelable);
		}
		
		/**
		 * Please override the clone method if the event need to be redispatched, because
		 * when redispatching an event, a clone event will be created automatically using this method.
		 */ 
		override public function clone():Event {
			var e:PaginationEvent = new PaginationEvent(type, bubbles, cancelable);
			e.searchCriteria=searchCriteria;
			e.pageNumber = pageNumber;
			e.recordsPerPage = recordsPerPage;
			e.sortable = sortable;
			e.sortField = sortField;
			e.isAscendSort= isAscendSort;
			e.filterField = filterField;
			e.filterValue = filterValue;
			return e;
		}
		
	}

}


package model.vo
{
	public interface IPageable
	{
		function toFirstPage():void;
		function toLastPage():void;
		function toNextPage():void;
		function toPreviousPage():void;
		function toSelectedPage(selectedPage:int):void;
		function get totalPages():int;
		function get totalRecords():int;
	}
}


package model.vo
{
	import model.event.PaginationEvent;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	/**
	 * Service side will return a new PaginatedResults object for each search with only setting the value of totalPages and resultSet, 
	 * so we need to reset the value of selectedPage, recordsPerPage and searchCriteria at flex side.</br>
	 * 
	 * <p>There are two classes for implementing pagination: PaginationBar.mxml and PaginatedResults, and usually you need to make two calls:</br>
	 * 1) make the initial call to set the results of the first page to your target.</br>
	 * 2) make the second call to set the results of the requested page to your target.</br>
	 * 
	 * <p>Please ensure to invoke method:resetToolBar after invoking the setter methods: selectedPage and recordsPage, 
	 * otherwise the tool bar may not update to proper status, currently resetToolBar is invoked inside PaginationBar, and you only need to remember
	 * to reset the value of other 3 required parameters mentioned above.
	 */ 
	public class PaginatedResults extends EventDispatcher implements IPageable
	{
		public static const DEFAULT_PAGE_NUMBER:int=1;
		public static const DEFAULT_PAGE_SIZE:int=10;
		public var alertCapturedErrorInfo:Boolean=false;
		
		[Transient]
		private var _searchCriteria:Object;
		
		[Transient]
		private var _selectedPage:int;
		[Transient]
		private var _totalRecords:int;
		
		private var _totalPages:int;
		
		[Transient]
		private var _recordsPerPage:int;//same meaning as pageSize
		
		[Bindable]
		[Transient]
		public var recordsPerPageComboBoxSeletedIndex:int;
		
		[Bindable]
		[Transient]
		public var selectedPageComboBoxSeletedIndex:int;
		
		[Transient]
		[Bindable]
		public var availablePageRange:ArrayCollection=new ArrayCollection();
		
		[Transient]
		[Bindable]
		public var availabeRecordsPerPageRange:ArrayCollection=new ArrayCollection([10,20,30,40,50,60,80,100,150,200,500]);
		
		public var resultSet:Object;
		
		[Bindable] public var enableToFirstPage:Boolean=false;
		[Bindable] public var enableToLastPage:Boolean=false;
		[Bindable] public var enableToNextPage:Boolean=false;
		[Bindable] public var enableToPreviousPage:Boolean=false;
		[Bindable] public var enableRecordsPerPageRange:Boolean=false;
		[Bindable] public var enableAvailablePageRange:Boolean=false;
		[Bindable] public var enableRefresh:Boolean=false;
		
		[Bindable]public var pageNumberStatus:String="0/0";
		
		
		public function PaginatedResults(recordsType:String=null){
			this._selectedPage=DEFAULT_PAGE_NUMBER;
			this._recordsPerPage=DEFAULT_PAGE_SIZE;
		}
		
		/**
		 * This function should be called after reset the value of selectedPage and recordsPerPage,
		 * Otherwise, PaginationBar may not update to the right status.
		 */ 
		public function resetToolBar():void{
			resetSelectedPageCombBoxSeletedIndex();
			resetRecordsPerPageComboBoxSeletedIndex();
			resetNavigationButton();
		}
		
		private function resetAvailablePageRange():void{
			if(this.availablePageRange==null)
				this.availablePageRange=new ArrayCollection();
			if(this._totalPages>0){
				this.availablePageRange.removeAll();
				for(var i:int=0;i<this._totalPages;i++){
					this.availablePageRange.addItem(i+1);
				}
			}
		}
		
		private function resetNavigationButton():void{
			if(this.totalPages>0){
				this.enableToFirstPage=true;
				this.enableToLastPage=true;
				this.enableAvailablePageRange=true;
				this.enableRecordsPerPageRange=true;
				this.enableRefresh=true;
			}else{
				this.enableToFirstPage=false;
				this.enableToLastPage=false;
				this.enableAvailablePageRange=false;
				this.enableRecordsPerPageRange=false;
				this.enableRefresh=false;
			}
			
			if(this._selectedPage==this.totalPages)
				this.enableToLastPage=false;
			if(this._selectedPage==1)
				this.enableToFirstPage=false;
			
			if(this._selectedPage<this.totalPages){
				this.enableToNextPage=true;
			}else{
				this.enableToNextPage=false;
			}
				
			if(this._selectedPage>=2){
				this.enableToPreviousPage=true;
			}else{
				this.enableToPreviousPage=false;
			}				
		}
				
		private function updatePageNumberStatus():void{
			this.pageNumberStatus=this._selectedPage+"/"+this._totalPages;
		}
		
		public function refresh():void{
			dispatchPaginationEvent();
		}
		
		public function toNextPage():void
		{
			if(this._selectedPage==this._totalPages){
				toLastPage();
			}else{
				toSelectedPage(this._selectedPage+1);
			}
		}
		
		public function toPreviousPage():void
		{
			if(this._selectedPage==1){
				toFirstPage();
			}else{
				toSelectedPage(this._selectedPage-1);
			}
			
		}
		
		public function toFirstPage():void
		{
			toSelectedPage(1);
			
		}
		
		public function toLastPage():void
		{
			toSelectedPage(this._totalPages);
			
		}
		
		public function toSelectedPage(selectedPage:int):void{
			this._selectedPage=selectedPage;
			dispatchPaginationEvent();
		}
		
		/**
		 * Please set value to recordsPerPage manually, because server don't return recordsPerPage to flex though flex will send recordsPerPage to server.
		 */ 
		public function set recordsPerPage(recordsPerPage:int):void{
			this._recordsPerPage=recordsPerPage;
		}
		
		/**
		 * each time server will return us a new PaginatedResults object, which will result in the default index to be set in PaginationBar's UIComponent,
		 * so we need to manually update UIComponent's selectedIndex to the proper value.
		 */ 
		private function resetRecordsPerPageComboBoxSeletedIndex():void{
			var index:int=this.availabeRecordsPerPageRange.getItemIndex(this._recordsPerPage);
			if(index!=-1){
				this.recordsPerPageComboBoxSeletedIndex=index;
			}else{
				this.recordsPerPageComboBoxSeletedIndex=0;
			}
		}
		
		private function resetSelectedPageCombBoxSeletedIndex():void{
			var index:int=this.availablePageRange.getItemIndex(this._selectedPage);
			if(index!=-1){
				this.selectedPageComboBoxSeletedIndex=index;
			}else{
				this.selectedPageComboBoxSeletedIndex=0;
			}
		}
		
		public function get recordsPerPage():int{
			return this._recordsPerPage;
		}
		
		/**
		 * Change UIComponent directly, no new PaginatedResults object has been set to PaginatinoBar, so don't need to manually update the selectedIndex.
		 */ 
		public function resetRecordsPerPage(recordsPerPage:int):void{
			this._recordsPerPage=recordsPerPage;//Don't use setter method:this.recordsPerPage, otherwise may cause infinite loop value set
			this._selectedPage=1;
			dispatchPaginationEvent();	
		}
		
		private function dispatchPaginationEvent():void{
			try{
				if(this._searchCriteria!=null){
					var newEvent:PaginationEvent=new PaginationEvent(PaginationEvent.PAGE_CHANGED);
					newEvent.pageNumber=this._selectedPage;
					newEvent.recordsPerPage=this._recordsPerPage;
					newEvent.searchCriteria=this._searchCriteria;
					this.dispatchEvent(newEvent);
				}else{
					Alert.show("SearchCriteria is null.","Message");
				}
			}catch(error:Error){
				if(alertCapturedErrorInfo){
					Alert.show("PaginatedResults->dispatchPaginationEvent error:"+error.message,"Error");
				}
			}
		}
		
		[Bindable]
		public function get totalPages():int
		{
			return this._totalPages;
		}
		
		public function set totalPages(totalPages:int):void
		{
			this._totalPages=totalPages;
			resetAvailablePageRange();
			updatePageNumberStatus();
		}
		
		[Bindable]
		public function get totalRecords():int
		{
			return this._totalRecords;
		}
		
		public function set totalRecords(totalRecords:int):void
		{
			this._totalRecords=totalRecords;
		}
		
		[Bindable]
		public function get selectedPage():int
		{
			return this._selectedPage;
		}
		
		/**
		 * Please set value to selectedPage manually, because server don't return selectedPage to flex though flex will send selectedPage to server.
		 */ 
		public function set selectedPage(selectedPage:int):void
		{
			this._selectedPage=selectedPage;
			updatePageNumberStatus();
		}
		
		/**
		 * Please set value to searchCriteria manually, because server don't return searchCriteria to flex though flex will send searchCriteria to server.
		 */ 
		public function set searchCriteria(searchCriteria:Object):void{
			this._searchCriteria=searchCriteria;
		}
		
		public function get searchCriteria():Object{
			return this._searchCriteria;
		}
		
		override public function toString():String{
			return "PaginatedResults["
				+"selectedPage:"+this._selectedPage
				+",recordsPerPage:"+this._recordsPerPage
				+",totalPages:"+this._totalPages
				+",totalRecords:"+this._totalRecords
				+",searchCriteria:"+this._searchCriteria
				+"]"
				+"\n"+super.toString();
		}
	}
}


PaginationBar.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" verticalAlign="middle" horizontalAlign="left" initialize="init()">
	<mx:Metadata>
		[Event(name="pageChanged", type="model.event.PaginationEvent")]
		[Event(name="dataSorted", type="model.event.PaginationEvent")]
		[Event(name="dataFiltered", type="model.event.PaginationEvent")]
	</mx:Metadata>
	<mx:Script>
		<![CDATA[
			import model.event.PaginationEvent;
			import model.vo.PaginatedResults;
			
			import mx.controls.Alert;
			
			[Bindable]
			private var _paginatedResults:PaginatedResults;

			protected function init():void{
				_paginatedResults=new PaginatedResults();
			}
			
			public function set paginatedResults(value:PaginatedResults):void{
				if(value!=null){
					this._paginatedResults=value;
					this._paginatedResults.resetToolBar();				
					registerEventListener(this._paginatedResults);
				}
			}
					
			public function get paginatedResults():PaginatedResults{
				return this._paginatedResults;
			}
			
			private function registerEventListener(obj:PaginatedResults):void{
				if(!obj.hasEventListener(PaginationEvent.PAGE_CHANGED)){
					obj.addEventListener(PaginationEvent.PAGE_CHANGED,propagateEvent);
				}
				if(!obj.hasEventListener(PaginationEvent.DATA_FILTERED)){
					obj.addEventListener(PaginationEvent.DATA_FILTERED,propagateEvent);
				}
				if(!obj.hasEventListener(PaginationEvent.DATA_SORTED)){
					obj.addEventListener(PaginationEvent.DATA_SORTED,propagateEvent);
				}			
			}
			
			private function propagateEvent(e:PaginationEvent):void{
				//a clone event will be created automatically by using the clone method of class PaginationEvent
				this.dispatchEvent(e);
			}
			
		]]>
	</mx:Script>
	<mx:HBox paddingBottom="2">
		<mx:LinkButton width="36" label="First" click="_paginatedResults.toFirstPage()"
					   enabled="{_paginatedResults.enableToFirstPage}" styleName="PaginationLinkButton"/>
		<!--<mx:Image source="Embed(source='assets/customImg/pagingImg/page-first.gif')"/>-->
		<mx:LinkButton width="65" label="Previous" click="_paginatedResults.toPreviousPage()"
					   enabled="{_paginatedResults.enableToPreviousPage}"
					   styleName="PaginationLinkButton"/>
		<mx:LinkButton width="37" label="Next" click="_paginatedResults.toNextPage()"
					   enabled="{_paginatedResults.enableToNextPage}" styleName="PaginationLinkButton"/>
		<mx:LinkButton width="38" label="Last" click="_paginatedResults.toLastPage()"
					   enabled="{_paginatedResults.enableToLastPage}" styleName="PaginationLinkButton"/>
	</mx:HBox>
	<mx:Spacer width="5"/>
	<mx:Label text="Go to Page:" styleName="PaginationText"/>
	<mx:ComboBox id="cmbSelectedPage" height="20" dataProvider="{_paginatedResults.availablePageRange}" width="65" 
				 selectedIndex="{_paginatedResults.selectedPageComboBoxSeletedIndex}"
				 enabled="{_paginatedResults.enableAvailablePageRange}" change="_paginatedResults.toSelectedPage(int(cmbSelectedPage.text))"/>
<!--	<mx:LinkButton label="Go" click="_paginatedResults.toSelectedPage(int(cmbSelectedPage.text))" 
				   styleName="PaginationLinkButton" enabled="{_paginatedResults.enableAvailablePageRange}"/>
	<mx:Spacer width="10"/>-->
	<!--<mx:LinkButton label="Refresh" click="_paginatedResults.refresh()" styleName="PaginationLinkButton" enabled="{_paginatedResults.enableRefresh}"/>-->
	<mx:Spacer width="5"/>
	<mx:HBox verticalAlign="middle">
		<mx:Label text="CurrentPage/TotalPages:" styleName="PaginationText"/>
		<mx:Label text="{_paginatedResults.pageNumberStatus}" styleName="PaginationText" id="txtCurrentPage"/>
<!--		<mx:Label text="TotalPages:" styleName="PaginationText"/>
		<mx:Label text="{_paginatedResults.totalPages}" styleName="PaginationText" id="txtTotalPages"/>-->
	</mx:HBox>
	<mx:Spacer width="5"/>
	<mx:Label text="Records Per Page:" styleName="PaginationText"/>
	<mx:ComboBox id="cmbRecordsPerPage" width="65" height="20"
				 dataProvider="{_paginatedResults.availabeRecordsPerPageRange}" 
				 selectedIndex="{_paginatedResults.recordsPerPageComboBoxSeletedIndex}"
				 enabled="{_paginatedResults.enableRecordsPerPageRange}" change="_paginatedResults.resetRecordsPerPage(int(cmbRecordsPerPage.text))"/>
<!--	<mx:LinkButton label="Apply" click="_paginatedResults.resetRecordsPerPage(int(cmbRecordsPerPage.text))" 
				   styleName="PaginationLinkButton" enabled="{_paginatedResults.enableRecordsPerPageRange}"/>-->
</mx:HBox>



PaginationImgBar
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
		 width="100%" verticalAlign="middle">
	<mx:Script>
		<![CDATA[
			[Embed(source="paginationBarImg/arrow_refresh.png")]
			[Bindable] public var arrowRefresh:Class;
			
			[Embed(source="paginationBarImg/arrow_left.png")]
			[Bindable] public var arrowLeftIcon:Class;
			
			[Embed(source="paginationBarImg/arrow_right.png")]
			[Bindable] public var arrowRightIcon:Class;
			
			[Embed(source="paginationBarImg/arrow_double_left.png")]
			[Bindable] public var arrowDoubleLeftIcon:Class;
			
			[Embed(source="paginationBarImg/arrow_double_right.png")]
			[Bindable] public var arrowDoubleRightIcon:Class;
		]]>
	</mx:Script>
	
	<mx:Image source="{arrowDoubleLeftIcon}"/>
	<mx:Image source="{arrowLeftIcon}"/>
	<mx:TextInput id="pageInput" width="30" height="20"/>
	<mx:Label text="TotalPages:"/>
	<mx:Image source="{arrowRightIcon}"/>
	<mx:Image source="{arrowDoubleRightIcon}"/>
	<mx:Image source="{arrowRefresh}"/>
</mx:HBox>
分享到:
评论

相关推荐

    flex客户端和服务端分页控件,后台java

    flex客户端和服务端分页控件,后台使用java实现数据的传送,flex客户端调用服务端的webservices服务进行数据的展现~~ 内附myeclipse项目和flex项目·~ 分别导入可运行,注意端口的修改~

    使用Flex开发DataGrid分页控件应用支持客户端及服务端

    对于服务端分页,需要在事件处理函数中构建HTTPService或WebService请求,将分页参数传递给服务器,然后处理返回的XML或JSON数据,更新DataGrid的dataProvider。 总之,使用Flex开发DataGrid分页控件,无论是客户端...

    flex 分页

    "Flex分页"指的是使用Adobe Flex框架实现数据的分页显示。Flex是一个开源的、基于ActionScript的开发框架,用于构建富互联网应用(RIA)。在这个场景下,我们将深入探讨Flex中的分页实现以及相关知识点。 首先,...

    FlexPagingBar Flex分页控件

    Paging 是Flex做的客户端分页控件 PagingService是Java做的WebService服务端,用来模拟服务端返回数据。 服务端需要部署到类似tomcat这样的服务器中运行 服务端返回XML格式的数据,主要用了XStream这个jar, 它...

    Flex通信-Java服务端通信实例

    Flex通信-Java服务端通信实例主要探讨的是在Web开发中,如何使用Adobe Flex与Java后端进行交互。Flex是一款强大的富互联网应用程序(RIA)开发工具,它可以创建动态、交互式的用户界面,而Java则通常作为服务器端的...

    flex 端实现分页的两种方法

    服务端收到请求后,处理分页逻辑,返回指定页的数据。这种方式减少了客户端的内存负担,但增加了服务器的处理压力。 具体到SPageController.mxml文件,这可能是一个Flex应用程序中的控制器,负责处理分页逻辑。在...

    上一篇的flex的远程对象调用,flex和spring集成分页的jar和截图

    3. 定义服务接口:在Spring服务端,定义接口并实现分页查询的方法。 4. 调用服务:在Flex客户端,通过RemoteObject调用Spring服务的方法,传递分页参数(如页码和每页大小)。 5. 处理返回结果:Spring服务执行查询...

    flex基于datagrid控件的增删改查及分页实现

    这更适用于大数据量的情况,因为客户端只需处理少量数据,但需要服务端支持分页查询。 五、自定义CellRenderer和HeaderRenderer 为了实现更复杂的界面效果,如格式化显示、下拉选择等,可以自定义CellRenderer。...

    flex2.0创建可编辑及分页.txt

    1. **数据获取**:前端使用Flex中的`RemoteObject`组件与后端进行通信,通过`getGridData()`方法从Java服务端获取数据,数据以JSON格式返回。 2. **数据展示**:前端接收到数据后,使用`ArrayCollection`类存储这些...

    Flex+spring+mybatis架构搭建

    这种架构结合了富客户端(Flex)、服务端控制(Spring)以及持久层处理(Mybatis),实现了前端用户体验的提升与后端业务逻辑的分离。 Flex是一种基于Adobe Flash Player或Adobe AIR运行时的开源框架,用于创建交互...

    将Flex DataGrid数据导出到Excel中

    在Flex客户端,接收到服务端的响应后,可以使用Flash Player的navigateToURL方法打开一个新的浏览器窗口,指向服务器返回的Excel文件地址,让用户下载。 在整个过程中,确保处理好错误情况,比如数据解析失败、...

    flex和hibernate的整合

    10. **部署与维护**:最后,部署时要考虑服务器配置,如BlazeDS/LCDS的设置,以及如何更新和维护服务端代码,而不影响正在运行的Flex应用。 以上是Flex和Hibernate整合的主要方面,实际开发中还需要结合具体项目...

    flex与数据库交互

    同时,Flex提供了ArrayCollection类,可以方便地管理和操作数据集,如排序、过滤和分页。 ```actionscript ``` 这里,`lastResult`属性包含了HTTPService的响应数据,`data`是返回数据中的具体属性,`...

    使用BlazeDS实现flex与java 整合

    1. **Remoting Service**:通过AMF协议提供远程方法调用,使得Flex客户端可以直接调用Java服务端的方法。 2. **LiveCycle Data Services**:提供了高级的数据管理功能,如数据推送、数据缓存和数据分页等。 3. **...

    Adobe_Flex_4教程

    2. 在服务端实现分页。 3. 将分页后的数据传递给Flex应用程序。 **第6章: 使用数据管理同步服务器更新** 在动态Web应用中,经常需要同步客户端和服务器端的数据更新。本章将探讨数据管理相关的概念,具体包含: 1...

    flex 与Java通讯

    1. Remote Object(RO):RO允许Flex客户端调用Java服务端的方法,如同调用本地方法一样。通过AMF,Flex可以直接序列化和反序列化Java对象,实现高效的数据交换。 2. WebService:Flex也可以通过SOAP协议与Java的...

    java开发的一个flex实例 使用DAGAGRID

    3. **调用服务端方法**:在Flex客户端,你可以像调用本地函数一样调用这些远程方法,传递参数并接收返回值。 4. **处理返回数据**:一旦服务器返回数据,你可以将这些数据绑定到DAGAGRID组件,更新UI展示。 在具体...

    ssh+flex 整合方式FABridge

    3. **Flex客户端调用**:在Flex UI中,使用FABridge调用Java服务端接口,实现数据交互。例如,可以通过AJAX发送异步请求,获取服务器数据并更新Flex界面。 4. **数据传输格式**:Flex和Java之间通常使用JSON或AMF...

    flex 学生信息管理

    7. **服务端交互**:通常,Flex应用会与后端服务器(如Java、PHP或.NET)配合工作,通过HTTP或HTTPS协议交换数据。服务器端负责处理复杂的业务逻辑和数据操作,而Flex客户端专注于展示和交互。 8. **安全性**:为了...

    java对象在前台flex的datagrid中显示

    DataGrid是Flex中用于展示数据集合的强大控件,它能够以表格的形式展示数据,支持排序、分页等功能。 标题"java对象在前台flex的datagrid中显示"指出,我们的任务是将后端Java程序创建的对象在Flex的用户界面,即...

Global site tag (gtag.js) - Google Analytics