Get the ID of a custom Control

2020-03-26 05:39发布

I have a fairly complex Custom Control that may be used multiple times on any given XPage. In the control I create a couple of viewScope variables that have to be unique to the specific custom Control. I would like to do something like viewScope.put(customControlID + "variableName","Stuff) But I don't know how to get the custom controls ID

3条回答
2楼-- · 2020-03-26 06:09

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", ...

查看更多
再贱就再见
3楼-- · 2020-03-26 06:12

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>
查看更多
Animai°情兽
4楼-- · 2020-03-26 06:23

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}" />
查看更多
登录 后发表回答