flex分页

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
		
	<mx:Script>
		<![CDATA[
			import mx.collections.XMLListCollection;
			import mx.events.FlexEvent;
			import mx.collections.ArrayCollection;
			import mx.controls.List;
			import mx.controls.Alert;
			import mx.collections.IViewCursor;
			import mx.controls.listClasses.ListBase;
			import mx.core.UIComponent;
			import mx.collections.ICollectionView;
			[Bindable]
			protected var _dataProvider:ICollectionView;//
			private var _targetUI:ListBase;//目标listBase
			private var _perPage:int=3;//当前显示几条记录
			private var _length:int=0;//总共的记录
			[Bindable]
			private var _pageCount:int;
			[Bindable]
			private var _currentPage:int=1;//当前页
			
			private var _tempData:Array;
			
			public function set dataProvider(d:Object):void{
//				trace(d+"fuzhi");
//				if(d is ICollectionView){
//					mx.controls.Alert.show("is a IcollectionVIew");
//				}
//				if(d is XMLList){
//					Alert.show("is a xmlList ");
//				}
//				if(d is XMLListCollection){
//					Alert.show("is a xml List Collection");
//				}
				if(d is XMLList){
					this._dataProvider=new XMLListCollection(XMLList(d));
					
				}
				//this._dataProvider=ICollectionView(d);
				if(this._dataProvider){
					this.startControl();
					
				}
				
			}
			public function set targetUI(ui:ListBase):void{
				this._targetUI=ui;
			}
			public function set perPage(pc:int):void{
				this._perPage=pc;
			}
			public function startControl():void{
				this._targetUI.rowCount=this._perPage;
				this._length=this._dataProvider.length;
				this._pageCount=this._length/this._perPage+(this._length%this._perPage>0?1:0);
				
				trace("总页数"+this._pageCount+":记录数"+this._length);
			}
			private function onChangeData(event:FlexEvent):void{
				mx.controls.Alert.show("datachange");
				this.startControl();
			}
			public function gotoNext():void{
				if(!(this._currentPage+1>this._pageCount)){
					this._currentPage=this._currentPage+1;
					this.spliceArray();
				}
			}
			public function gotoProvious():void{
				if(!(this._currentPage-1<1)){
					this._currentPage=this._currentPage-1;
					this.spliceArray();
				}
			}
			public function spliceArray():void{
				//splice有可能超出范围,所以这样做
				
				
				//因为数组是从0开始,加入currentPage=1;perpage=5,
				//则下一页的数据是第6个,数组索引是5
				var startIndex:int=(this._currentPage-1)*this._perPage; 
				var count:int=startIndex+this._perPage>this._length?this._length-startIndex:this._perPage;
				
				//用数组最方便
				//this._targetUI.dataProvider=this._tempData.slice(startIndex,startIndex+count);
				
				this._targetUI.dataProvider=this.filterCollection(startIndex,startIndex+count);
				trace(startIndex+":"+count);
			}
			public function filterCollection(start:int,end:int):ICollectionView{
				var newArray:ArrayCollection=new ArrayCollection;
				for(var i:int=start;i<end;i++){
					newArray.addItem(XML(this._dataProvider[i]).copy());
					
				}
			
				
				return newArray;
			}
			
			
			public function gotoPage():void{
				if(int(this.pageno.text)<=this._pageCount){
					this._currentPage=int(this.pageno.text)
					this.spliceArray();
				}
			}
			//页数
			public function get pageArray():Array{
				var array:Array=new Array;
				for(var i:int=1;i<=this._pageCount;i++){
					array.push(i);
				}
				return array;
			}
		]]>
	</mx:Script>	
	<mx:HBox>
		<mx:LinkButton label="上一页" click="this.gotoProvious()"/>
		<mx:LinkButton label="下一页" click="this.gotoNext()"/>
		<mx:Label text="当前{this._currentPage}页"/>
		<mx:Label text="共{this._pageCount}页"/>
	</mx:HBox>
	<mx:HBox>
		<mx:TextInput id="pageno">
		
		</mx:TextInput>
		<mx:Button label="go" click="gotoPage()">
		
		</mx:Button>
	</mx:HBox>
	
</mx:VBox>
 

你可能感兴趣的:(xml,UI,Flex,Adobe,Go)