Search code examples
javaarraylisttapestry

Tapestry 5 - Edit arraylist of Strings


I have a Result object (named result), and inside that object is an arraylist of strings (named action), as well as some other values.

I can make a text area to edit values of the Result object like this:

<input t:type="TextArea" t:id="feedback" t:value="result.someValue" /> 

This works fine. However, I would like to show a text field for each of the Strings in the ArrayList within the result object

I can create a loop like this:

<t:loop t:source="result.action" t:value="currentAction" index="indexProp" t:formstate="ITERATION">
   ${currentAction}
</t:loop>

This will show me on the screen all of the actions (that is great, half way there). However I want these to be editable using a TextField.

I have tried several things, none of which have worked how I wanted. However, to help explain and as an example of what I have tried, this is what I have:

<t:loop t:source="result.action" t:value="currentAction" index="indexProp" t:formstate="ITERATION">
<input t:type="TextField" t:value="result.action.indexProp"/>
</t:loop>  

This won't work because (as far as I know), this is the same as getResult().getAction.getIndexProp. So I tried

<input t:type="TextField" t:value="result.action.${indexProp}"/>

This doesn't work either, although it shows the correct number of TextFields, it does not link them up properly (they just say inside them "result.action.0" and "result.action.1".

Any help is much appreciated.


Solution

  • You could try:

    <input t:type="TextField" t:value="result.action[indexProp]"/>
    

    Not sure if tapestry is smart enough to determine getters and setters for individual list elements... probably not.

    If that doesn't work, you can add a getter/setter to your page so that tapestry can create an appropriate PropBinding

    Java

    @Property
    private int indexProp;
    
    @Property
    private SomeObject result;
    
    public void setCurrentAction(String action) {
        result.actions.set(indexProp);
    }
    
    public String getCurrentAction() {
        return result.actions.get(indexProp);
    }
    

    TML

    <t:loop t:source="result.action" t:index="indexProp" t:formstate="ITERATION">
        <input t:type="TextField" t:value="currentAction"/>
    </t:loop>