Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        }
    }
like image 562
jacruzca Avatar asked Nov 17 '11 22:11

jacruzca


2 Answers

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();
like image 76
BalusC Avatar answered Nov 04 '22 07:11

BalusC


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.

like image 38
Khizar Avatar answered Nov 04 '22 07:11

Khizar