How to delete multiple items from the bd with checkbox marked in Ionic 3?

Asked

Viewed 66 times

0

I would like to know how to delete several items when the checkbox is marked, in sqlite, with Ionic 3, in the delete() function I do not know how to delete only products that are marked with checkbox. I thought that this way that I’m trying to do, it would work, but it didn’t delete anything. If anyone can help me, thank you.

code html product.

<ion-item-sliding *ngFor="let produto of produtos">
      <ion-item>
        <input [(ngModel)]="modelo[produto.id]" value="{{produto.id}}" name="modelo" type="checkbox" ng-checked="produto.checked">{{produto.id}}
        <h1> {{ produto.barra }}</h1>
        <h3> {{ produto.deposito_nome }}</h3>
        <h3> {{ produto.quantidade }}</h3>
        <!--<h2 class="price">{{ produto.preco | currency:'BRL':true }}</h2> -->
      </ion-item>

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

product ts.

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)
      })
  }
})
  • let data = [produto.id]; So you’re not assigning an array to date? I believe the correct one would be let data = produto.id

  • 1

    Thank you, I believe that might be it

  • Caroline, I’m going to post as an answer.

1 answer

0

So you are assigning an array to date

let data = [produto.id];

I believe the right thing would be

let data = produto.id

Browser other questions tagged

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