tyoshikawa1106のブログ

- Force.com Developer Blog -

SFDC:repeat 処理で動的にIDを指定する

repeat処理中apexタグの場合は配列みたいにIDが割り当てられますが、HTMLタグの場合は割り当てられない問題に悩みました。


apexタグの場合はたぶん次のように割り当てられると思います。
thePage:theRepeat:0:theValue
thePage:theRepeat:1:theValue
thePage:theRepeat:2:theValue

これならJavaScript等で指定するときに一意の値で指定ができます。


これがHTMLタグの場合は配列のようには割り当てられませんでした。
※ulタグで確認


apexタグの場合

<apex:page controller="repeatCon" id="thePage">

    <apex:repeat value="{!objAccounts}" var="item" id="theRepeat">

        <apex:outputText value="{!item.Name}" id="theValue"/><br/>

    </apex:repeat>

</apex:page>


HTMLタグの場合

<apex:repeat value="{!objAccounts}" var="item" id="theRepeat">
    <ul id="treeView">
        <li></li>
    </ul>
</repeat>


どうやって動的にIDを割り当てようかいろいろ試してみて次の方法で対応できました。

<apex:repeat value="{!objAccounts}" var="item" id="theRepeat">
    <ul id="{!item.Id}">
        <li></li>
    </ul>
</repeat>


この方法ならオブジェクトのIDは一意なので重複することもなく指定できます。
id="Sample-{!item.Id}"みたいに書くことも可能だと思います。