1
votes

I have a p: datatable using rowedit and works fine, but when you delete an item datatable not updated.

@Named(value = "localesMB")
@SessionScoped
public class LocalesMB implements Serializable {
  /**
  * Creates a new instance of Activos
  */
  public LocalesMB() {
  }

  private List<entidades.Locales> localesAll = new ArrayList<>();
  private entidades.Locales selected = new Locales();

  public Locales getSelected() {
      return selected;
  }

  public void setSelected(Locales selected) {
    this.selected = selected;
  }


  public List<Locales> getLocalesAll() {
    return localesAll;
  }

  public void setLocalesAll(List<Locales> localesAll) {
    this.localesAll = localesAll;
  }

  public void deleteSelect(Locales locales) {
    this.selected = locales;
  }

  public void obtenerLocalesAll() {
    try {

        FacesContext ctx1 = FacesContext.getCurrentInstance();
        ValueExpression vex1 = ctx1.getApplication().getExpressionFactory().createValueExpression(ctx1.getELContext(), "#{controladorMB}", ControladorMB.class);
        ControladorMB cn = (ControladorMB) vex1.getValue(ctx1.getELContext());
        this.localesAll = cn.getInterfaces().obtenerLocalesAll();

    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: obtenerActivosAll() " + e.getMessage());
    }
  } 

  public void eliminarLocal() {
    try {
        FacesContext ctx1 = FacesContext.getCurrentInstance();
        ValueExpression vex1 = ctx1.getApplication().getExpressionFactory().createValueExpression(ctx1.getELContext(), "#{controladorMB}", ControladorMB.class);
        ControladorMB cn = (ControladorMB) vex1.getValue(ctx1.getELContext());
        cn.getInterfaces().eliminarLocal(this.selected);
        this.localesAll = cn.getInterfaces().obtenerLocalesAll();
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: eliminarLocal() " + e.getMessage());
    }
  }  

  public void onRowEdit(RowEditEvent event) {
    try {
        Locales ps = (Locales) event.getObject();
        FacesContext ctx1 = FacesContext.getCurrentInstance();
        ValueExpression vex1 = ctx1.getApplication().getExpressionFactory().createValueExpression(ctx1.getELContext(), "#{controladorMB}", ControladorMB.class);
        ControladorMB cn = (ControladorMB) vex1.getValue(ctx1.getELContext());
        cn.getInterfaces().modificarLocal(ps);
        this.localesAll = cn.getInterfaces().obtenerLocalesAll();            
        JsfUtil.addSuccessMessage("Se ha cambiado el valor correctamente.");
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, "Error: onRowEdit() " + e.getMessage());
    }
  }
}

xhtml

 <h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="./../resources/css/default.css" rel="stylesheet" type="text/css" />
    <link href="./../resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
    <title>Activos</title>
    <script language="javascript">
        window.onload = function() {
            PF('tb1').clearFilters();
        };
    </script>
</h:head>

<h:body>
 <h:form id="frm1">
        <p:growl id="growl" sticky="true" showDetail="true"/>  
        <p:panel header="Locales e Instalaciones" id="pn1">              
            <p:dataTable  emptyMessage="No se encontraron elementos"  editable="true" rowKey="#{item.idLocales}"
                          id="tabla_listado" var="item"  paginator="true"  widgetVar="tb1"
                          rows="25"  value="#{localesMB.localesAll}"> 

                <f:facet name="header">
                    <h:outputText value="Locales e Instalaciones: #{localesMB.localesAll.size()}"/>
                </f:facet>

                <p:ajax event="rowEdit"  listener="#{localesMB.onRowEdit}"/>  

                <p:column headerText="Descripción">  
                    <p:cellEditor>  
                        <f:facet name="output">  
                            <h:outputText value="#{item.descripcion}" />
                        </f:facet>  
                        <f:facet name="input">  
                            <p:inputText value="#{item.descripcion}" style="width:95%"/>  
                        </f:facet>  
                    </p:cellEditor>  
                </p:column> 
                <p:column headerText="Ubicación">  
                    <p:cellEditor>  
                        <f:facet name="output">  
                            <h:outputText value="#{item.ubicacion}" />
                        </f:facet>  
                        <f:facet name="input">  
                            <p:inputText value="#{item.ubicacion}" style="width:95%"/>  
                        </f:facet>  
                    </p:cellEditor>  
                </p:column>                 

                <p:column style="width:20px">  
                    <p:rowEditor/>  
                </p:column> 
                <p:column style="width:20px">  
                    <p:commandButton id="selectButton256" oncomplete="PF('confirmation').show()" process="@this, tabla_listado"  actionListener="#{localesMB.deleteSelect(item)}" 
                                     icon="ui-icon-trash" title="Eliminar" update=":frm1:confirmDialog">
                    </p:commandButton>  
                </p:column>        
            </p:dataTable>  

        </p:panel>
        <p:confirmDialog appendTo="@(body)" id="confirmDialog" message="Confirma que desea eliminar a: #{localesMB.selected.descripcion}"  
                         header="Mensaje de Confirmación" severity="alert" widgetVar="confirmation" closeOnEscape="true">  
                <p:commandButton id="confirm" value="Sí" update=":frm1"  oncomplete="PF('confirmation').hide()"  
                                 actionListener="#{localesMB.eliminarLocal}" process="@this" styleClass="ui-confirmdialog-yes" icon="ui-icon-check"/>  
                <p:commandButton id="decline" value="No" onclick="PF('confirmation').hide()" type="button" styleClass="ui-confirmdialog-no" icon="ui-icon-close"/>   
        </p:confirmDialog>  
    </h:form>      
1
@JaqenH'ghar You are right, I didn't look at the code properly.user2880020
@OP, post the code and/or test your application scoped bean's methods (cn.getInterfaces().eliminarLocal(this.selected); this.localesAll = cn.getInterfaces().obtenerLocalesAll();). But first of all, is the eliminarLocal() method getting called ?user2880020
eliminarLocal() is called on confirmDialog´yes button, after datatable is updated, but number of row no change.meyquel
Post the code, or check the size of the list before and after the operation, do you see any difference in the backing bean ?user2880020
I check de list in datatable header, look: <f:facet name="header"> <h:outputText value="Locales e Instalaciones: #{localesMB.localesAll.size()}"/> </f:facet>meyquel

1 Answers

0
votes

that the problem, this code active the filter, but datatable dont have filterValue:

   <script language="javascript">
        window.onload = function() {
            PF('tb1').clearFilters();
        };
    </script>

DataTable frm1:tabla_listado has filtering enabled but no filteredValue model reference is defined, for backward compatibility falling back to page viewstate method to keep filteredValue. It is highly suggested to use filtering with a filteredValue model reference as viewstate method is deprecated and will be removed in future.

Solution: remove script.