2013-10-16 46 views
3

我有一個相當複雜的自定義控件,可以在任何給定的XPage上多次使用。在控件中,我創建了幾個viewScope變量,這些變量必須對特定的自定義控件唯一。我想要做一些像viewScope.put(customControlID +「variableName」,「Stuff) 但我不知道如何獲得自定義控件ID獲取自定義控件的ID

回答

11

您可以獲得當前的自定義控件ID this.getId()<xp:view>水平

如果你把這個ID到的CompositeData變量(例如compositeData.id),那麼你可以使用該ID的自定義控制無處不在,你想裏面

<xp:view xmlns:xp="http://www.ibm.com/xsp/core" 
    beforePageLoad="#{javascript:compositeData.id = this.getId()}" > 

用法在SSJS:。

viewScope.put(compositeData.id + "variableName","Stuff") 

通常,標識被命名爲喜歡 「_id2」, 「_id8」,...

+0

這是一個相當聰明的方法,Knut。 –

+0

聰明!表示讚許,滿意,勝利! –

1

這裏是另一種解決方案作爲SSJS功能:

function getCCId(cmp:javax.faces.component.UIComponent):string{ 

    try{ 
     if(typeof(cmp) === 'com.ibm.xsp.component.UIIncludeComposite'){ 
      return cmp.getId(); 
     } 
     return getCCId(cmp.getParent()) 
    }catch(e){} 

} 

功能爬上組件樹,直到它找到父CC,然後返回該ID。

您可以使用它f.e.在這樣的標籤:

<xp:label id="label1"> 
    <xp:this.value><![CDATA[#{javascript:getCCId(this)}]]></xp:this.value> 
</xp:label> 
3

您可以使用的DataContext變量:

<xp:this.dataContexts> 
    <xp:dataContext 
     value="#{javascript:this.getId()}" 
     var="id"> 
    </xp:dataContext> 
</xp:this.dataContexts> 

變量然後訪問作爲ID在SSJS ...

<xp:label id="label1" value="#{javascript:id}" /> 

...或在EL中:

<xp:label id="label1" value="#{id}" /> 
+0

這也是一個非常方便的解決方案。 –

相關問題