Get the ID of a custom Control

我是研究僧i 提交于 2019-12-01 08:02:43

You can get the current custom control ID with this.getId() at the <xp:view> level.

If you put this ID into a compositeData variable (e.g. compositeData.id) then you can use the ID inside the custom control everywhere you want.

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

Usage in SSJS:

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

Typically, IDs are named like "_id2", "_id8", ...

You can use a dataContext variable:

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

The variable is then accessible as id in SSJS...

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

... or in EL:

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

Here is another solution as an SSJS function:

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){}

}

The function climbs the component tree until it finds the parent CC and then returns the id.

You can use it f.e. in a label like this:

<xp:label id="label1">
    <xp:this.value><![CDATA[#{javascript:getCCId( this )}]]></xp:this.value>
</xp:label>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!