Data compared within an array

Asked

Viewed 32 times

0

So I’m making a screen where the person puts their name and date of birth, adds in an Array, but now I want the person to choose to list by name or date of birth, but I don’t know how to use my filter.value to compare sort by date

<mat-form-field  appearance="fill">
    <mat-label>Nome</mat-label>
    <input matInput [(ngModel)]="registroIncluido.nome"  placeholder="Digite seu Nome">
  </mat-form-field>

  <mat-form-field appearance="fill">
    <mat-label>Choose a date</mat-label>
    <input matInput [(ngModel)]="registroIncluido.data" [matDatepicker]="picker">
    <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
    <mat-datepicker #picker></mat-datepicker>
  </mat-form-field>

  <button mat-icon-button (click)="adicionaNaLista()">
    <mat-icon>add</mat-icon>
  </button>

  <mat-form-field appearance="fill">
    <mat-label>filtro</mat-label>
    <mat-select [(ngModel)]="filtro" (selectionChange)="eventSelection($event)">
      <mat-option *ngFor = "let filtro of filtros" [value]="filtro.value">
        {{filtro.label}}
      </mat-option>
    </mat-select>
  </mat-form-field>


  <mat-list role="list">
    <mat-list-item role="listitem" *ngFor = "let registro of listaRegistros">
        {{registro.nome}}
        {{registro.data | date:'MM/dd/yyyy'}}
    </mat-list-item>
  </mat-list>

----------------------------------------------------------------------------------------

import { Component, OnInit } from '@angular/core';
import * as moment from 'moment';

@Component({
  selector: 'app-tarefadois',
  templateUrl: './tarefadois.component.html',
  styleUrls: ['./tarefadois.component.scss'],
})
export class TarefadoisComponent implements OnInit {

  listaRegistros: Registro[] = [];
  registroIncluido: Registro = new Registro();
  filtros: any[] = [];
  filtro: any

  constructor() { }

  ngOnInit(): void {
    this.filtros = [
      {label:'Nome',value:'NOME'},
      {label:'Data',value:'DATA'},
    ]
  }

  adicionaNaLista(){
    this.listaRegistros.push(this.registroIncluido);
    this.registroIncluido = new Registro();
    console.log(this.listaRegistros)
  }

  eventSelection(event:any){
    this.filtro = event.value
    return;
   }


}

export class Registro {
  nome: string = '';
  data: Date = new Date();

 /* constructor(nome: string, data: Date){
    this.nome = nome;
    this.data = data;
  }*/
}
  • Hello Jean, welcome to the site. It is important [Edit] and add a [mcve] of the problem (DO NOT delete and DO NOT repeat the question), with a step by step of what has already done and explain clearly and objectively and then wait for the reopening process (which will be evaluated by other users). To better enjoy the site, understand and avoid closures is worth reading the Stack Overflow Survival Guide in English. Thank you for understanding.

No answers

Browser other questions tagged

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