カスタマイズ性重視。
例外処理も抜きで、超シンプルなページネーションのサンプルコードです。
完成品
VFコード
<apex:page controller="pagination">
<h1>ページネーション練習用</h1>
<apex:form>
<apex:dataList value="{!accResults}" var="acc">
<p>{!acc.Name}</p>
</apex:dataList>
<div>
<apex:repeat value="{!pageNumList}" var="pageNum">
<apex:commandLink action="{!goClickNumPage}" value="{!pageNum}" style="margin-left:10px;">
<apex:param value="{!pageNum}" name="clickNum"/>
</apex:commandLink>
</apex:repeat>
</div>
<apex:commandLink action="{!goPrevious}" value="前へ"/>
<apex:commandLink action="{!goNext}" value="次へ" style="margin-left:10px;"/>
</apex:form>
</apex:page>
Apexコード
public with sharing class pagination {
private Apexpages.StandardSetController ssc ;
public List<Account> accResults {get; private set;}
public pagination() {
List<Account> accList = [SELECT Name FROM Account];
this.ssc = new Apexpages.StandardSetController(accList);
this.ssc.setPageSize(5);
this.accResults = ssc.getRecords();
}
public List<Integer> getPageNumList(){
List<Integer> resultList = new List<Integer>();
Decimal totalRecordSize = (Decimal)this.ssc.getResultSize();
Decimal endPageNumDec = totalRecordSize.divide(this.ssc.getPageSize(), 0, System.RoundingMode.UP);
Integer endPageNum = (Integer)endPageNumDec;
for(Integer i=1; i<=endPageNum; i++){
resultList.add(i);
}
return resultList;
}
public void goClickNumPage(){
String strClickNum = Apexpages.currentPage().getParameters().get('clickNum');
Integer clickNum = Integer.valueOf(strClickNum);
this.ssc.setPageNumber(clickNum);
this.accResults = this.ssc.getRecords();
}
public void goPrevious(){
this.ssc.previous();
this.accResults = ssc.getRecords();
}
public void goNext(){
this.ssc.Next();
this.accResults = ssc.getRecords();
}
}