1

I have a Combo Box and I would like to have the possibility to add new values in the Combo Box using a button and an Input Field. I tried with:

var value = getComponent("input").getValue(); 
getComponent("combobox").setValue(value);

but it is not working.

Thank you,

Florin

Florin Pop
  • 5,105
  • 3
  • 25
  • 58

1 Answers1

4

Use a viewScope e.g. viewScope.selectItems variable.

  • Use this viewScope as the selectItems list.
  • Add the initial values to it.
  • Later, add a additional new item to this viewScope and then it will appear in combobox's selection item list.

This is a working example:

<xp:comboBox
    id="comboBox1"
    value="#{sessionScope.test}">
    <xp:selectItems>
        <xp:this.value><![CDATA[#{javascript:
        if (!viewScope.selectItems) {
            viewScope.selectItems = ["your","initial","values"];
        }
        return viewScope.selectItems;}]]></xp:this.value>
    </xp:selectItems>
</xp:comboBox>
<xp:inputText
    id="inputText1"
    value="#{viewScope.newItem}">
</xp:inputText>
<xp:button
    value="Add to selectItems"
    id="button1">
    <xp:eventHandler
        event="onclick"
        submit="true"
        refreshMode="complete">
        <xp:this.action><![CDATA[#{javascript:
            viewScope.selectItems.add(viewScope.newItem); 
            viewScope.newItem = "";
        }]]></xp:this.action>
    </xp:eventHandler>
</xp:button>
Knut Herrmann
  • 30,880
  • 4
  • 31
  • 67
  • If I use applicationScope the values will remain there permanently? I want those values to be saved. Thank you. – Florin Pop Sep 08 '14 at 07:01
  • 2
    Not really permanently. Only as long applicationScope lives. If you restart http task or server or you change code then applicationScope will be reset. If you want to save the values permanently you have to save them in a e.g. Notes document. – Knut Herrmann Sep 08 '14 at 07:09
  • 2
    @Flopet17 If you have a view with a column containing all saved values for that field, you could use a DbColumn formula to build the selectItems list. Alternatively, you might consider creating config documents to store the possible values rather than relying on scoped variables. – David Navarre Sep 08 '14 at 18:42
  • what if I do not want to use a viewscope? – Patrick Kwinten Apr 21 '16 at 09:58
  • @Patrick: depending on your use case you can use sessionScope, applicationScope, document item, datacontext, compositeData... any data container which is able to store an item list. – Knut Herrmann Apr 21 '16 at 11:30
  • but can't you add them directly? – Patrick Kwinten Apr 22 '16 at 06:39