Primefaces - update datatable with commandButton doesn't work
Asked Answered
M

3

7

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;
        }
    }
Marroquin answered 17/11, 2011 at 22:18 Comment(0)
P
11

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();
Piled answered 18/11, 2011 at 12:48 Comment(1)
Thank you very much. I don't know why I forgot that. Reloading the list works great!Marroquin
S
1

Looks like the datatable is not refreshing, try using update=":userLists" or update=":lists:userLists", the ':' telling it to look for the id starting from root. Currently it looks like it cant find the id to update. Rather, put it in an p:outputPanel, give it an id and then do something like update=":panel". I guess that will work.

Swedenborgianism answered 18/11, 2011 at 10:59 Comment(0)
C
0

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

You should post the code from the method:userService.removeUserById(id); I can't tell what's happening in there but if you aren't updating the users list, the dataTable won't never be (successfully) updated!

Cathryncathy answered 18/11, 2011 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.