tags:

views:

147

answers:

1

In JSF, we can bind HtmlDataTable to backing bean and get the row data. But ui:repeat doesn't even have a binding attribute. So, how do I know which row (element) is clicked in ui:repeat?

+4  A: 

Either use f:setPropertyActionListener

<h:form>
    <ui:repeat value="#{bean.items}" var="item">
        <h:outputText value="#{item.value}">
        <h:commandButton value="submit" action="#{bean.submit}">
            <f:setPropertyActionListener target="#{bean.item}" value="#{item}"/>
        </h:commandButton>
    </ui:repeat>
</h:form>

with

private List<Item> items;
private Item item;

public void submit() {
    System.out.println(item);
}

Or just put action method in iterated item

<h:form>
    <ui:repeat value="#{bean.items}" var="item">
        <h:outputText value="#{item.value}">
        <h:commandButton value="submit" action="#{item.submit}" />
    </ui:repeat>
</h:form>

Either case, you need to ensure that the same items is preserved in subsequent request.

Both ways by the way also just works in a h:dataTable.

BalusC