本部長は管理ができない

Salesforceに関わっているエンジニアの技術メモ。ときどきそれ以外。

ページネーション

ApexとVisualforceでページネーション(ページング、ページ送り)を作ってみた。
※この記事を参考にさせていただきました。
StandardSetController | Official Blog of CloudClickware

表示するリンク
  • 最初のページ
  • 最後のページ
  • 前のページ
  • 次のページ
  • 現在ページを中心としたページ番号

ページ番号の表示数は定数で指定。

ページリンクのデザイン
<< 最初 | < 前 | 1 | 2 | 3 | 4 | 5 | 次 > | 最後 >>
コントローラ
public with sharing class paginationController{
    // 1ページあたりの表示件数
    private static final Integer ROW_PER_PAGE = 3;
    // 表示するリンクの数(現在ページを中心に前後に表示する数)
    private static final Integer NUMBER_OF_LINKS = 2;

    private ApexPages.StandardSetController setCon;

    // クリックしたページ番号
    public Integer clickPageNumber {get; set;}

    // コンストラクタ
    public paginationController(){
        setCon = new ApexPages.StandardSetController([SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact]);
        this.setCon.setPageSize(ROW_PER_PAGE);
    }

    // 指定のページへ移動
    public void doPageNumber(){
        this.setCon.setPageNumber(this.clickPageNumber);
    }

    // 前のページへ移動
    public void doPrevious(){
        this.setCon.previous();
    }

    // 次のページへ移動
    public void doNext(){
        this.setCon.next();
    }

    // 最初のページへ移動
    public void doFirst(){
        this.setCon.first();
    }

    // 最後のページへ移動
    public void doLast(){
        this.setCon.last();
    }

    // 前のページが存在するか
    public Boolean getHasPrevious(){
        return this.setCon.getHasPrevious();
    }

    // 次のページが存在するか
    public Boolean getHasNext(){
        return this.setCon.getHasNext();
    }

    // 最初のページか
    public Boolean getIsFirst(){
        return this.setCon.getPageNumber() == 1;
    }

    // 最後のページか
    public Boolean getIsLast(){
        return this.setCon.getPageNumber() == this.getTotalPages();
    }

    // 現在のページを取得
    public Integer getPageNumber(){
        return this.setCon.getPageNumber();
    }

    // ページ数を取得
    public Integer getTotalPages(){
        Decimal pages = Decimal.valueOf(this.setCon.getResultSize()) / Decimal.valueOf(this.setCon.getPageSize());
        return Integer.valueOf(pages.round(System.RoundingMode.CEILING));
    }

    // ページ番号のリンクを取得
    public List<Integer> getPageNumberLinks(){
        Integer startPageNumber = this.setCon.getPageNumber() - NUMBER_OF_LINKS;
        if(startPageNumber <= 0){
            startPageNumber = 1;
        }
        Integer endPageNumber = this.setCon.getPageNumber() + NUMBER_OF_LINKS;
        if(endPageNumber > this.getTotalPages()){
            endPageNumber = this.getTotalPages();
        }

        List<Integer> links = new List<Integer>();
        for(Integer i = startPageNumber; i <= endPageNumber; i++){
            links.add(i);
        }

        return links;
    }

    // 表示するレコードを取得
    public List<Contact> getContacts(){
        return (List<Contact>)setCon.getRecords();
    }
}
ページ
<apex:page controller="paginationController">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!contacts}" var="cont">
                <apex:column value="{!cont.FirstName}"/>
                <apex:column value="{!cont.LastName}"/>
                <apex:column value="{!cont.Title}"/>
                <apex:column value="{!cont.Phone}"/>
                <apex:column value="{!cont.Email}"/>
            </apex:pageBlockTable>

            <br/>

            <!-- ページネーション -->
            <apex:commandLink value="<< 最初" rendered="{!!isFirst}" action="{!doFirst}" />
            <apex:outputLabel value="<< 最初" rendered="{!isFirst}" />
            <apex:outputLabel value=" | " />

            <apex:commandLink value="< 前" rendered="{!hasPrevious}" action="{!doPrevious}" />
            <apex:outputLabel value="< 前" rendered="{!!hasPrevious}" />
            <apex:outputLabel value=" | " />

            <apex:repeat value="{!pageNumberLinks}" var="links">
                <apex:commandLink value="{!links}" rendered="{!links != pageNumber}" action="{!doPageNumber}" >
                    <apex:param value="{!links}" name="clickPageNumber" assignTo="{!clickPageNumber}" />
                </apex:commandLink>
                <apex:outputLabel value="{!links}" rendered="{!links == pageNumber}" />
                <apex:outputLabel value=" | " />
            </apex:repeat>

            <apex:commandLink value="次 >" rendered="{!hasNext}" action="{!doNext}" />
            <apex:outputLabel value="次 >" rendered="{!!hasNext}" />
            <apex:outputLabel value=" | " />

            <apex:commandLink value="最後 >>" rendered="{!!isLast}" action="{!doLast}" />
            <apex:outputLabel value="最後 >>" rendered="{!isLast}" />
        </apex:pageBlock>
    </apex:form>
</apex:page>