Flex 2 DataGrid 分页代码,效果如下:
<?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[
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;
public function initApp():void{
txt.text = "Page " + currentPage;
totalRows = initDG.length;
if(initDG.length > pageRecordes){
dg.dataProvider = initDG.slice(0,pageRecordes);
pPage.enabled = false;
}
if((totalRows % pageRecordes) == 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;
}
}
public function showPreviousPage():void{
currentPage = currentPage - 1;
txt.text = "Page " + 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;
}
dg.dataProvider = initDG.slice(pageStartRow - 1,pageEndRow);
}
public function showNextPage():void{
currentPage = currentPage + 1;
txt.text = "Page " + 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;
}
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 = "Page " + 1;
currentPage = 1;
}
public function showLastPage():void{
dg.dataProvider = initDG.slice((totalPages - 1) * pageRecordes,totalRows);
pPage.enabled = true;
nPage.enabled = false;
txt.text = "Page " + totalPages;
currentPage = totalPages;
}
]]>
</mx:Script>
<mx:Panel title="DataGrid Panel" height="287" width="500">
<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:HBox width="391">
<mx:Spacer />
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="fPage" label="firstPage" itemClick="showFirstPage()">
<mx:dataProvider>
<mx:Array>
<mx:String>firstPage</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:LinkBar>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="pPage" label="prevPage" itemClick="showPreviousPage()">
<mx:dataProvider>
<mx:Array>
<mx:String>prePage</mx:String>
</mx: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>
<mx:Array>
<mx:String>nextPage</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:LinkBar>
<mx:LinkBar borderStyle="solid" color="#000000" backgroundColor="#CCFFFF" id="lPage" label="lastPage" itemClick="showLastPage()">
<mx:dataProvider>
<mx:Array>
<mx:String>lastPage</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:LinkBar>
</mx:HBox>
<mx:Spacer />
</mx:Panel>
</mx:Application>