完璧じゃなくてもとりあえず終わらせるブログ

主にSalesforceの悪夢を記録しています

Visualforceコンポーネントにデータをattributeするときの記載方法まとめ

Visualforceコンポーネントにattributeを書こうとするときに一瞬、あれ?となるのでメモしておきます。

公式:Visualforce開発者ガイド > apex:attribute
https://developer.salesforce.com/docs/atlas.ja-jp.pages.meta/pages/pages_compref_attribute.htm


プリミティブデータ型

<apex:component>
    <apex:attribute name="bool" type="Boolean" description="Booleanを渡す" />
    <apex:attribute name="dcml" type="Decimal" description="Decimalを渡す" />
    <apex:attribute name="dubl" type="Double" description="Doubleを渡す" />
    <apex:attribute name="sfid" type="ID" description="SFIDを渡す" />
    <apex:attribute name="int" type="Integer" description="Integerを渡す" />
    <apex:attribute name="lng" type="Long" description="Longを渡す" />
    <apex:attribute name="str" type="String" description="Stringを渡す" />
        -中略-
</apex:component>

これは単純ですね。


sObject

<apex:component>
    <apex:attribute name="sobj" type="sObject" description="汎用型のsObjectを渡す" />
    <apex:attribute name="cstmObj" type="CustomObject__c" description="カスタムオブジェクトを渡す" />
        -中略-
</apex:component>

汎用型のsObjectの使いどころとしては、
フィールド名も一緒に渡すことで動的なフィールド指定ができるので、コンポーネントの汎用性がより高まります。

例えば

<apex:component>
    <apex:attribute name="sobj" type="sObject" description="汎用型のsObjectを渡す" />
    <apex:attribute name="fieldName" type="String" description="フィールド名を渡す" />

    <apex:outputField value="{!sobj[fieldName]}" label="{!fieldName}" />
        -中略-
</apex:component>

汎用的な一覧表を表示させるコンポーネントなどに使えそうです。


一次元リスト

<apex:component>
    <apex:attribute name="arrayStr" type="String[]" description="Stringの配列を渡す" />
    <apex:attribute name="arrayCstmObj" type="CustomObject__c[]" description="カスタムオブジェクトの配列を渡す" />
    <apex:attribute name="selectItems" type="SelectOption[]" description="選択リストも渡せる" />
        -中略-
</apex:component>

選択リストもただの配列なので渡せます。
汎用的なプルダウンをコンポーネント化するのに使えそう。
※二次元以上の配列は渡せないようです。


カスタムApexデータ型(クラス)

<apex:component>
    <apex:attribute name="cstmCls" type="CustomClass" description="ApexClassを渡す" />
    <apex:attribute name="cstmClsList" type="CustomClass[]" description="ApexClass(データ型)のリストを渡す" />
        -中略-
</apex:component>

ApexClassをそのまま渡してコンポーネント内でプロパティを使うというのは結構やっちゃう。
データ型のApexClassのリストを渡して汎用的なグラフ表示用コンポーネントを作る、といったこともできそう。


pageAction

<apex:component>
    <apex:attribute name="pageAction" type="ApexPages.Action" description="pageActionを渡す"/>
        -中略-
</apex:component>

コンポーネント用のcontrollerは用意せず、呼び出し元のVisualforceのpageActionを使いたいときって結構あると思うんですよ。
これはつまり、呼び出し元Visualforceのcontrollerのメソッドを呼べるのでコンポーネント内で様々な処理をしなければならない場合は重宝します。


map

<apex:component>
    <apex:attribute name="hogeMap" type="map" description="mapを渡す" />
        -中略-
</apex:component>

すみません。使ったことないです。
mapとして渡せるならなんでも渡せる気がする……。
検証できたら追記します。



よし、とりあえず終わらせたぞ