Salesforce StandardSetControllerクラスの使い方のサンプル

環境
Salesforce

操作例
1. インスタンス作成
List<Account> accList = [SELECT Name FROM Account];
Apexpages.StandardSetController ass = new Apexpages.StandardSetController(accList);

2. インスタンスの状態を変える
ass.setPageSize(5); //1セットに格納するレコード数を指定
ass.Next(); //次のレコードセットに切替

ページネーションのコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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: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: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>

3. インスタンスから値を取得する
List<Account> accResults = ass.getRecords(); //現在のレコードセットを取得

4.クラスpaginationのコード
Apexコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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();
}
}
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(); } }
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();
    }
}

 

IT

Posted by arkgame