how to update a data table by cleaning the records by clicking on a button?

1

I have the following code which is inside of a p: fieldset, they are two inputs one for the year and another for the month, and a search button, what it does is filter records according to those two parameters

<p:inputText id="anio" style="width:100px;" value="#{RegistroAccesoComponent.annio}"></p:inputText>

 <p:inputText  style="width:100px;" value="#{RegistroAccesoComponent.mes}"></p:inputText>
 
 <p:commandButton   value="Buscar" icon="ui-icon-search"  styleClass="element-button green"  style="margin-right: 15em;"  id="buscar" 
                                           action="#{RegistroAccesoComponent.buscaDetalleIncidencia()}" update="consultaIncidencia,detalle" process="@form"  >
                        </p:commandButton>

which sends to call the following method

public void buscaDetalleIncidencia() {
        System.out.println("ENTRO AL METODO BUSCA DETALLE POR AÑO/MESSS");

        incidenciaAnioMes = incidenciaAnioMesService.consultarIncidenciasAnioMes(new IncidenciaAnioMes(annio, mes, new Persona(cvePersona)));

        incidenciaAnioMesDet = anioMesDetService.findIncidenciaAnioMesDeByAnioMes(cvePersona, annio, mes);

        fechasIncidencias = crearFechaIncidencia(incidenciaAnioMesDet, annio, mes);

    }

What it does is that when you click on the button, it shows a table with all the days of the month that you entered in the input of the month, for example if you put the number 3, then it shows all the days of the month of March, if you entered the 2, shows every day of the month of February and so

I fill the table with the list "datesIncidences" which is inside the method,

<p:dataTable id="detalle" var="detalle" value="#{RegistroAccesoComponent.fechasIncidencias}" emptyMessage="No se encontraron registros." selectionMode="single" selection="#{RegistroAccesoComponent.fechaIncidencia}" rowKey="#{detalle.fecha}">

  <p:ajax event="rowSelect" listener="#{RegistroAccesoComponent.seleccionaFecha}" update=":form:carDetail" oncomplete="PF('modalIntentos').show();" />
  <p:column headerText="FECHA" style="width: 100px;">
    <h:outputText value="#{detalle.fecha}">
      <f:convertDateTime type="date" pattern="dd/MM/yyyy" />
    </h:outputText>
  </p:column>

  <p:column headerText="ENTRADA" style="width: 100px;">
    <h:outputText value="#{RegistroAccesoComponent.horaEntradaBiometrico}">
      <f:convertDateTime type="date" timeZone="CST" pattern="HH:mm:ss" />
    </h:outputText>

  </p:column>
  <p:column headerText="SALIDA" style="width: 100px;">
    <h:outputText value="#{detalle.incidencia.horaSalida}">
      <f:convertDateTime type="date" timeZone="CST" pattern="HH:mm:ss" />
    </h:outputText>
  </p:column>
  <p:column headerText="INCIDENCIA">
    <h:outputText value="#{detalle.incidencia.incidencias.cveIdIncidencia} - #{detalle.incidencia.incidencias.concepto}" />
  </p:column>
  <p:column headerText="ACCIÓN" style="width: 100px;">
    <h:outputText styleClass="ui-corner-all ui-icon ui-icon-search"></h:outputText>
  </p:column>
</p:dataTable>

to that list I pass a method, which is composed as follows

public List<Date> crearFechas(int annio, int mes) {
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(annio, mes - 1, 1);

    List<Date> fechas = new ArrayList<>();
    while (calendar.get(Calendar.MONTH) == mes - 1) {
        fechas.add(calendar.getTime());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    return fechas;
}

public List<FechaIncidencia> crearFechaIncidencia(List<IncidenciaAnioMesDet> incidenciaAnioMesDet, int annio, int mes) {
    List<Date> fechas = crearFechas(annio, mes);

    Calendar calendar = Calendar.getInstance();
    for (Date fecha : fechas) {
        FechaIncidencia fechaIncidencia = new FechaIncidencia();
        fechaIncidencia.setFecha(fecha);
        calendar.setTime(fecha);
        for (IncidenciaAnioMesDet incidencia : incidenciaAnioMesDet) {
            if (calendar.get(Calendar.DAY_OF_MONTH) == incidencia.getDia()) {
                fechaIncidencia.setIncidencia(incidencia);

                break;
            }
        }
        fechasIncidencias.add(fechaIncidencia);
    }
    return fechasIncidencias;
}

What I do is create a list of dates type Date from the month and the year that are entered in the input

then when I enter the year 2018 and month 3 and I give it in the search button, I paint the table with all the days of the month

but when I change the month, for example 2 and I give it in search again, if it paints me every February day, but paints them in a row

So, I want the table to be updated when I do a new search, that is to erase the data of the previous month and paint only the new ones How can I update the table?

    
asked by Root93 22.03.2018 в 17:23
source

1 answer

2

You must clean or initialize the list ( DatesIncidences ), you keep the previous result because you are not cleaning the list, add the following line:

fechasIncidencias = new ArrayList<>();

You can put it in the method SearchDetailIncidence () :

public void buscaDetalleIncidencia() {
        System.out.println("ENTRO AL METODO BUSCA DETALLE POR AÑO/MESSS");
        fechasIncidencias = new ArrayList<>(); //Aqui

        incidenciaAnioMes = incidenciaAnioMesService.consultarIncidenciasAnioMes(new IncidenciaAnioMes(annio, mes, new Persona(cvePersona)));

        incidenciaAnioMesDet = anioMesDetService.findIncidenciaAnioMesDeByAnioMes(cvePersona, annio, mes);

        fechasIncidencias = crearFechaIncidencia(incidenciaAnioMesDet, annio, mes);

}

or within the method createDetailFace () :

public List<FechaIncidencia> crearFechaIncidencia(List<IncidenciaAnioMesDet> incidenciaAnioMesDet, int annio, int mes) {
    List<Date> fechas = crearFechas(annio, mes);
    fechasIncidencias = new ArrayList<>(); //Aqui

    Calendar calendar = Calendar.getInstance();
    for (Date fecha : fechas) {
        FechaIncidencia fechaIncidencia = new FechaIncidencia();
        fechaIncidencia.setFecha(fecha);
        calendar.setTime(fecha);
        for (IncidenciaAnioMesDet incidencia : incidenciaAnioMesDet) {
            if (calendar.get(Calendar.DAY_OF_MONTH) == incidencia.getDia()) {
                fechaIncidencia.setIncidencia(incidencia);

                break;
            }
        }
        fechasIncidencias.add(fechaIncidencia);
    }
    return fechasIncidencias;
}
    
answered by 22.03.2018 в 17:56