ちょっとハマることがあったのでComponentについて勉強してみました。
attributeではString型やInteger型など通常のデータ型に加え、sObject型やList型なども渡すことができます。
リストのリスト型や内部クラスは渡すことができないようです。
また、Classのインスタンスを生成して渡すことも可能のようです。
List型を渡す場合は次のように指定する必要があります。
<apex:attribute name="objContacts" type="Contact[]" assignTo="{!subContacts}" description="" />
サンプルソース
Page
<apex:page standardController="Account" extensions="ComponentSampleController" title="ComponentSample" showHeader="true" sidebar="false"> <apex:sectionHeader title="Force.com" subTitle="Component Sample" /> <apex:form > <c:ComponentSampleSub objAccount="{!objAccount}" objContacts="{!objContacts}" str="{!str}" num="{!num}" day="{!day}" dayTime="{!dayTime}" ctrl="{!ctrl}" /> </apex:form> </apex:page>
Controller
public with sharing class ComponentSampleController { // 取引先 public Account objAccount {get; set;} // 取引先責任者 public List<Contact> objContacts {get; set;} // 文字列 public String str {get; set;} // 数値 public Integer num {get; set;} // 日付 public Date day {get; set;} // 日付時間 public DateTime dayTime {get; set;} // コントローラー public ComponentSampleAssistController ctrl {get; set;} /* * コンストラクタ * @param : StandardController * @return : なし */ public ComponentSampleController(ApexPages.StandardController prmStandard) { System.debug('◆◆◆◆ComponentSampleController:START'); // 取引先 this.objAccount = (Account)prmStandard.getRecord(); if (String.isEmpty(this.objAccount.Id) == true) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '取引先の取得に失敗しました。')); return; } // 取引先責任者 this.objContacts = getContact(prmStandard.getId()); if (this.objContacts.isEmpty() == true) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, '取引先責任者:0件')); } // 文字列 this.str = 'Component Sample'; // 数値 this.num = 123456789; // 日付 this.day = system.today(); // 日付時間 this.dayTime = dateTime.now(); // コントローラー this.ctrl = new ComponentSampleAssistController(); System.debug('◆◆◆◆ComponentSampleController:END'); } /* * 取引先責任者取得 * @param : 取引先ID [prmAccountId] * @return : 取引先責任者 */ private List<Contact> getContact(Id prmAccountId) { return [select Id,Name from Contact where AccountId =: prmAccountId order by Name asc limit 200]; } }
Component
<apex:component Controller="ComponentSampleSubController"> <apex:attribute name="objAccount" type="Account" assignTo="{!subAccount}" description="" /> <apex:attribute name="objContacts" type="Contact[]" assignTo="{!subContacts}" description="" /> <apex:attribute name="str" type="String" assignTo="{!subStr}" description="" /> <apex:attribute name="num" type="Integer" assignTo="{!subNum}" description="" /> <apex:attribute name="day" type="Date" assignTo="{!subDay}" description="" /> <apex:attribute name="dayTime" type="DateTime" assignTo="{!subDayTime}" description="" /> <apex:attribute name="Ctrl" type="ComponentSampleAssistController" assignTo="{!subCtrl}" description="" /> <apex:pageBlock mode="detail"> <apex:pageMessages /> <apex:pageBlockSection title="sObject" columns="1" collapsible="false"> <apex:outputText value="{!subAccount.Id}" /> </apex:pageBlockSection> <apex:pageBlockSection title="List<sObject>" columns="1" collapsible="false"> <apex:pageBlockTable value="{!subContacts}" var="item"> <apex:column headerValue="{!$ObjectType.Contact.Fields.Name.Label}"> <apex:outputText value="{!item.Name}" /> </apex:column> </apex:pageBlockTable> </apex:pageBlockSection> <apex:pageBlockSection title="String" columns="1" collapsible="false"> <apex:outputText value="{!subStr}" /> </apex:pageBlockSection> <apex:pageBlockSection title="Integer" columns="1" collapsible="false"> <apex:outputText value="{!subNum}" /> </apex:pageBlockSection> <apex:pageBlockSection title="Date" columns="1" collapsible="false"> <apex:outputText value=" {0,date,yyyy/MM/dd}"> <apex:param value="{!subDay}" /> </apex:outputText> </apex:pageBlockSection> <apex:pageBlockSection title="DateTime" columns="1" collapsible="false"> <apex:outputText value=" {0,date,yyyy/MM/dd hh:mm:ss}"> <apex:param value="{!subDayTime}" /> </apex:outputText> </apex:pageBlockSection> <apex:pageBlockSection title="Controller" columns="1" collapsible="false"> <apex:outputText value="{!subCtrl.msg}" /> </apex:pageBlockSection> </apex:pageBlock> </apex:component>
Component・Controller
public with sharing class ComponentSampleSubController { // 取引先 public Account subAccount {get; set;} // 取引先責任者 public List<Contact> subContacts {get; set;} // 文字列 public String subStr {get; set;} // 数値 public Integer subNum {get; set;} // 日付 public Date subDay {get; set;} // 日付時間 public DateTime subDayTime {get; set;} // コントローラー public ComponentSampleAssistController subCtrl {get; set;} /* * コンストラクタ * @param : なし * @return : なし */ public ComponentSampleSubController() { System.debug('◆◆◆◆ComponentSampleSubController:START'); // 取引先 this.subAccount = new Account(); // 取引先責任者 this.subContacts = new List<Contact>(); // 文字列 this.subStr = ''; // 数値 this.subNum = 0; // 日付 this.subDay = null; // 日付時間 this.subDayTime = null; // コントローラー this.subCtrl = new ComponentSampleAssistController(); System.debug('◆◆◆◆ComponentSampleSubController:END'); } }
パラメータ用・Controller
public with sharing class ComponentSampleAssistController { public String msg {get; set;} /* * コンストラクタ * @param : なし * @return : なし */ public ComponentSampleAssistController() { System.debug('◆◆◆◆ComponentSampleAssistController:START'); this.msg = 'ComponentSampleAssist'; System.debug('◆◆◆◆ComponentSampleAssistController:END'); } }
GitHub Link
Apex_ComponentSample