Thomas Adrian
Thomas Adrian

Reputation: 3636

Is it possible to create dynamic fieldnames in custom control using EL language?

I have a custom control with a lot of fields, I want to include this custom control several times on the same Xpage but I need the fieldnames to be different for each custom control I add, and as there are many fields I do not want to create property definitions for all the fields.

I use all kind of fields including RT so I guess I need the binding to be done using EL language,

is there a way to make the field names dynamic? so that the the fields will be suffixed with something I add to property definition, i.e in the first cc all the fields will be suffixed "_1" and the second "_2" and so on.

Below just an example of a custom control

<?xml version="1.0" encoding="UTF-8"?>
    <xp:view xmlns:xp="http://www.ibm.com/xsp/core">

        <xp:panel>
            <xp:inputText id="inputText1" value="#{d.Fld1}"></xp:inputText>
            <xp:inputText id="inputText2" value="#{d.Fld2}"></xp:inputText>
            <xp:inputText id="inputText3" value="#{d.Fld3}"></xp:inputText>
        </xp:panel>
    </xp:view>

Upvotes: 1

Views: 90

Answers (1)

Knut Herrmann
Knut Herrmann

Reputation: 30970

Use the possibility to replace custom control's code parts with the pre-processor ${javascript:...} at first load time.

For example, an expression

value="#{doc.${javascript:'Fld1_' +  compositeData.index}}"

gets replaced by

value="#{doc.Fld1_2}"

if compositeData.index is "2".


Here is a working example for testing purposes:

custom control "ccFields":

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core">
    <xp:panel>
        <xp:inputText
            id="inputText1"
            value="#{doc.${javascript:'Fld1_' +  compositeData.index}}" />
        <xp:inputText
            id="inputText2"
            value="#{doc.${javascript:'Fld2_' +  compositeData.index}}" />
        <xp:inputRichText
            id="inputRichText1"
            value="#{doc.${javascript:'Fld3_' +  compositeData.index}}" />
    </xp:panel>
</xp:view>

with the property definition enter image description here

main XPage:

<?xml version="1.0" encoding="UTF-8"?>
<xp:view
    xmlns:xp="http://www.ibm.com/xsp/core"
    xmlns:xc="http://www.ibm.com/xsp/custom">
    <xp:this.data>
        <xp:dominoDocument var="doc" formName="Test" />
    </xp:this.data>
    <xc:ccFields index="1" />
    <xc:ccFields index="2" />
    <xp:messages />
    <xp:button
        value="save"
        id="button1">
        <xp:eventHandler
            event="onclick"
            submit="true"
            refreshMode="complete"
            immediate="false"
            save="true">
        </xp:eventHandler>
    </xp:button>
</xp:view>

When you hit button "save" it creates a new document with form "Test", the text fields

  • Fld1_1
  • Fld1_2
  • Fld2_1
  • Fld2_2

and the rich text fields

  • Fld3_1
  • Fld3_2

with the values you entered.

Upvotes: 1

Related Questions