Search code examples
jsfprimefacesdatatableajax-update

Primefaces - update datatable with commandButton doesn't work


I'm facing some problems by updating a datatable with a 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 clicking the commandButton that performs the action listUsersController.deleteUser. The method is successfully executed and the user is deleted. But the datatable is not updated. I want the deleted record to not appear anymore in the list with ajax.

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

This is the method in the 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;
        }
    }

Solution

  • You need to reload the users from the DB after deletion.

    So, instead of alone

    userService.removeUserById(id);
    

    you should do

    userService.removeUserById(id);
    users = userService.list();