Primefaces - updating datatable with commandButton does not work

I am facing some problems updating datatable with commandButton. This is the xhtml file:

<div class="grid_16"> <h:form id="list"> <p:messages></p:messages> <p:dataTable styleClass="result-table" var="user" id="usersList" value="#{listUsersController.users}" widgetVar="userTable" paginator="true" rows="10" paginatorAlwaysVisible="false"> <f:facet name="header"> Listado de usuarios </f:facet> <p:column> <f:facet name="header"> <h:outputText value="#{cms['users.username']}" /> </f:facet> #{user.username} </p:column> <p:column> <f:facet name="header"> <h:outputText value="#{cms['users.name']}" /> </f:facet> #{user.name} </p:column> <p:column> <f:facet name="header"> <h:outputText value="#{cms['users.lastname']}" /> </f:facet> #{user.lastname} </p:column> <p:column> <f:facet name="header"> <h:outputText value="#{cms['users.active']}" /> </f:facet> #{user.active} </p:column> <p:column> <f:facet name="header"> <h:outputText value="#{cms['general.actions']}" /> </f:facet> <p:commandButton value="Eliminar" image="ui-icon-trash" actionListener="#{listUsersController.deleteUser}" update="list:usersList"> <f:param name="user" value="#{user.id}" /> </p:commandButton> </p:column> </p:dataTable> </h:form> </div> 

The problem is when you press the commandButton button, which performs the listUsersController.deleteUser action. The method completed successfully and the user was deleted. But the data is not updated. I want the remote entry to no longer appear in the list using ajax.

I already tested with update="@form" , update="@parent" , update="@all" , update="usersList" , update=":list:usersList" and nothing works.

This is the method in managedBean:

 public String deleteUser() { try { FacesContext fc = FacesContext.getCurrentInstance(); Map<String, String> params = fc.getExternalContext().getRequestParameterMap(); int id = Integer.parseInt(params.get("user")); userService.removeUserById(id); FacesContext.getCurrentInstance().addMessage (null,new FacesMessage(FacesMessage.SEVERITY_INFO,MessageProvider.getMessageProvider() .getValue("cms","users.error.userDoesNotExist"),"")); return SUCCESS; } catch (EntityNotFoundException e) { FacesContext.getCurrentInstance().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,MessageProvider .getMessageProvider().getValue("cms","users.error.userDoesNotExist"),"")); return ERROR; } } 
+6
source share
3 answers

You need to restart users from the database after uninstalling.

So instead of one

 userService.removeUserById(id); 

you have to do

 userService.removeUserById(id); users = userService.list(); 
+9
source

It seems you are deleting the user in db if you are using it, and not from the list - #{listUsersController.users} ...

You must publish the code from the method: userService.removeUserById(id); I can’t say what happens there, but if you are not updating the list of users, the DataTable value will never (successfully) be updated!

0
source

It seems that datatable is not refreshing, try using update = ": userLists" or update = ": lists: userLists", ':' so that it indicates id starting with root. Currently, it seems that it cannot find the identifier for the update. Rather, put it in p: outputPanel, give it an identifier, and then do something like update = ": panel". I think it will work.

0
source

Source: https://habr.com/ru/post/901760/


All Articles