Tapestry loop through hashmap
Asked Answered
G

1

9

I'm trying to loop through a hashmap and display a number checkboxes with id the key of the hashmap and label the value of the hashmap. Anyone knows how the tapestry syntax for that is?

Cheers Dimitris

Gillis answered 12/3, 2010 at 14:36 Comment(0)
V
14

You should be able to loop through the key set like this:

<form t:type="Form">
    <t:Loop t:source="myMap.keySet()" t:value="currentKey"> 
        <input type="Checkbox" t:type="Checkbox" t:id="checkbox"
            t:value="currentValue"/>
        <label t:type="Label" for="checkbox">${mapValue}</label>
    </t:Loop>
</form>

Class file:

@Property
private Object currentKey;

@Persist
private Set<String> selection = new HashSet<String>();

public Map<String,String> getMyMap() {
    ...
}

public boolean getCurrentValue() {
     return this.selection.contains(this.currentKey);
}

public void setCurrentValue(final boolean currentValue) {
    final String mapValue = this.getMapValue();

    if (currentValue) {
        this.selection.add(mapValue);
    } else {
        this.selection.remove(mapValue);
    }
}


public String getMapValue() {
    return this.getMyMap().get(this.currentKey);
}

I haven't compiled this, but it should help you get started.

Valenevalenka answered 12/3, 2010 at 18:45 Comment(1)
Thanks a lot!!! That's exactly what I was searching for. Had to add a method. public String getLabelValue(){ return this.getMyMap().get(this.currentKey); } and change <label t:type="Label" for="checkbox">${labelValue}</label> in order to display the values of my HashMap and pass the keys to the next page. Thanks a lot...Gillis

© 2022 - 2024 — McMap. All rights reserved.