Abhijit C
Abhijit C

Reputation: 313

Use struts2 iterator or initialize arraylist

I have a List. The user can add n item number and description.

He will add first and then to add second will press add more button .

I am giving a call to action class storing the data in array and redirecting back to the page to get the next record.

When I am using the iterator the text boxes are getting added again.

If I dont use iterator my arraylist is getting initailized everytime I visit the action class.

I cannot make it static.It will helpful if anyone can help me either to not initailize the arraylist everytime or use iterator and not repeat the textfields.

<table>
  <tr align="center">
    <s:iterator value="preAdviceDetailsDO" id="preAdviceDetailsDO" status="outerStat">
      <s:if test="#outerStat.may be some useful word like even odd first == true">
        <td style="background: #CCCCCC">
          <s:textfield value="%{itemNumber}" name="preAdviceDetailsDO[%{#outerStat.index}].itemNumber" onblur="checkBarcode();"/>
        </td>

        <td style="background: #CCCCCC">
          <s:textfield value="%{itemDescription}" name="preAdviceDetailsDO[%{#outerStat.index}].itemDescription"/>
        </td>
      </s:if>
    </s:iterator>
  </tr>     
</table>
<input type="button" value="Add More" onclick="addRow()" />

Upvotes: 1

Views: 571

Answers (1)

Abhijit C
Abhijit C

Reputation: 313

Thanks Dave Newton for editing. MohanaRao SV, I found the solution on it actually its a tweak.The problem I was facing in the above code was :

When the user see the form for the first time he will see two text field.He will enter the details and to add another record he will press add button. This time the arraylist is incremented. So the text fields on the form will increase to four.That is two earlier one and the new two text fields.And so on...

So now what I did is in the if condition I am checking if the index is last then I am displaying the fields and in the else part I am writing the same code for adding text field but I have set the css property display:none.

If I dont write the else part the arraylist is properly incremented but the details in the arraylist are not persisted.

So now only the required text fields are displayed.

Upvotes: 1

Related Questions