Delete records from selected checkboxes with angular

Asked

Viewed 97 times

0

I need to create a function that deletes records according to the checkbox(s) selected(s). I was able to select these Ids for each checkbox and include them in the array "Selected" through function alternating(), but now I need to create a function to exclude these ids stored in "Selected" which correspond to main array records "people".

html

<tr *ngFor="let pessoa of pessoasArray; let i = index">
                <th><input type="checkbox" name="" (click)="alternaSelecao(pessoa)"/></th>
                <!-- <th>{{pessoa.$key}}</th> -->
                <td>{{ i+1 }}</td>
                <td>{{pessoa.nome}}</td>
                <td>{{pessoa.email}}</td>
                <td>{{pessoa.tipo}}</td>
                <td>{{pessoa.datanasc}}</td>
                <td>

class

export class PessoaComponent implements OnInit {

  constructor(public bd: Bd) { }

  selected = [];
  public pessoasArray

  ngOnInit() {
    this.bd.getPessoas().subscribe(
      list => {
        this.pessoasArray = list.map(item => {
          return {
            $key: item.key,
            ...item.payload.val()
          };

        });
      });    

  }

alternaSelecao(pessoa) {
    let idx = this.selected.indexOf(pessoa);
    if(idx > -1){
      this.selected.splice(idx,1);
    }
    else {
      this.selected.push(pessoa);
    }     
  }

service comic:

export class Bd {

constructor(private firebase: AngularFireDatabase) { }
    public pessoasList: AngularFireList<any>;

    public getPessoas() {
    this.pessoasList = this.firebase.list('pessoa');
    return this.pessoasList.snapshotChanges();

    }

1 answer

0

I’m not sure I fully understand your question, but if you’re just trying to clear the array Selected Then it’s up to you:

Method :

clearArray(){
     this.selected.splice(0,myArray.lenght);
}

or you can choose other variations such as:

while(myArray.lenght >  0 ){
   myArray.pop();
}

Or

myArray.lenght = 0;
  • I’m actually managing to clear the array Selected. What I need is to use these ids contained in Selected to compare and delete with your correspondents in the bank. In ngOnInit i do a firebase subscribe received on people that I use to show in HTML. I just added the service for better understanding.

Browser other questions tagged

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