这个是在客户端分页的,如果想在服务器段分页,分页原理一样,请看源码:
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns=”*” layout=”absolute” creationComplete=”initApp()”>
<mx:Script>
<![CDATA[
import mx.events.ListEvent;
public var pageRecordes:uint = 8; //每页显示记录数
public var totalPages:uint = 0; //总页数
public var totalRows:uint = 0; //总记录数
public var currentPage:uint = 1; //当前页
public var pageStartRow:uint = 1; //开始页
public var pageEndRow:uint = 0; //结束页
[Bindable]
public var indexarr:Array = new Array();
public function initApp():void{
txt.text = “当前页 ” + currentPage;
totalRows = initDG.length; //获取总记录数
if(initDG.length > pageRecordes){
dg.dataProvider = initDG.slice(0,pageRecordes);
pPage.enabled = false;
}
//总页数
if((totalRows % pageRecordes) == 0 && totalRows > 0){
totalPages = Math.floor(totalRows / pageRecordes);
}else{
totalPages = Math.floor(totalRows / pageRecordes + 1);
}
//
if(totalRows <= pageRecordes){
this.pageStartRow = 1;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 1;
this.pageEndRow = pageRecordes;
}
if(totalPages == 1){
pPage.enabled = false;
nPage.enabled = false;
}
var i:int = 0;
while(i<totalPages){
indexarr.push(++i);
}
if(indexarr.length > 0){
sPage.selectedItem = 1;
}
}
public function showPreviousPage():void{
if(currentPage > 1){
currentPage = currentPage – 1;
}
txt.text = “当前页 ” + currentPage;
if(currentPage == 1){
pPage.enabled = false;
nPage.enabled = true;
}else{
pPage.enabled = true;
nPage.enabled = true;
}
if (currentPage == totalPages) {
pageStartRow = (currentPage – 1) * pageRecordes + 1;
pageEndRow = totalRows;
} else {
pageStartRow = (currentPage – 1) * pageRecordes + 1;
pageEndRow = currentPage * pageRecordes;
}
sPage.selectedItem = currentPage;
dg.dataProvider = initDG.slice(pageStartRow – 1,pageEndRow);
}
public function showNextPage():void{
if(currentPage < totalPages){
currentPage = currentPage + 1;
}
txt.text = “当前页 ” + currentPage;
if(currentPage == totalPages){
nPage.enabled = false;
pPage.enabled = true;
}else{
nPage.enabled = true;
pPage.enabled = true;
}
if (currentPage == totalPages) {
pageStartRow = (currentPage – 1) * pageRecordes + 1;
pageEndRow = totalRows;
} else {
pageStartRow = (currentPage – 1) * pageRecordes + 1;
pageEndRow = currentPage * pageRecordes;
}
sPage.selectedItem = currentPage;
dg.dataProvider = initDG.slice((currentPage – 1) * pageRecordes,pageEndRow);
}
public function showFirstPage():void{
dg.dataProvider = initDG.slice(0,pageRecordes);
pPage.enabled = false;
nPage.enabled = true;
txt.text = “当前页 ” + 1;
currentPage = 1;
sPage.selectedItem = currentPage;
}
public function showLastPage():void{
dg.dataProvider = initDG.slice((totalPages – 1) * pageRecordes,totalRows);
pPage.enabled = true;
nPage.enabled = false;
txt.text = “当前页 ” + totalPages;
currentPage = totalPages;
sPage.selectedItem = currentPage;
}
protected function combobox1_changeHandler(event:ListEvent):void
{
currentPage = event.target.selectedItem;
if(currentPage == 1){
dg.dataProvider = initDG.slice(0,pageRecordes);
pPage.enabled = false;
nPage.enabled = true;
txt.text = “当前页 ” + 1;
}else if(currentPage == totalPages){
dg.dataProvider = initDG.slice((totalPages – 1) * pageRecordes,totalRows);
pPage.enabled = true;
nPage.enabled = false;
txt.text = “当前页 ” + totalPages;
}else{
pageStartRow = (currentPage – 1) * pageRecordes + 1;
pageEndRow = currentPage * pageRecordes;
dg.dataProvider = initDG.slice((currentPage – 1) * pageRecordes,pageEndRow);
txt.text = “当前页 ” + currentPage;
}
}
]]>
</mx:Script>
<mx:Panel title=”DataGrid Panel” height=”287″ width=”508″>
<mx:DataGrid id=”dg” height=”100%” width=”100%”>
<mx:dataProvider>
<mx:Array id=”initDG”>
<mx:Object PLAYER=”Rafer Alston” POS=”G” FROM=”Fresno State”/>
<mx:Object PLAYER=”Luther Head” POS=”G” FROM=”lllinois”/>
<mx:Object PLAYER=”Tracy McGrady” POS=”F-G” FROM=”Mount Zion Christian Acad. HS (NC)”/>
<mx:Object PLAYER=”Dikembe Mutombo” POS=”C” FROM=”Georgetown”/>
<mx:Object PLAYER=”Stromile Swift” POS=”F” FROM=”Louisiana State”/>
<mx:Object PLAYER=”David Wesley” POS=”G” FROM=”Baylor”/>
<mx:Object PLAYER=”Yao Ming” POS=”C” FROM=”China”/>
<mx:Object PLAYER=”Kobe Bryant” POS=”G” FROM=”Lower Merion HS (PA)”/>
<mx:Object PLAYER=”Kwame Brown” POS=”F-C” FROM=”Glynn Academy HS (GA)”/>
<mx:Object PLAYER=”Lamar Odom” POS=”F” FROM=”Rhode Island”/>
<mx:Object PLAYER=”Andrew Bynum” POS=”C” FROM=”St. Joseph HS (NJ)”/>
<mx:Object PLAYER=”Brian Cook” POS=”F” FROM=”Illinois”/>
<mx:Object PLAYER=”Devean George” POS=”F” FROM=”Augsburg”/>
<mx:Object PLAYER=”Devin Green” POS=”G” FROM=”Hampton”/>
<mx:Object PLAYER=”Aaron McKie” POS=”F” FROM=”Temple”/>
<mx:Object PLAYER=”Chris Mihm” POS=”C” FROM=”Texas”/>
<mx:Object PLAYER=”Smush Parker” POS=”G” FROM=”Fordham”/>
<mx:Object PLAYER=”Ronny Turiaf” POS=”F” FROM=”Gonzaga”/>
<mx:Object PLAYER=”Sasha Vujacic” POS=”F” FROM=”Slovenia”/>
<mx:Object PLAYER=”Von Wafer” POS=”F” FROM=”Florida State”/>
<mx:Object PLAYER=”Luke Walton” POS=”F” FROM=”Arizona”/>
<mx:Object PLAYER=”Lamar Odom” POS=”F” FROM=”Rhode Island”/>
<mx:Object PLAYER=”Jim Jackson” POS=”F-G” FROM=”Ohio State”/>
</mx:Array>
</mx:dataProvider>
</mx:DataGrid>
<mx:Spacer />
<mx:Spacer />
<mx:HBox width=”100%”>
<mx:Button label=”首页” id=”fPage” click=”showFirstPage()”/>
<mx:Button label=”第一页” id=”pPage” click=”showPreviousPage()”/>
<mx:Text id=”txt” width=”52″/>
<mx:Button label=”下一页” id=”nPage” click=”showNextPage()”/>
<mx:Button label=”尾页” id=”lPage” click=”showLastPage()”/>
<mx:ComboBox id=”sPage” dataProvider=”{indexarr}” selectedIndex=”0″ change=”combobox1_changeHandler(event)”></mx:ComboBox>
</mx:HBox>
</mx:Panel>
</mx:Application>
转载自 [ http://www.flexswf.com ]