kramsay
kramsay

Reputation: 55

How to submit specific iterator entry to action in Struts 2

I'm working on a marketplace to purchase virtual items, and I'm planning on having it display the details of each item in rows, with a "Buy" button next to each item. Here's what I have:

<s:form method="post">
    <s:hidden name="shownType"/>
    <table>
        <tr>
            <td>Name</td>
            <td>Image</td>
            <td>Type</td>
            <td>Price</td>
            <td>Buy</td>
        </tr>
        <s:iterator value="items" var="item">
            <tr>
                <td><s:property value="name" /></td>
                <td><s:property value="image" /></td>
                <td><s:property value="type" /></td>
                <td align="center"><s:property value="price" /></td>
                <td><s:submit value="Buy"
                onclick="coinAlert()" action="Buy"
                type="button" theme="simple">
                </s:submit></td>
            </tr>
        </s:iterator>
    </table>
</s:form>

Theoretically, I want to pass the specific Item object (name, image, etc.) to the Buy action (specifically, to an Item named boughtItem). The problem is, I'm not sure how to refer to the specific item next to the clicked button. I've tried

<s:hidden name="boughtItem.name" value="%{#item.name}"/>

but that sets the boughtItem's name to the names of all the items in the List, separated by commas. Any attempt to set the entire boughtItem object at once using the iterator has failed.

Any insight on this would be appreciated.

Upvotes: 1

Views: 3730

Answers (1)

Anupam
Anupam

Reputation: 8016

Instead of using a submit button use a simple button and submit the form using javascript

<s:form method="post" action="Buy" name="myForm">
    <s:hidden name="name" id='name_to_submit'/>
    <s:hidden name="image" id='image_to_submit'/>
    <s:hidden name="type" id='type_to_submit'/>
    <s:hidden name="price" id='price_to_submit'/>
    <table>
        <tr>
            <td>Name</td>
            <td>Image</td>
            <td>Type</td>
            <td>Price</td>
            <td>Buy</td>
        </tr>
        <s:iterator value="items" var="item">
            <tr id="<s:property value="name" />">
                <td><s:property value="name" /></td>
                <td class='image_td'><s:property value="image" /></td>
                <td class='type_td'><s:property value="type" /></td>
                <td class='price_td' align="center"><s:property value="price" /></td>
                <td><input type="button" value="Buy"
                onclick="submitForm('<s:property value="name" />')">
                </td>
            </tr>
        </s:iterator>
    </table>
</s:form>

JavaScript

function submitForm(name){
   //All this would be very easy if you were using jQuery
   var nameElem = document.getElementById(name);
   //get values for selected row
   var imageVal = nameElem.getElementsByClassName("image_td")[0].innerHTML;
   var priceVal = nameElem.getElementsByClassName("price_td")[0].innerHTML;
   var typeVal = nameElem.getElementsByClassName("type_td")[0].innerHTML;
    //set the values to submit
   document.getElementById("name_to_submit").value = name;
   document.getElementById("image_to_submit").value = imageVal;
   document.getElementById("price_to_submit").value = priceVal;
   document.getElementById("type_to_submit").value = typeVal;

   document.myForm.submit(); //finally submit the form
}

It will be better to use some identifier property as id not the name

Upvotes: 1

Related Questions