使用CommandButton更新数据表

使用CommandButton更新数据表

本文介绍了Primefaces-使用CommandButton更新数据表不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用commandButton更新数据表面临一些问题。这是xhtml文件:

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>

问题是单击执行操作listUsersController.deleteUser的命令按钮时。该方法已成功执行,并且删除了用户。但是数据表未更新。我希望删除的记录不再出现在使用Ajax的列表中。

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.

我已经使用 update = @ form update = @ parent update = @ all update = usersList update =:list:usersList 无效。

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

这是managedBean中的方法:

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;
        }
    }


推荐答案

您删除后需要从数据库中重新加载个用户

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

因此,而不是一个人

userService.removeUserById(id);

您应该这样做

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

这篇关于Primefaces-使用CommandButton更新数据表不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:15