K
K
Kirill_mont2021-05-03 20:10:27
Java
Kirill_mont, 2021-05-03 20:10:27

How to get a specific loop element?

In short, I'm currently trying to work with jstl, I have a foreach loop that creates product cards according to a template: Name, price, submission time, Add to cart button. So, when the button is clicked, information about the product is written to the sheet for further processing, but now I can only get the 1st element in this way. The next question is how to get the element on the card of which the button was pressed? For example, 5th.

I read about varStatus, but did not quite understand how to apply it.

<section>
    <form action="Controller" method="post">
    <sql:query dataSource = "${snapshot}" var = "result">
        SELECT * from menu where category='alcohol';
    </sql:query>
    <c:forEach var = "row" items = "${result.rows}">
        <div class="product-item">
            <div class="product-list">
                <h3><c:out value = "${row.name}"/> per bottle</h3>
                <input type="hidden" name="name" value="${row.name}"/>
                <span class="price">Price: $<c:out value = "${row.price}"/> </span>
                <input type="hidden" name="price" value="price">
                <span class="time">Filing time: <c:out value = "${row.time}"/>min</span>
                <input type="hidden" name="command" value="addtocart" />
                <input type="submit" class="button" value="Add to cart">
            </div>
        </div>
    </c:forEach>
    </form>
</section>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-05-03
@Kirill_mont

After the template was created with the template engine and given to the client, Java, jsp, etc. did not play a role. Further, using js, you can get the element that was clicked and I'd elements, for example, using ajax to pass to the backend. This requires knowledge of Javascript. Perhaps using jquery

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question