当前页数:pageNow
页面记录的数:pageSize
总的数据信息:Collection (总数据)
总的记录数:totalResults 从总数据量中得到即可
总的页面数量: pageTotal=(totalResults+pageSize-1)/pageSize;
当前的页面开始记录: startResult=(pageNow-1)*pageSize;
当前的页面结束记录: endResult=pageNow*pageSize;
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
//每页的记录数
public var pageSize:uint=8;
//总的页数
public var totalPages:uint=0;
//总的记录数量
public var totalRows:uint=0;
//当前页的记录数
public var pageNow:uint=1;
//开始记录的行
public var pageStartRow:uint=1;
//结束记录的行
public var pageEndRow:uint=1;
//用于初始化的方法
public function initApp():void
{
txt.text="Page "+pageNow;
//计算总的记录数
totalRows=initDG.length;
if(initDG.length>pageSize)
{
//从总的记录中获取部分记录的方法同时并切换数据的
dg.dataProvider=initDG.slice(0,pageSize);
pPage.enabled=false;
}
//记录总的记录数量
totalPages=Math.floor((totalRows+pageSize-1)/pageSize);
if(totalRows<=pageSize)
{
this.pageStartRow=1;
this.pageEndRow=totalRows;
}else{
this.pageStartRow=1;
this.pageEndRow=pageSize;
}
if(totalPages==1)
{
pPage.enabled=false;
nPage.enabled=false;
}
}
//点击上一页的处理
public function showPreviousPage():void
{
pageNow=pageNow-1;
txt.text="page "+pageNow;
if(pageNow != 1)
{
pageStartRow=(pageNow-1)*pageSize+1;
pageEndRow=pageNow*pageSize;
}else{
pageEndRow=pageSize;
pageStartRow=1;
pPage.enabled=false;
}
dg.dataProvider=initDG.slice(pageStartRow-1,pageEndRow);
}
public function showNextPage():void{
pageNow = pageNow + 1;
txt.text = "Page " + pageNow;
if(pageNow == totalPages){
pageEndRow = totalRows;
pageStartRow =totalRows-pageSize;
nPage.enabled = false;
pPage.enabled = false;
}else{
pageStartRow = (pageNow - 1) * pageSize + 1;
pageEndRow = pageNow * pageSize;
nPage.enabled = true;
pPage.enabled = true;
}
dg.dataProvider = initDG.slice((pageNow - 1) * pageSize,pageEndRow);
}
//首页调用的方法
public function showFirstPage():void{
dg.dataProvider = initDG.slice(0,pageSize);
pPage.enabled = false;
nPage.enabled = true;
txt.text = "Page " + 1;
pageNow = 1;
}
//最后一页调用的方法
public function showLastPage():void{
dg.dataProvider = initDG.slice((totalPages - 1) * pageSize,totalRows);
pPage.enabled = true;
nPage.enabled = false;
txt.text = "Page " + totalPages;
pageNow = totalPages;
}
]]>
</fx:Script>
<mx:Panel title="DataGrid分页应用" height="287" width="500">
<mx:DataGrid id="dg" height="100%" width="100%">
<mx:dataProvider >
<fx:Array id="initDG">
<fx:Object PLAYER="Rafer Alston" POS="G" FROM="Fresno State"/>
<fx:Object PLAYER="Luther Head" POS="G" FROM="lllinois"/>
<fx:Object PLAYER="Tracy McGrady" POS="F-G" FROM="Mount Zion Christian Acad. HS (NC)"/>
<fx:Object PLAYER="Dikembe Mutombo" POS="C" FROM="Georgetown"/>
<fx:Object PLAYER="Stromile Swift" POS="F" FROM="Louisiana State"/>
<fx:Object PLAYER="David Wesley" POS="G" FROM="Baylor"/>
<fx:Object PLAYER="Yao Ming" POS="C" FROM="China"/>
<fx:Object PLAYER="Kobe Bryant" POS="G" FROM="Lower Merion HS (PA)"/>
<fx:Object PLAYER="Kwame Brown" POS="F-C" FROM="Glynn Academy HS (GA)"/>
<fx:Object PLAYER="Lamar Odom" POS="F" FROM="Rhode Island"/>
<fx:Object PLAYER="Andrew Bynum" POS="C" FROM="St. Joseph HS (NJ)"/>
<fx:Object PLAYER="Brian Cook" POS="F" FROM="Illinois"/>
<fx:Object PLAYER="Devean George" POS="F" FROM="Augsburg"/>
<fx:Object PLAYER="Devin Green" POS="G" FROM="Hampton"/>
<fx:Object PLAYER="Aaron McKie" POS="F" FROM="Temple"/>
<fx:Object PLAYER="Chris Mihm" POS="C" FROM="Texas"/>
<fx:Object PLAYER="Smush Parker" POS="G" FROM="Fordham"/>
<fx:Object PLAYER="Ronny Turiaf" POS="F" FROM="Gonzaga"/>
<fx:Object PLAYER="Sasha Vujacic" POS="F" FROM="Slovenia"/>
<fx:Object PLAYER="Von Wafer" POS="F" FROM="Florida State"/>
<fx:Object PLAYER="Luke Walton" POS="F" FROM="Arizona"/>
<fx:Object PLAYER="Lamar Odom" POS="F" FROM="Rhode Island"/>
<fx:Object PLAYER="Jim Jackson" POS="F-G" FROM="Ohio State"/>
</fx:Array>
</mx:dataProvider>
<mx:columns>
<mx:DataGridColumn headerText="歌 手" dataField="PLAYER" textAlign="center"/>
<mx:DataGridColumn headerText="方 式" dataField="POS" textAlign="center"/>
<mx:DataGridColumn headerText="国 家" dataField="FROM" textAlign="center"/>
</mx:columns>
</mx:DataGrid>
<mx:Spacer />
<mx:HBox width="391">
<mx:Spacer/>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="fPage" label="firstPage" itemClick="showFirstPage()">
<mx:dataProvider>
<fx:Array>
<fx:String>firstPage</fx:String>
</fx:Array>
</mx:dataProvider>
</mx:LinkBar>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="pPage" label="prevPage" itemClick="showPreviousPage()">
<mx:dataProvider>
<fx:Array>
<fx:String>prePage</fx:String>
</fx:Array>
</mx:dataProvider>
</mx:LinkBar>
<mx:Text id="txt" width="52"/>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="nPage" label="nextPage" itemClick="showNextPage()">
<mx:dataProvider>
<fx:Array>
<fx:String>nextPage</fx:String>
</fx:Array>
</mx:dataProvider>
</mx:LinkBar>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="lPage" label="lastPage" itemClick="showLastPage()">
<mx:dataProvider>
<fx:Array>
<fx:String>lastPage</fx:String>
</fx:Array>
</mx:dataProvider>
</mx:LinkBar>
</mx:HBox>
<mx:Spacer />
</mx:Panel>
</s:Application>