4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

超シンプルなVF・Apexのページネーション

Last updated at Posted at 2021-12-30

カスタマイズ性重視。
例外処理も抜きで、超シンプルなページネーションのサンプルコードです。

完成品

スクリーンショット 2022-01-08 11.44.13.png

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();
    }
}

参考文献

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?