flex 分页

<?xml version="1.0" encoding="utf-8"?>
<mx:ControlBar xmlns:mx="http://www.adobe.com/2006/mxml" width="100%"
  paddingTop="0" height="24">
   <mx:Script>
   <![CDATA[
    import mx.events.ValidationResultEvent;
    import mx.events.ItemClickEvent;
    import mx.rpc.AsyncToken;
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;
    import mx.controls.Alert;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import mx.rpc.http.mxml.HTTPService;
    import mx.validators.NumberValidator;
   
    [Bindable]   
    private var _pageSize: Number =  20;
       
    // 当前页
    [Bindable]
    private var _curPage: Number = 1;  

    //总记录数
    [Bindable]   
    private var _recordCount: String;
           
    // 页面总数
    private var pageCount: Number = -1;
   
    private var numValid:NumberValidator = new NumberValidator();   
    // 请求数据的服务,由调用程序设置
    private var _service: HTTPService;
    private var _params: URLVariables;
    public var resultData: Object;
     
    public function set service(service: HTTPService): void
    {
     this._service = service;
     this._service.addEventListener(ResultEvent.RESULT, onServiceResult);
     init();
    }
    
    public function get service(): HTTPService
    {
     return this._service;
    }
     
    public function set params(params: URLVariables): void
    {
     this._params = params;
     this._params.pageSize = _pageSize;
     init();
    }
    
    public function get params(): URLVariables
    {
     return this._params;
    }
     
    private function init(): void
    {
     _curPage = 1;
     pageCount = -1;
     pageNavigator.enabled = false;
    }
     
    public function set pageSize(pageSize: Number): void
    {
     this._pageSize = pageSize;
     if (params != null)
     {
      this._params.pageSize = _pageSize;
     }
    }
    
    public function get pageSize(): Number
    {
     return _pageSize;
    }
     
    private function onServiceResult(e: ResultEvent): void
    {
         recordCount.text = "共"+(e.result.nodes.recordNum)+"条";
         pageCount = Math.ceil( e.result.nodes.recordNum / _pageSize);
         totalLabel.text = "页" +"/共" + pageCount + "页";
         pageNavigator.enabled = true;
         
      var event: NavigateResultEvent = new NavigateResultEvent(NavigateResultEvent.RESULT);
         event.resultData =  XML(e.message.body).children();;
         this.dispatchEvent(event);   
    }
    
       
    private function gotoBtnClick(event:Event): void
    {
        _curPage = new Number(curPageText.text);
        if (_curPage > pageCount)
         _curPage = pageCount;
        doQuery(_curPage);
    }
       
    public function doQuery(pageNo: Number = 1): void
    {
     params.curPage = pageNo;
        service.send(params); 
        pageNavigator.enabled   = false;    
     }
       
    private function turnPage(e: ItemClickEvent): void
    {
     
       switch (e.index)
       {
         case 0:
         _curPage = 1;
         doQuery(_curPage);
         break;
         case 1:
         if (_curPage > 1)
          _curPage--;
         doQuery(_curPage);
         break;
         case 2:
         if (_curPage < pageCount)
          _curPage++;
         doQuery(_curPage);
         break;
         case 3:
         doQuery(pageCount);
         _curPage = pageCount;
         break;
         default:
         break;
       }
   }
  
   private function createValid():void
   {
       numValid.source = curPageText;
       numValid.property = "text";
       numValid.addEventListener(ValidationResultEvent.INVALID,handValid);
       numValid.addEventListener(ValidationResultEvent.VALID,handValid);
   }
  
   private function handValid(event:ValidationResultEvent):void
   {
       if(event.type == ValidationResultEvent.VALID)
       {
           turnBn.enabled = true;
       }
       if(event.type == ValidationResultEvent.INVALID)
       {
           turnBn.enabled = false;
           curPageText.errorString = "请输入数字";
       }
   }
  
   ]]>
   </mx:Script>
   <mx:HBox  width="60%" cornerRadius="12" borderStyle="solid" horizontalAlign="center" verticalAlign="middle"> 
    <mx:Label text="第"/>
    <mx:TextInput width="32" height="20" text="{_curPage}" id="curPageText" creationComplete="createValid()"/>
 <mx:Label id="totalLabel" text="/0页"/>
 <mx:Label id="recordCount" text="{_recordCount}"/>
 <mx:Button label="跳转" click="gotoBtnClick(event);" id="turnBn"/>
 <mx:Label id="pageSizeText" text="每页{_pageSize}条"/>
 <mx:LinkBar id="pageNavigator" enabled="false" width="100%" horizontalAlign="center" itemClick="turnPage(event);">
     <mx:dataProvider>
         <mx:Object id="firstPage" Tooltip="首页" label="第一页" enabled="false" icon="@Embed(source='page-first.gif')"/>
         <mx:Object id="prePage" Tooltip="上一页" label="上一页" icon="@Embed(source='page-prev.gif')"/>
         <mx:Object id="nextPage" Tooltip="下一页" label="下一页" icon="@Embed(source='page-next.gif')"/>                  
         <mx:Object id="lastPage" Tooltip="末页" label="最后一页" icon="@Embed(source='page-last.gif')"/>
     </mx:dataProvider>
 </mx:LinkBar>
   </mx:HBox> 
   
</mx:ControlBar>

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