1、源码主要如下:
<?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"
width="100%" height="100%" creationComplete="init();">
<!-- datagrid控制列表-->
<s:layout>
<s:HorizontalLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<!--
页面功能:分页控件,控制datagrid的显示。
-->
<fx:Script>
<![CDATA[
import com.surekam.WHJYFW.MC101000001.MC201000001.MC301000404.VO.CacheVO;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.DataGrid;
import mx.controls.Text;
import spark.components.CheckBox;
private var _currPage:int = 0;//当前页数
private var _numPerPage:int = 10;//每页显示的页数
private var _totalPage:int = 0;//数据源最大的页数
private var _totalRecords:int = 0;//数据源的数量
private var _dataProvider:Array = null;//控件的数据源
private var currDS:ArrayCollection = null;//当前的数据集合
private var scanPage:Array = new Array();//浏览过的页数
private var _targetUI:Object = null;//使用数据源的控件
private var _cacheMAX:int = 4;//缓存的页面的数目
private var _callBackFun:Function = null;//回调函数
[Bindable]
private var dropSource:ArrayCollection ;
/*
初始化页面完毕
*/
private function init():void{
this.addEventListener(KeyboardEvent.KEY_UP,handlerEnter);
dropSource = new ArrayCollection([{label:10},{label:20},{label:30},{label:40},{label:50}]);
displayNumber.selectedIndex = 0;
// Alert.show(_currPage+" ");
}
/*
处理键盘事件
*/
private function handlerEnter(event:KeyboardEvent):void{
if(13 == event.keyCode){
gotoIndex();
}
}
/*
如果改变了显示数量的话,重新加载数据源,同时也要更新缓冲的数据源
*/
private function changeHandler(event:Event):void{
var amount:int = parseInt((event.target as ComboBox).selectedItem.label);
numPerPag = amount;
/*
重新计算分页的结果
*/
var mod:int = _totalRecords % getNumPerPag;
var divid:int = _totalRecords / getNumPerPag;
totalPage = mod>0?divid+1:divid;
cleanCache();
// if(_currPage > totalPage){
//
// }
currPage =1;
}
/*
清除缓冲的数据源
*/
private function cleanCache():void{
while(scanPage.pop()!=null){
}
}
/*
设置数据源的目标
*/
public function set TargetUI(object:Object):void{
this._targetUI = object;
}
/*
当前页数
*/
public function set currPage(page:int):void{
this._currPage = page;
this.btnNext.enabled = true;
if(page<=1){
this.btnPrevious.enabled = false;
}else{
this.btnPrevious.enabled = true;
}
if(page>=_totalPage){
this.btnNext.enabled =false;
}
btnGo.enabled = true;
displayData();
displayInfo();
}
/*
*/
private function displayInfo():void{
this.txtPageIndex.text = _currPage.toString();
lblInfo.text = "第"+_currPage+"页/共"+_totalPage+"页";
}
/*
最大的页数
*/
public function set totalPage(page:int):void{
this._totalPage = page;
}
/*
每页最大显示数量
*/
public function set numPerPag(num:int):void{
this._numPerPage = num;
//(_targetUI as DataGrid).rowCount = num;
}
/*
每页的数量
*/
public function get getNumPerPag():int{
return this._numPerPage;
}
/*
*/
public function set totalRecords(record:int):void{
this._totalRecords = record;
}
/*
缓存的最大的数量
*/
public function set cacheMAX(max:int):void{
if(max<4){
return ;
}else{
_cacheMAX = max;
}
}
/*
回调函数
*/
public function set callBackFun(callBack:Function):void{
this._callBackFun = callBack;
}
/*
控件的数据源
*/
public function set dataProvider(data:Array):void{
this._dataProvider = data;
}
private function goto(num:int):void{
if(-1 == num){
currPage = _currPage-1;
}else if(1 == num){
currPage = _currPage+1;
}
}
private function gotoIndex():void{
var index:int = parseInt(txtPageIndex.text);
if(index>=1 && index <= _totalPage){
currPage = index;
}else{
//抛出错误
throw new Error("输入不正确");
}
}
/*
更新缓冲区的数据源
*/
private function refreshData(data:Array):void{
var cacheVO:CacheVO = new CacheVO(_currPage,data);
if(scanPage.length < _cacheMAX){
scanPage.push(cacheVO);
}else{
var temp:Array = scanPage.reverse();
temp.pop();
temp.push(cacheVO);
temp.reverse();
scanPage = temp;
}
}
/*
查找哪页的数据是否在缓冲区中
*/
private function findCache(page:int):CacheVO{
var len:int = scanPage.length;
var cache:CacheVO = null;
for(var i:int =0;i<len;i++){
cache = scanPage.pop();
if(page == cache.pageSeq){
return cache;
}
}
return null;
}
public function displayData():void{
if(this._dataProvider == null){
throw new Error("数据源没设定");
}
// else{
// var sourceVO:CacheVO = findCache(_currPage);
// if(null == sourceVO){
// //调用回调函数调用服务(起始序列,结束序列)
// _callBackFun((_currPage-1)*_numPerPage, _currPage*_numPerPage);
// refreshData(_dataProvider);
// (_targetUI as DataGrid).dataProvider = _dataProvider;
// }else{
// //更新datagrid的数据源
// (_targetUI as DataGrid).dataProvider = sourceVO.datasource;
// }
//
// }
}
]]>
</fx:Script>
<mx:Button label="上一页" id="btnPrevious" click="goto(-1);" enabled="false" width="54"/>
<mx:Button label="下一页" id="btnNext" click="goto(1);" enabled="false" width="54"/>
<mx:Label text="转到"/>
<mx:TextInput width="48" id="txtPageIndex" restrict="0-9" text=""/>
<mx:Label text="页"/>
<mx:Button label="确定" id="btnGo" width="54" click="gotoIndex();" enabled="false"/>
<mx:Label text="显示数量:"/>
<s:ComboBox id="displayNumber" dataProvider="{dropSource}"
change="changeHandler(event);"
selectedIndex="1" width="49"/>
<mx:Label id="lblInfo" text=""/>
</s:Group>