2

In a my applications I have defined a property of an object as a LinkedHashSet. The property I fill with values from a multi-value field:

Vector<String> ctrs = doc.getItemValue("countries");        
LinkedHashSet<String> items = new LinkedHashSet<String>();      
for (int i = 0; i < ctrs.size(); i++){          
    items.add(ctrs.get(i));
}       
employee.setCountry(items);

On an XPage I would like to display the values as followed:

<xp:inputText id="inputCountries" value="#{employeeBean.employee.Country}">
    <xp:this.multipleSeparator><![CDATA[#{javascript:var val = getComponent("contractType").getValue();
if (val == "Multi"){
    return ",";
}}]]></xp:this.multipleSeparator>
</xp:inputText>

Depending on the type of employee this field may be single or multi-value.

When view the XPage the returned value is displayed as followed:

[Sweden, Denmark, Estonia]

Ofcourse I would have it displayed as multi-value. What should I do to correct this?

Malin
  • 697
  • 5
  • 21

3 Answers3

3

If you convert your HashSet to an Array then it should work

Here an example, the first as single/ the second as multi value field:

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

    <xp:inputText id="singleValue" multipleSeparator=",">
        <xp:this.value><![CDATA[#{javascript:var items:java.util.Set = new java.util.LinkedHashSet();
items.add("Sweden");      
return items.toArray();}]]>
        </xp:this.value>
    </xp:inputText>

    <br></br>
    <br></br>

    <xp:inputText id="multiValue" multipleSeparator=",">
        <xp:this.value><![CDATA[#{javascript:var items:java.util.Set = new java.util.LinkedHashSet();
items.add("Sweden");
items.add("Denmark");  
items.add("Estonia");
return items.toArray();}]]>
        </xp:this.value>
    </xp:inputText>

</xp:view>

The output on a browser looks like:

Sweden

Sweden,Denmark,Estonia
Georg Kastenhofer
  • 1,387
  • 12
  • 32
1

You must use List instead of Set.

In addition, unless you are using LinkedHashSet as a way to remove duplicate values from the Vector, you can just pass the setCountry method the vector because Vector implements List:

// setting country method
yourBlock() {
   // It's better to use interface instead of implementation for the variable
   List<String> countries = doc.getItemValue("countries");

   setCountries(countries);
}

If you do use LinkedHashSet as a way to get unique values you need to tweak the above code just a bit more.

// setting country method
yourBlock() {
   List<String> countries = doc.getItemValue("countries");

   setCountries(new ArrayList<String>(new LinkedHashSet<String>(countries)));
}
shillem
  • 1,260
  • 7
  • 12
0

In case you want just view the data

Option 1: Split values with @Implode and some separator, coma or <br/> should work.

Option 2: Use repeat with custom rendering: multiple computed texts, divs or table.

In case you want to edit data

You need to use repeat. In that case you need to implement Map interface to your bean if you want to bind inputs. Good read is here: https://www.mindoo.com/web/blog.nsf/dx/16.07.2009095816KLEBCY.htm?opendocument&comments

Frantisek Kossuth
  • 3,524
  • 2
  • 23
  • 42