Search code examples
jsffacelets

How do I know which element is clicked in ui:repeat?


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?


Solution

  • 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.