re: 【第二版】RichFaces中使用datatable和datascroller进行分页(使用数据库分页,改良版)(含源码)(JSF 1.2,RichFaces 3.2.1GA)
2009-09-02 17:25 |
修改后的代码:
public abstract class PageListDataModel extends DataModel {
int pageSize;
int rowIndex;
DataPage page;
/**
* 抽像获取一页数据方法,由子类实现
* */
public abstract DataPage fetchPage(int dataSize,int startRow, int pageSize);
public PageListDataModel(int pageSize) {
super();
this.pageSize = pageSize;
this.rowIndex = -1;
this.page = null;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public DataPage getPage() {
int dataSize = 0;
if(null !=this.page) {
dataSize = this.page.getDataSize();
return this.page;
}
int rowIndex = this.getRowIndex();
int startRow = rowIndex;
if(rowIndex == -1) {
startRow = 0;
}
return fetchPage(dataSize,startRow, pageSize);
}
public void setPage(DataPage page) {
this.page = page;
}
@Override
public int getRowCount() {
return this.getPage().getDataSize();
}
@Override
public Object getRowData() {
if(rowIndex <0) {
throw new IllegalArgumentException("Invalid rowIndex for PagedListDataModel; not within page");
}
if(null == page) {
page = fetchPage(0,rowIndex, pageSize);
}
int dataSize = page.getDataSize();
int startRow = page.getStartRow();
int nRows = page.getData().size();
int endRow = startRow + nRows;
if(rowIndex >= dataSize) {
throw new IllegalArgumentException(" Invalid rowIndex ");
}
if(rowIndex < startRow) {
page = fetchPage(dataSize,rowIndex, pageSize);
startRow = page.getStartRow();
}else if (rowIndex >= endRow) {
page = fetchPage(dataSize,rowIndex, pageSize);
startRow = page.getStartRow();
}
return page.getData().get(rowIndex - startRow);
}
@Override
public int getRowIndex() {
return this.rowIndex;
}
@Override
public Object getWrappedData() {
return page.getData();
}
@Override
public boolean isRowAvailable() {
DataPage page = getPage();
if (page == null) {
return false;
}
int rowIndex = getRowIndex();
if (rowIndex < 0) {
return false;
} else if (rowIndex >= page.getDataSize()) {
return false;
} else {
return true;
}
}
@Override
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
@Override
public void setWrappedData(Object o) {
if (o instanceof DataPage) {
this.page = (DataPage)o;
}else {
throw new UnsupportedOperationException("setWrappedData");
}
}
public void refresh() {
if (null !=this.page) {
this.page = null;
getPage();
}
}
回复 更多评论