flex 分页组件的实现

<?xml version="1.0" encoding="utf-8"?>
<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009"
   xmlns:s="library://ns.adobe.com/flex/spark"
   xmlns:mx="library://ns.adobe.com/flex/mx" height="30">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Metadata>
</fx:Metadata>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
[Bindable]
public var totalRecord:int;
[Bindable]
public var pageSize:int=15;
private var offset:int;
[Bindable]
private var pageNo:int;
[Bindable]
private var currentPage:int=1;
private function getPageNo():int {
var mod:int = totalRecord%pageSize;
if(mod == 0) {
return totalRecord/pageSize;
}
return totalRecord/pageSize+1;
}

private function firstButtonHandler(event:MouseEvent):void {
if(currentPage == 1) {
Alert.show("当前已经是首页");
return;
}
this.currentPage = 1;
offset = 0;
dispatchEvent(new DataPagesEvent(DataPagesEvent.DATA_PAGES_EVENT,offset,pageSize,true));
}

private function preButtonHandler(event:MouseEvent):void {
if(currentPage>1) {
currentPage--;
offset = (currentPage-1)*pageSize;

dispatchEvent(new DataPagesEvent(DataPagesEvent.DATA_PAGES_EVENT,offset,pageSize,true));
} else {
Alert.show("当前已经是第一页");
return;
}
}

private function nextButtonHandler(event:MouseEvent):void {
if(currentPage<getPageNo()) {
offset = (currentPage)*pageSize;
currentPage++;
dispatchEvent(new DataPagesEvent(DataPagesEvent.DATA_PAGES_EVENT,offset,pageSize,true));
} else {
Alert.show("当前已经是尾页");
return;
}
}

private function lastButtonHandler(event:MouseEvent):void {
if(currentPage < getPageNo()) {
currentPage = getPageNo();
offset = (currentPage-1)*pageSize;
dispatchEvent(new DataPagesEvent(DataPagesEvent.DATA_PAGES_EVENT,offset,pageSize,true));
} else {
Alert.show("当前已经是尾页");
return;
}
}


]]>
</fx:Script>
<s:layout>
<s:HorizontalLayout horizontalAlign="right" verticalAlign="middle" gap="2"/>
</s:layout>
<mx:LinkButton label="首页"  id="firstButton" click="firstButtonHandler(event)"/>
<mx:LinkButton label="上一页" id="preButton" click="preButtonHandler(event)"/>
<mx:LinkButton label="下一页" id="nextButton" click="nextButtonHandler(event)"/>
<mx:LinkButton label="尾页"   id="lastButton" click="lastButtonHandler(event)"/>
<s:Label id="allRecord" paddingTop="5" text="共{totalRecord}条记录   "/>
<s:Label id="currentPageNo" paddingTop="5" text=" 当前第{currentPage}页" paddingRight="6"/>
</s:BorderContainer>

你可能感兴趣的:(Flex,分页组件)