0

I've got a problem getting a row element from a table in JSF. My goal is to create a table with elements taken from a database (order entity) which has some attributes (name, address, description etc.). All of that with a button on all single rows.

The task of the button is to delete the particular order from the database onClick, so i need to get the id of the order on the particular row.

I found somewhere on the way how to do it using setPropertyActionListener(). The problem is it works only for the first element of the table. If I click on the button assigned to another row but not first, the selectedId of the order is empty (not assigned).

I would really appreciate any help.

<h:form>
    <h:dataTable id="orders" value="#{orderController.otherOrders}" var="order" border="2"
                 cellspacing="1" cellpadding="1">
        <h:column>
            <c:facet name="header">Name</c:facet>
            #{order.name}
        </h:column>
        <h:column>
            <c:facet name="header">Address</c:facet>
            #{order.address}
        </h:column>
        <h:column>
            <c:facet name="header">Description</c:facet>
            #{order.description}
        </h:column>
        <h:column>
            <c:facet name="header"> </c:facet>
            <h:form>
                <h:commandButton action="#{orderController.selectOrder}" value="delete">
                    <f:setPropertyActionListener target="#{orderController.selectedOrderId}" value="#{order.id}" />
                </h:commandButton>
            </h:form>
        </h:column>

    </h:dataTable>
</h:form>

In orderController I have function selectOrder() that takes actual selectedOrderId and deletes the order.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • 1
    There are a number of ways to do this - as described here https://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ Your particular example looks mostly fine on first glance. However, you cannot have nested form tags - this is very likely the cause of your problems. It's not allowed in HTML and not allowed in JSF either. I would also suggest looking at your data source to check everything actually has an ID assigned. – Adam Waldenberg Feb 12 '19 at 06:16
  • 1
    Consequently, this is most likely a duplicate of https://stackoverflow.com/questions/7371903/how-to-use-hform-in-jsf-page-single-form-multiple-forms-nested-forms – Adam Waldenberg Feb 12 '19 at 06:21
  • Possible duplicate of [How to use in JSF page? Single form? Multiple forms? Nested forms?](https://stackoverflow.com/questions/7371903/how-to-use-hform-in-jsf-page-single-form-multiple-forms-nested-forms) – Kukeltje Feb 12 '19 at 08:03
  • Wow! I am so sorry! I was looking for a tutorials how to implement those buttons and I would never say that, it is because of the nested forms! All I had to do was deleting nested form and everything is now perfect! Sorry for a kind of duplicate. Should I delete this question? – M4rty32 Feb 12 '19 at 10:25
  • @M4rty32Yes I would say you can delete it, it doesn't add anything to the site. – Adam Waldenberg Feb 16 '19 at 12:34

0 Answers0