Apex ページネーション(ページング、ページ送り)の実装サンプル

環境
Salesforce

機能
最初のページ
最後のページ
前のページ
次のページ
現在ページを中心としたページ番号

1.コントローラ側コード
書式
ApexPages.StandardSetController 変数名 =new ApexPages.StandardSetController([SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact]);
前のページ
変数名.previous();
次のページ
変数名.next();
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public with sharing class pagination{
// 1ページあたりの表示件数
private static final Integer PER_PAGE = 5;
// 表示するリンクの数
private static final Integer NUMBER_LINKS =4;
private ApexPages.StandardSetController ass;
// クリックしたページ番号
public Integer clickPageNumber {get; set;}
// コンストラクタ
public pagination(){
ass = new ApexPages.StandardSetController([SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact]);
this.ass.setPageSize(PER_PAGE);
}
// 指定のページ
public void goPageNumber(){
this.ass.setPageNumber(this.clickPageNumber);
}
// 前のページ
public void goPrevious(){
this.ass.previous();
}
// 次のページ
public void goNext(){
this.ass.next();
}
// 最初のページ
public void goFirst(){
this.ass.first();
}
// 最後のページ
public void goLast(){
this.ass.last();
}
// 前のページが存在するか
public Boolean getHasPrevious(){
return this.ass.getHasPrevious();
}
// 次のページが存在するか
public Boolean getHasNext(){
return this.ass.getHasNext();
}
// 最初のページか
public Boolean getIsFirst(){
return this.ass.getPageNumber() == 1;
}
// 最後のページか
public Boolean getIsLast(){
return this.ass.getPageNumber() == this.getTotalPages();
}
// 現在のページを取得
public Integer getPageNumber(){
return this.ass.getPageNumber();
}
// ページ数を取得
public Integer getTotalPages(){
Decimal pages = Decimal.valueOf(this.ass.getResultSize()) / Decimal.valueOf(this.ass.getPageSize());
return Integer.valueOf(pages.round(System.RoundingMode.CEILING));
}
// ページ番号のリンクを取得
public List<Integer> getPageNumberLinks(){
Integer startPageNumber = this.ass.getPageNumber() - NUMBER_LINKS;
if(startPageNumber <= 0){
startPageNumber = 1;
}
Integer endPageNumber = this.ass.getPageNumber() + NUMBER_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>)ass.getRecords();
}
}
public with sharing class pagination{ // 1ページあたりの表示件数 private static final Integer PER_PAGE = 5; // 表示するリンクの数 private static final Integer NUMBER_LINKS =4; private ApexPages.StandardSetController ass; // クリックしたページ番号 public Integer clickPageNumber {get; set;} // コンストラクタ public pagination(){ ass = new ApexPages.StandardSetController([SELECT Id, FirstName, LastName, Title, Phone, Email FROM Contact]); this.ass.setPageSize(PER_PAGE); } // 指定のページ public void goPageNumber(){ this.ass.setPageNumber(this.clickPageNumber); } // 前のページ public void goPrevious(){ this.ass.previous(); } // 次のページ public void goNext(){ this.ass.next(); } // 最初のページ public void goFirst(){ this.ass.first(); } // 最後のページ public void goLast(){ this.ass.last(); } // 前のページが存在するか public Boolean getHasPrevious(){ return this.ass.getHasPrevious(); } // 次のページが存在するか public Boolean getHasNext(){ return this.ass.getHasNext(); } // 最初のページか public Boolean getIsFirst(){ return this.ass.getPageNumber() == 1; } // 最後のページか public Boolean getIsLast(){ return this.ass.getPageNumber() == this.getTotalPages(); } // 現在のページを取得 public Integer getPageNumber(){ return this.ass.getPageNumber(); } // ページ数を取得 public Integer getTotalPages(){ Decimal pages = Decimal.valueOf(this.ass.getResultSize()) / Decimal.valueOf(this.ass.getPageSize()); return Integer.valueOf(pages.round(System.RoundingMode.CEILING)); } // ページ番号のリンクを取得 public List<Integer> getPageNumberLinks(){ Integer startPageNumber = this.ass.getPageNumber() - NUMBER_LINKS; if(startPageNumber <= 0){ startPageNumber = 1; } Integer endPageNumber = this.ass.getPageNumber() + NUMBER_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>)ass.getRecords(); } }
public with sharing class pagination{
    // 1ページあたりの表示件数
    private static final Integer PER_PAGE = 5;
    // 表示するリンクの数
    private static final Integer NUMBER_LINKS =4;

    private ApexPages.StandardSetController ass;

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

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

    // 指定のページ
    public void goPageNumber(){
        this.ass.setPageNumber(this.clickPageNumber);
    }

    // 前のページ
    public void goPrevious(){
        this.ass.previous();
    }

    // 次のページ
    public void goNext(){
        this.ass.next();
    }

    // 最初のページ
    public void goFirst(){
        this.ass.first();
    }

    // 最後のページ
    public void goLast(){
        this.ass.last();
    }

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

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

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

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

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

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

    // ページ番号のリンクを取得
    public List<Integer> getPageNumberLinks(){
        Integer startPageNumber = this.ass.getPageNumber() - NUMBER_LINKS;
        if(startPageNumber <= 0){
            startPageNumber = 1;
        }
        Integer endPageNumber = this.ass.getPageNumber() + NUMBER_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>)ass.getRecords();
    }
}

2.画面表示
書式
<apex:pageBlockTable value="{!リスト名s}" var="変数名">
<apex:column value="{!変数名.属性名}"/>
</apex:pageBlockTable>
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<apex:page controller="pagination">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!contacts}" var="contc">
<apex:column value="{!contc.FirstName}"/>
<apex:column value="{!contc.LastName}"/>
<apex:column value="{!contc.Title}"/>
<apex:column value="{!contc.Phone}"/>
<apex:column value="{!contc.Email}"/>
</apex:pageBlockTable>
<br/>
<!-- ページネーション -->
<apex:commandLink value="<< 最初" rendered="{!!isFirst}" action="{!goFirst}" />
<apex:outputLabel value="<< 最初" rendered="{!isFirst}" />
<apex:outputLabel value=" | " />
<apex:commandLink value="< 前" rendered="{!hasPrevious}" action="{!goPrevious}" />
<apex:outputLabel value="< 前" rendered="{!!hasPrevious}" />
<apex:outputLabel value=" | " />
<apex:repeat value="{!pageNumberLinks}" var="links">
<apex:commandLink value="{!links}" rendered="{!links != pageNumber}" action="{!goPageNumber}" >
<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="{!goNext}" />
<apex:outputLabel value="次 >" rendered="{!!hasNext}" />
<apex:outputLabel value=" | " />
<apex:commandLink value="最後 >>" rendered="{!!isLast}" action="{!goLast}" />
<apex:outputLabel value="最後 >>" rendered="{!isLast}" />
</apex:pageBlock>
</apex:form>
</apex:page>
<apex:page controller="pagination"> <apex:form > <apex:pageBlock > <apex:pageBlockTable value="{!contacts}" var="contc"> <apex:column value="{!contc.FirstName}"/> <apex:column value="{!contc.LastName}"/> <apex:column value="{!contc.Title}"/> <apex:column value="{!contc.Phone}"/> <apex:column value="{!contc.Email}"/> </apex:pageBlockTable> <br/> <!-- ページネーション --> <apex:commandLink value="<< 最初" rendered="{!!isFirst}" action="{!goFirst}" /> <apex:outputLabel value="<< 最初" rendered="{!isFirst}" /> <apex:outputLabel value=" | " /> <apex:commandLink value="< 前" rendered="{!hasPrevious}" action="{!goPrevious}" /> <apex:outputLabel value="< 前" rendered="{!!hasPrevious}" /> <apex:outputLabel value=" | " /> <apex:repeat value="{!pageNumberLinks}" var="links"> <apex:commandLink value="{!links}" rendered="{!links != pageNumber}" action="{!goPageNumber}" > <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="{!goNext}" /> <apex:outputLabel value="次 >" rendered="{!!hasNext}" /> <apex:outputLabel value=" | " /> <apex:commandLink value="最後 >>" rendered="{!!isLast}" action="{!goLast}" /> <apex:outputLabel value="最後 >>" rendered="{!isLast}" /> </apex:pageBlock> </apex:form> </apex:page>
<apex:page controller="pagination">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!contacts}" var="contc">
                <apex:column value="{!contc.FirstName}"/>
                <apex:column value="{!contc.LastName}"/>
                <apex:column value="{!contc.Title}"/>
                <apex:column value="{!contc.Phone}"/>
                <apex:column value="{!contc.Email}"/>
            </apex:pageBlockTable>

            <br/>

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

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

            <apex:repeat value="{!pageNumberLinks}" var="links">
                <apex:commandLink value="{!links}" rendered="{!links != pageNumber}" action="{!goPageNumber}" >
                    <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="{!goNext}" />
            <apex:outputLabel value="次 >" rendered="{!!hasNext}" />
            <apex:outputLabel value=" | " />

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

 

Apex

Posted by arkgame