Thang Pham
Thang Pham

Reputation: 38705

Bizarre bug: inputTextarea only hook its value to the last row in dataTable

The bug is the inputTextarea I have below. Only the last row can record any input from the inputTextarea, in other word, only the last row, hook it value to #{PostComment.comment.comment}. Why is this?

<h:form id="userCommentList" >
   <p:dataTable value="#{CentralFeed.profileComments}" var="item">
        <p:column>        
           <!-- Original Comment -->         
           <h:outputText value="#{item.comment}"/> &nbsp;              

           <!-- ****BUG IS THIS INPUTTEXTAREA BELOW*** -->
           <h:inputTextarea id="keyword" value="#{PostComment.comment.comment}"/> &nbsp;

           <p:commandButton actionListener="#{PostComment.postProfileComment(item.id)}"
                                             value="Post" update="userCommentList" />
        </p:column>                                                                                   
   </p:dataTable>
</h:form>

EDIT I change the inputTextarea and commandButton like you suggest BalusC. And inside Comment entity I add in another field call newComment, so Comment look like this

Comment
+ id
+ comment
+ newComment --> I have @Transient to this field so it wont map to the database. I also set its default value to the empty string in the constructor
+ ...

...
<p:column>
    ...
    <h:inputTextarea id="keyword" value="#{item.newComment}" rows="2" />
    <p:commandButton actionListener="#{PostComment.postReply(item)}" value="Post" />
</p:column>

I hope the item.newComment would hold the value I just type in, so when I pass the object item to postReply, I can extract the content of newComment out, however it is an empty string. So whatever I type in does not bind to newComment. Any idea why?

Upvotes: 2

Views: 267

Answers (1)

BalusC
BalusC

Reputation: 1108722

You're in essence binding the value of multiple textareas in a table to one and same bean property. During request processing JSF will update this property with the value of all textareas in the table, each overriding the previous one everytime until with the one in the last row. That's why you will see only the value of one in the last row.

You need to bind the textarea value to the row object, which is #{item} in your case. E.g.

<h:inputTextarea id="keyword" value="#{item.newComment}"/>

Upvotes: 2

Related Questions