tags:

views:

87

answers:

1

I've an h:dataTable to display my item information (in my case a customer support request) and for each row I'd like to put an h:inputText and h:commandButton to add a comment to the desired item. I've no problem with action, but my question is about the correct way to manage many h:inputText and related value on backbean.

I'm not sure that is correct to make all h:inputText set the value to the same backbean property. Any suggestions? Thanks all.

+2  A: 

Depends on how the form is composed.

If each row of the table represents one form (i.e. the h:form with h:inputText and h:commandButton is inside h:column), then it's technically no problem.

<h:dataTable value="#{bean.list}" var="item">
    <h:column>
        <h:form>
            <h:inputText value="#{bean.value}" />
            <h:commandButton value="Submit" action="#{bean.submit}" />
        </h:form>
    </h:column>
</h:dataTable>

The problem is only that you need to figure to which row the input was related. The f:setPropertyActionListener may be useful in this. But this approach hasn't my recommendation. Rather bind the input value (and if necessary also the action) to the iterated row object as declared in var attribute of h:dataTable instead. I.e. #{item.value} and #{item.submit}. Or, go for the approach described below.

If the whole table is placed inside a single form (i.e. the h:dataTable is inside h:form), then you'd better to set the value attribute of the h:inputText as a property of the iterated row object as declared in var attribute of h:dataTable.

<h:form>
    <h:dataTable value="#{bean.list}" var="item">
        <h:column><h:inputText value="#{item.value}" /></h:column>
    </h:dataTable>
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

When you set it as a backing bean property, i.e. #{bean.value}, it will always end up to be the value of the last row.

BalusC
Thank you BalusC, you are always so exhaustive (I read your blog), I have not thought of putting many forms. Now I try this hint, and also I add an h:inputHiddend for the row id. There are problems that I do not know to put a lot of forms?
Mirco Attocchi
Hi BalusC,Today I made some tests, but my idea yesterday to use an h: inputHidden was wrong I can not use it.I do not know the contraindications to use many h:form (or many h:form in to a main h:form)It was my intention to do something "clean" and do not use strange mechanisms, you believe that I can use without problems?I believe that my case is real, many rows displayed and the possibility for end-user to add a comment by row (not like in a blog where the comment is "posted" for the displayed item) and some others may have the same need, so I'm looking for the most elegant solution.
Mirco Attocchi
As stated in my answer, either use `f:setPropertyActionListener` to set the row object in question as managed bean property, or put the action method in the iterated row object itself.
BalusC