0

I am working on a web application using jsf/ primefaces, netbeans and tomcat. I have a datatable with some values loaded in from another table and some editable fields. My question is, after the user has edited this table how do i submit the whole table so it can be stored in a database, in a new table?

 <h:form id="form" prependId="false">

                    <h3>All of your Paddocks</h3>
                    <p:dataTable var="paddock" value="#{paddock.getfromPaddock()}" editable="true">
                        <p:ajax event="rowEdit" listener="#{paddock.onRowEdit}"  />
                        <p:ajax event="rowEditCancel" listener="#{paddock.onRowCancel}"  />
                        <p:column headerText="Id">
                            <h:outputText value="#{paddock.idPaddock}" />
                        </p:column>

                        <p:column headerText="Name">
                            <h:outputText value="#{paddock.name}" />
                        </p:column>

                        <p:column headerText="Area">

                                 <h:outputText value="#{paddock.area}" />


                        </p:column>

                        <p:column headerText="Enter Grass Weight">
                         <p:cellEditor>

                                <f:facet name="output"><h:outputText value="0" /></f:facet>
                                <f:facet name="input"><p:inputText id="modelInput" value="0" style="width:100%"/></f:facet>
                            </p:cellEditor>
                        </p:column>

                        <p:column style="width:32px">
                            <p:rowEditor />
                        </p:column>



                    </p:dataTable>
                    <h:commandButton value="Log" action="#{paddock.add}" />

                </h:form>

Note that the only editable column is the grass column. enter image description here

Sean Sheehy
  • 89
  • 1
  • 13

1 Answers1

3

First of all, there are two main mistakes which may have undesired side effects:

  1. <h:form ... prependId="false">

    Never use prependId. Remove the whole attribute.

  2. <p:dataTable var="paddock" value="#{paddock.getfromPaddock()}">

    You should give var a different name than the managed bean. E.g. paddockItem.


As to the concrete question, your other mistake is here:

<p:inputText id="modelInput" value="0" />

You didn't bind the input value to the model. So JSF won't be able to update the model with the submitted values anyway.

Fix it accordingly, e.g.:

<p:inputText id="modelInput" value="#{paddockItem.grass}" />

In the submit method, it'll be right away there in the model.

You should only make absolutely sure that you aren't interacting with the database in the getter method of the <p:dataTable value>, otherwise you will be overwriting the model on every iteration round, hereby basically trashing the submitted values until the last row before the submit method is hit. The strange method name behind the data table value value="#{paddock.getfromPaddock()}" namely suggests that you're doing that. If it were a real property, the average starter would just have used value="#{paddock.fromPaddock}" or so.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @ BalusC, why not use `prependId` attribute ? – Chirag Patel Jul 26 '15 at 07:33
  • @ BalusC, hi again, thanks for the helpful tips, however i still think i wasn't clear in my question, so i added an image to further explain. Im aware of how mapping a field to a backing bean works, however in this instance(ie the image) the values are being displayed from the backing bean paddock, with the exception of the grass field which is editable. So my question is how do iterate through the jsf table above+ the grass value and submit it to a new bean, since i cant use #{paddock.idpaddock} since it is already mapped for displaying that field in output text? – Sean Sheehy Jul 26 '15 at 18:11
  • Then just create a new property, or wrap it in a new bean with that property, or create a mapping between old bean and new property. All boils down to that you should bind the input value in the view to a property in the model, irrespective of the way (and that your getter methods are free of business logic). JSF will then transparently take care of the remainder. – BalusC Jul 26 '15 at 18:12