カスタムリストコントローラの作成
カスタムリストコントローラは、標準リストコントローラと似ています。カスタムリストコントローラには、レコードセットの表示や操作を行うために定義した Apex ロジックを実装できます。
たとえば、SOQL クエリに基づいて次のカスタムリストコントローラを作成できます。
1swfobject.registerObject("clippy.sosc_code_example", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class opportunityList2Con {
18 // ApexPages.StandardSetController must be instantiated
19 // for standard list controllers
20 public ApexPages.StandardSetController setCon {
21 get {
22 if(setCon == null) {
23 setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
24 [SELECT Name, CloseDate FROM Opportunity]));
25 }
26 return setCon;
27 }
28 set;
29 }
30
31 // Initialize setCon and return a list of records
32 public List<Opportunity> getOpportunities() {
33 return (List<Opportunity>) setCon.getRecords();
34 }
35}次の Visualforce マークアップは、上記のカスタムコントローラをページ内で使用する方法を示します。
1swfobject.registerObject("clippy.sosc_custom_page", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="opportunityList2Con">
18 <apex:pageBlock>
19 <apex:pageBlockTable value="{!opportunities}" var="o">
20 <apex:column value="{!o.Name}"/>
21 <apex:column value="{!o.CloseDate}"/>
22 </apex:pageBlockTable>
23 </apex:pageBlock>
24</apex:page>また、SOQL クエリの一部として反結合および準結合を使用するカスタムリストコントローラを作成することもできます。次のコードは、標準取引先コントローラの拡張として実装されます。
これらのレコードを表示するページでは、標準リストコントローラアクションを組み合わせて使用しますが、カスタムリストコントローラから返されるレコードの反復処理に基づいています。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public with sharing class AccountPagination {
18 private final Account acct;
19
20 // The constructor passes in the standard controller defined
21 // in the markup below
22 public AccountPagination(ApexPages.StandardSetController controller) {
23 this.acct = (Account)controller.getRecord();
24 }
25
26 public ApexPages.StandardSetController accountRecords {
27 get {
28 if(accountRecords == null) {
29 accountRecords = new ApexPages.StandardSetController(
30 Database.getQueryLocator([SELECT Name FROM Account WHERE Id NOT IN
31 (SELECT AccountId FROM Opportunity WHERE IsClosed = true)]));
32 }
33 return accountRecords;
34 }
35 private set;
36 }
37 public List<Account> getAccountPagination() {
38 return (List<Account>) accountRecords.getRecords();
39 }
40}
411swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page standardController="Account" recordSetVar="accounts" extensions="AccountPagination">
18 <apex:pageBlock title="Viewing Accounts">
19 <apex:form id="theForm">
20 <apex:pageBlockSection >
21 <apex:dataList value="{!accountPagination}" var="acct" type="1">
22 {!acct.name}
23 </apex:dataList>
24 </apex:pageBlockSection>
25 <apex:panelGrid columns="2">
26 <apex:commandLink action="{!previous}">Previous</apex:commandlink>
27 <apex:commandLink action="{!next}">Next</apex:commandlink>
28 </apex:panelGrid>
29 </apex:form>
30 </apex:pageBlock>
31</apex:page>