Primefaces数据表,删除行后不更新

时间:2013-06-27 15:59:37

标签: primefaces datatable row delete-row

我正在尝试从我的primefaces数据表中删除一行,但在刷新浏览器之前它永远不会更新。

我正是这样做的:

<p:column headerText="Eliminar Usuario" rendered="#{user.current.tipo.equalsIgnoreCase('planta')}">
<p:commandButton  action="#{usuarios.eliminar(o.idUsuario)}" value="Eliminar" update="@form"/>
</p:column>

我已经尝试将数据表包装到h:panelgroup中,我也尝试使用update =“:formUsuarios:users”(我的表单的id是formUsuarios,表的id是用户)。我在commandButton中输入“ajax =”true“,没有任何效果。

我不知道还有什么可以尝试的。我正在使用primefaces 3.5。

这是我的豆子:

@ManagedBean(name = "usuarios")
@ViewScoped
public class UsuariosBean {
    @ManagedProperty(value="#{user}")
    private LoginBean loginBean;
    private List<UsuariosEntity> usuarios;
    UsuariosEntity user=new UsuariosEntity();
    private String nombre;
    private int activo;
    private String tipo;
    private String password;
    private String selection;
    private UIComponent mybutton;
    @PostConstruct
    public void init(){

        UsuariosEntity usuario=loginBean.getCurrent();
        usuarios=new ArrayList<UsuariosEntity>();
        if(usuario.getTipo().equalsIgnoreCase("Planta")){
        usuarios= UsuariosDAO.getALL();
        }else{
            usuarios= UsuariosDAO.getALLbyRol(usuario.getUsuario());
        }
    }

    public UIComponent getMybutton() {
        return mybutton;
    }

    public void setMybutton(UIComponent mybutton) {
        this.mybutton = mybutton;
    }

    public String getSelection() {
        return selection;
    }

    public void setSelection(String selection) {
        this.selection = selection;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getActivo() {
        return activo;
    }

    public void setActivo(int activo) {
        this.activo = activo;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public LoginBean getLoginBean() {
        return loginBean;
    }

    public void setLoginBean(LoginBean loginBean) {
        this.loginBean = loginBean;
    }

    public UsuariosEntity getUser() {
        return user;
    }

    public void setUser(UsuariosEntity user) {
        this.user = user;
    }

    public List<UsuariosEntity> getUsuarios() {
        return usuarios;
    }

    public void setUsuarios(List<UsuariosEntity> usuarios) {
        this.usuarios = usuarios;
    }
    public void newUsuario(){
        UsuariosEntity user=new UsuariosEntity();
        user.setIdPlanta(2);
        user.setActivo(this.activo);
        user.setPassword(this.password);
        user.setUsuario(this.nombre);
        user.setTipo(this.selection);

        List<UsuariosEntity>usuariox=UsuariosDAO.findbyname(this.nombre);

            if (usuariox.size()==0){
                UsuariosDAO.save(user);
            }else{
                FacesMessage message = new FacesMessage("El usuario ya existe en la base de datos");
                FacesContext context = FacesContext.getCurrentInstance();
                context.addMessage(mybutton.getClientId(context), message);
            }
    }

    public void eliminar(int id_user){
        UsuariosEntity user=UsuariosDAO.find(id_user);
        UsuariosDAO.eliminar(user);
    }



    public void onEdit(RowEditEvent event) {
        UsuariosDAO.save(((UsuariosEntity)event.getObject()));
        FacesMessage msg = new FacesMessage("Usuario Editado", ((UsuariosEntity) event.getObject()).getUsuario());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edición Cancelada", ((UsuariosEntity) event.getObject()).getUsuario());

        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

观点:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
                 xmlns:f="http://java.sun.com/jsf/core"
                 template="/Contenido/Template.xhtml"
                 xmlns:p="http://primefaces.org/ui">

 <ui:define name="page-content">


    <h:form id="formUsuarios">

        <p:growl id="messages" showDetail="true"/>
        <p:dataTable id="users" value="#{usuarios.usuarios}" var="o" editable="true"  scrollRows="20" scrollable="true" scrollHeight="150" paginator="true" rows="10"
                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                     rowsPerPageTemplate="5,10,15">

    <f:facet name="header">Usuarios</f:facet>
            <p:ajax event="rowEdit" listener="#{usuarios.onEdit}" update=":formUsuarios:messages" />
            <p:ajax event="rowEditCancel" listener="#{usuarios.onCancel}" update=":formUsuarios:messages" />

            <p:column headerText="#" style="width:6%">

        <h:outputText value=" #{o.idUsuario} "/>
    </p:column>

    <p:column  rendered="#{user.current.tipo.equalsIgnoreCase('planta')}" headerText="Nombre">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{o.usuario}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{o.usuario}" style="width:100%"/>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column headerText="Nombre" rendered="#{user.current.tipo.equalsIgnoreCase('productor')||user.current.tipo.equalsIgnoreCase('exportadora')}">
        <h:outputText value="#{o.usuario}" />
    </p:column>

    <p:column rendered="#{user.current.tipo.equalsIgnoreCase('planta')}" headerText="Estado" style="width:6%">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{o.activo}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{o.activo}" style="width:100%"/>
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Password">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText  value="********"  />
            </f:facet>
            <f:facet name="input">
                <h:inputSecret value="#{o.password}" style="width:100%"/>
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column rendered="#{user.current.tipo.equalsIgnoreCase('planta')}" headerText="Tipo">
        <p:cellEditor>
            <f:facet name="output">
                <h:outputText value="#{o.tipo}" />
            </f:facet>
            <f:facet name="input">
                <p:inputText value="#{o.tipo}" style="width:100%"/>
            </f:facet>
        </p:cellEditor>
    </p:column>
            <p:column headerText="Editar" style="width:6%">
                <p:rowEditor />
            </p:column>
            <p:column headerText="Eliminar Usuario" rendered="#{user.current.tipo.equalsIgnoreCase('planta')}">

                <p:commandButton  actionListener="#{usuarios.eliminar(o.idUsuario)}" value="Eliminar" process="users" ajax="true"/>

            </p:column>

        </p:dataTable>

        <br/>
    </h:form>
        <h:form>
        <p:panelGrid id="grid" columns="3" rendered="#{user.current.tipo.equalsIgnoreCase('planta')}">
            <f:facet name="header">
                Crear Usuario
            </f:facet>
            <h:outputText value="Nombre"/>
         <h:inputText id="nombre" label="name" value="#{usuarios.nombre}" required="true" requiredMessage="Debe ingresar un nombre"/>
          <h:message for="nombre" style="color:red"/>
         <h:outputText value="Contraseña" />
         <h:inputText id="pass" label="pasw" value="#{usuarios.password}" required="true" requiredMessage="Debe ingresar una contraseña"/>
         <h:message style="color:red" for="pass"/>
        <h:outputText value="Tipo"/>

            <h:selectOneMenu id="select1" value="#{usuarios.selection}" onchange="submit();" required="true" requiredMessage="Debe seleccionar un tipo de usuario">
                <f:selectItem itemValue="Planta" itemLabel="Planta" />
                <f:selectItem itemValue="Exportadora" itemLabel="Exportadora" />
                <f:selectItem itemValue="Productor" itemLabel="Productor" />
            </h:selectOneMenu>

            <h:message style="color:red"  for="select1"/>
        <h:outputText value="Activo"/>

                <h:selectOneMenu id="select2" value="#{usuarios.activo}" onchange="submit();" required="true" requiredMessage="Debe seleccionar una opción ">
                    <f:selectItem itemValue="0" itemLabel="Inactivo" />
                    <f:selectItem itemValue="1" itemLabel="Activo" />
                 </h:selectOneMenu>
                <h:message style="color:red"  for="select2" />

            <f:facet name="footer">
            <h:commandButton value="Crear" id="mybutton"
                             binding="#{usuarios.mybutton}"
                             class="button" action="#{usuarios.newUsuario}">
            </h:commandButton>
            <h:message style="color:red"  for="mybutton" />
            </f:facet>
         </p:panelGrid>

    </h:form>

 </ui:define>
</ui:composition>

1 个答案:

答案 0 :(得分:3)

更改value引用的集合不会立即影响数据表,因为显示的值由过滤后的集合存储。

1)如果指定filteredValue属性,则应在数据表内容更改后手动清除该集合,例如在删除行之后。在数据表上更新后,它应该刷新内容。

2)您可以在dataTable小部件(客户端API)上调用filter()。它总是有效,但它会产生额外的AJAX请求。