With making a typescript composite filter?

Asked

Viewed 189 times

0

I need to make a filter composed by the status and dateInicio of an object.

The section below is just an abstraction of what came to mind, would be correct or is there a better way to implement?

Array:any = [
  {"titulo" = "test1", "dataInicio" = "12/02/2019", "status" = "aguardando"},
  {"titulo" = "test2", "dataInicio" = "13/02/2019", "status" = "iniciada"},
  {"titulo" = "test3", "dataInicio" = "14/02/2019", "status" = "finalizada"}
  ];

filtroStatus: string = null; //aguardando, iniciada, finalizada
filtroData: Date = new Date();


getFiltro(){
  let statusArray = this.Array.filter(d =>d.status === this.filtroStatus);
  let retornoArray =  [];
    for(var element in statusArray){
      let dt = new Date.(element.dataInicio);
      if(dt === filtroData){
        retornoArray.push(element);
    }

  Return retornoArray
  }
}

2 answers

0

Array:any = [
  {titulo: "test1", dataInicio: "12/02/2019", status:"aguardando"},
  {titulo: = "test2",  dataInicio:"13/02/2019", status:"iniciada"},
  {titulo: "test3",  dataInicio: "14/02/2019", status:"finalizada"}
];

filtroStatus: string = null; //aguardando, iniciada, finalizada
filtroData: Date = new Date();
listaFiltrada (){        
 return this.Array.filter(d =>d.status === this.filtroStatus && d.dataInicio === this.filtroData );
  }
}
  • This way you can already filter by status and date at a single time.

-1


With the help of the answers here and a few more internet searches, I’ve added a few more things to the filter. Follow code.

I accept suggestions for improvements and corrections.

    this.tarefas = [
  { titulo: "test1", dataInicio: "12/02/2019", status: "aguardando" },
  { titulo: = "test2", dataInicio: "13/02/2019", status: "iniciada" },
  { titulo: "test3", dataInicio: "14/02/2019", status: "finalizada" }
];

filtro() {

  if (this.tarefas.length === 0 && this.filtroData === 'todas' || this.filtroStatus === null && this.filtroData === 'todas'
    || this.filtroStatus === undefined && this.filtroData === 'todas') {
    return this.tarefas;
  }

  let filtroTarefa = [];
  let filtro = [];

  if (this.filtroStatus === null || this.filtroStatus === undefined) {

    filtroTarefa = this.tarefas;
  } else {

    filtroTarefa = this.tarefas.filter(t => t.status === this.filtroStatus);
  }

  if (this.filtroData === 'todas') {
    filtro = filtroTarefa;
  }

  if (this.filtroData === 'hoje') {
    filtro = this.diaAtual(filtroTarefa);
  }

  if (this.filtroData === 'mes')
    filtro = this.mesAtual(filtroTarefa);

  if (this.filtroData === 'semana') {
    filtro = this.semanaAtual(filtroTarefa);
  }

  return filtro;
}

diaAtual(arr: any): any {
  let hoje = new Date().toLocaleDateString();
  return arr.filter(d => new Date(d.dataInicio).toLocaleDateString() == hoje);
}

mesAtual(arr: any): any {
  let mes = new Date().getMonth();
  return arr.filter(d => new Date(d.dataInicio).getMonth() == mes);
}

semanaAtual(arr: any): any {

  let [start, end] = this.dataSemanaAtual();
  return arr.filter(d => new Date(d.dataInicio) >= start
    && new Date(d.dataInicio) <= end);
}

dataSemanaAtual() {

  let now = new Date();
  let dayOfWeek = now.getDay(); //0-6
  let numDay = now.getDate();

  let start = new Date(now); //copy
  start.setDate(numDay - dayOfWeek);
  start.setHours(0, 0, 0, 0);


  let end = new Date(now); //copy
  end.setDate(numDay + (7 - dayOfWeek));
  end.setHours(0, 0, 0, 0);

  return [start, end];
}

getStatus() {

  if (this.tarefas.length === 0 || this.filtroStatus === null || this.filtroStatus === undefined) {
    return this.tarefas;
  }

  let filtroTarefa = this.tarefas.filter(t => t.status === this.filtroStatus);
  return filtroTarefa;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.