Cell edit in primefaces is not updating the value

♀尐吖头ヾ 提交于 2019-12-04 05:21:24

问题


I have a datatable in my primefaces application . The code for the frontend has

    <!-- Start of customer datatable -->
            <p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header">
                List of Customers
                    <p:outputPanel>
                        <p:commandButton value="+" type="button" onclick="addCustDlg.show()"/>
                     </p:outputPanel>
                </f:facet>


                <p:column sortBy="#{customer.id}" filterBy="#{customer.id}" update=":custList">
                    <f:facet name="header">
                        <h:outputText value="ID"/>
                    </f:facet>
                    <h:outputText value="#{customer.id}"/>
                </p:column>

                <p:column sortBy="#{customer.name}" filterBy="#{customer.name}" headerText="NAME" filterMatchMode="contains" update=":custList">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.name}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.description}" filterBy="#{customer.description}" headerText="DESCRIPTION">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.description}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.description}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.signupDate}" filterBy="#{customer.signupDate}" headerText="SIGN UP DATE">
                    <f:facet name="output">
                        <h:outputText value="#{customer.signupDate}"/>
                    </f:facet>
                </p:column>

                <p:column sortBy="#{customer.validUntil}" filterBy="#{customer.validUntil}" headerText="EXPIRY DATE">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.validUntil}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.validUntil}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.status}" filterBy="#{customer.status}" headerText="STATUS">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.status}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.status}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column headerText="CREATION DATE" sortBy="#{customer.creationDate}" filterBy="#{customer.creationDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.creationDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="LAST UPDATE DATE" sortBy="#{customer.lastUpdateDate}" filterBy="#{customer.lastUpdateDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.lastUpdateDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="Options">
                    <p:rowEditor/>
                </p:column>

            </p:dataTable>
            <!-- End of dataTable (customer datatable) -->

And the function for handling the rowEvent is specified in the bean as

 public void custRowEdit(RowEditEvent event){
    Customer cust = (Customer) event.getObject();
    EntityManagerHelper.beginTransaction();
    custDao.update(cust);
    EntityManagerHelper.commit();
}

However , on an update event , when I am editing the cell in the table , I do not get the new updated value of the attribute .

Like in the image below , when I edit the status of the entry with ID 1 from 11 to 4 , in the function custRowEdit , when I try to get the customer object , I still get the status of the customer as 11 and not 4 .

Can anyone help me with understanding why the value of the cell is not being set ?


回答1:


from Where You are invoking custRowEdit(RowEditEvent event) method. I have not any related thing in your code.

In order to make your listener invoke add below attribute in your datatable declaration.

rowEditListener="#{customerBean.listenerInBackingBean}"

<p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header"

rowEditListener="#{customerBean.cutRowEvent}"
>



回答2:


Check the implementation of customerBean.customers. I reloaded the content from the database every time the method got called. Wrong. This should happen in the constructor instead. Now everything works fine. Thought it was a JavaScript error ...




回答3:


Thanks this helped me.

May I add that instead of loading the list from a query in the constructor, if one has a @SessionScoped managed bean one can instead use a reset() method to reset lists to null and then lazily populate the lists from the query. The reset can then be called on page load using an f:event:

    <f:view>
        <f:metadata>
            <f:event type="preRenderView" listener="#{sessionScopedBean.reset}"/>
        </f:metadata>
    </f:view>        



回答4:


I encountered a similar situation that was resolved by removing the update="" tag from the dataTable. The table's default behavior is to update the row, which evidently, in my case, was not occurring.




回答5:


You are missin the p:ajax event to trigger the method, function or whatever you wanna do after the cell editing

it's something like

<p:ajax event="cellEdit" listener="#{mBean.onCellEdit}" update="elementX" />


来源:https://stackoverflow.com/questions/6365877/cell-edit-in-primefaces-is-not-updating-the-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!