mardi 7 août 2018

how to delete multiple items with checkbox in ionic 3

I would like to know how to delete multiple items from the list in Ionic 3, using CheckBox, here is my code, in the delete () function I do not know how to only exclude the products that are checked with the checkbox. I thought that this would work but did not delete anything . If anyone can help me, thank you.

produto.html

<ion-item-sliding *ngFor="let produto of produtos">
      <ion-item>
        <input [(ngModel)]="modelo[produto.id]" value="" name="modelo" type="checkbox" ng-checked="produto.checked">
        <h1> </h1>
        <h3> </h3>
        <h3> </h3>
        <!--<h2 class="price"></h2> -->
      </ion-item>

    </ion-item-sliding>
  </ion-list>
  <button ion-button (click)="excluir()">Excluir Todos</button>


import { Component } from '@angular/core';
import { DatabaseProvider } from '../../providers/database/database';
import { SQLiteObject } from '@ionic-native/sqlite';
import { NavController, NavParams, ToastController } from 'ionic-angular';
import { InventarioProvider, Product } from '../../providers/inventario/inventario';
import { RestProvider } from '../../providers/rest/rest';
import { HomePage } from '../home/home';


@Component({

  selector: 'page-inventario',
  templateUrl: 'inventario.html',
})
export class InventarioPage {

    produtos: any[] = [];
      modelo: any[] = [];

    this.model = new Product();

        if (this.navParams.data.id) {
          this.inventarioProvider.get(this.navParams.data.id)
            .then((result: any) => {
              this.model = result;
            })
        }
      }
      ionViewDidEnter() {
        this.getAllProdutos();
      }

      getAllProdutos() {
        this.inventarioProvider.getAll()
          .then((result: any[]) => {
            this.produtos = result;
          });
      }

      excluir() {
         this.produtos.forEach(function (produto) {
  if (produto.checked) {
    this.modelo[produto.id] = produto.checked;
    return this.dbProvider.getDB()
      .then((db: SQLiteObject) => {
        let sql = 'delete from inventario where id = ?';
        let data = [produto.id];
        return db.executeSql(sql, data)
      })
  }
})

} }




Aucun commentaire:

Enregistrer un commentaire