DataGrid分页[原创]
<?xml version="1.0"?>
<mx:Application xmlns:mx="
http://www.macromedia.com/2003/mxml" backgroundColor="#FFFFFF" creationComplete="initApp()"> <mx:Script>
<![CDATA[
var pageRecordes:Number;
var totalPages:Number = 0;
var totalRows:Number = 0;
var currentPage:Number = 1;
var pageStartRow:Number = 1;
var pageEndRow:Number = 0;
public var initDG:Array = [
{ Artist: a, Price: 1, Album: one},
{ Artist: b, Price: 2, Album: two},
{ Artist: c, Price: 3, Album: three},
{ Artist: d, Price: 4, Album: four},
{ Artist: e, Price: 5, Album: five},
{ Artist: f, Price: 6, Album: six},
{ Artist: g, Price: 7, Album: seven},
{ Artist: h, Price: 8, Album: eight} ];
function initApp(){
txt.text = "Page " + currentPage;
pageRecordes = pageConfig.pageRecordes;
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;
}
}
function showPreviousPage(){
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);
}
function showNextPage(){
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(pageStartRow - 1,pageEndRow);
}
]]>
</mx:Script>
<mx:Model id="pageConfig" source="WEB-INF/classes/pageConfig.xml"/>
<mx:Panel title="DataGrid Panel" marginTop="10" height="100%" width="100%">
<mx:DataGrid id="dg" dataProvider="{initDG}" height="100%" width="100%" />
<mx:HBox>
<mx:Button id="pPage" label="prevPage" click="showPreviousPage()"></mx:Button>
<mx:Text id="txt"></mx:Text>
<mx:Button id="nPage" label="nextPage" click="showNextPage()"></mx:Button>
</mx:HBox>
</mx:Panel>
</mx:Application>
还有一个配置的xml文件,路径是:...WEB-INF/classes/pageConfig.xml
该xml文件内容如下:
<?xml version="1.0" encoding="shift_jis"?>
<web-app>
<pageRecordes>3</pageRecordes>
</web-app>
posted on 2007-02-28 13:48
☜♥☞MengChuChen 阅读(440)
评论(0) 编辑 收藏 所属分类:
flex2.0