0
My doubt I believe is something simple, but I’m not getting to a logic..
I own a sales application, which has a list of products being returned from firebase, and each product of this has a checkbox next to it for marking. The logic is, the customer selects the product at the checkbox and fires an event that captures this product and I pass it by NAVPARAMS to the sales screen. So far so good.
The problem is this, there will be the case of the customer selecting more than one item at a time to make the sale to expedite the process. I understand that I would need to create an array and pass this array to the next screen. But I’m not able to do that, could someone give me some example of code or suggest some improvement ? I will leave down my code ts and my html.
TS:
//FUNÇÃO QUE CAPTURA O VALOR DO CHECKBOX E ARMAZENA NA VARIÁVEL PARA PASSAR COMO PARAMETRO
produtoEscolhido(produto, isChecked){
if(isChecked) {
this.produtosEscolhidos = produto;
let produtos = [this.produtosEscolhidos];
console.log(produtos);
}
}
HTML:
//LISTA DE PRODUTOS COM O EVENTO PRODUTO ESCOLHIDO QUE CAPTURA O ITEM QUE O USUÁRIO SELECIONOU
<ion-list *ngFor="let produto of produtosLista | async">
<ion-item>
<ion-label>{{produto.descricao}}</ion-label>
<ion-checkbox [(ngModel)]="produto.selecionado" (ionChange)="produtoEscolhido(produto, $event.checked)"></ion-checkbox>
</ion-item>
</ion-list>
<button ion-button color="dark" (click)="escolherProdutos()">Escolher</button>
</ion-content>
Renata I did as you passed there, but in this case what would be inside my ionchange? choose products(){ Let productsSchoped = []; for(Let i=0; i < this.productsLista.length; i++){ if (this.productsList[i].selected){ productsSchool.push(this.productsList[i]); console.log("The products chosen are " +this.productsList[i]); }Else{ console.log("error"); } } } In my console.log is not returning anything..
– Diego Estacho
You don’t need to include any function in your
ionchange
. As you will wait for the user to choose all the items he needs, the function would be, for example, onbutton
, that you already have after theion-list
.– Renata
Okay, I’m going to run some more tests. Following up on the code I sent up, should you return me something on the right console ? Or I did something wrong?
– Diego Estacho
I would make a small change, leaving the.log console after. choose Products(){ Let productsChoose = []; for(Let i=0; i<this.productsLista.length; i++){ if (this.productsList[i].selected){ productsChoose.push(this.productsLista[i]); } } console.log("The products chosen are " + products); }
– Renata
When I try to add the length property to the code, it returns the following syntax error : [ts] Property 'length' does not exist on type 'Observable<Products[]>'. E running the function on a test button, it does not call anything on the console. I believe he is not going through the list on account of this property. Anything I can do to resolve?
– Diego Estacho
I updated some things in my code : //A List is on the same sales screen, or no longer need to pass parameter, just show the items on a card and send to the database <ion-list *ngFor="Let product of productsLista | async"> <ion-item> <ion-label>{{product.Description}} {{product.preco}}</ion-label> <ion-checkbox></ion-checkbox> </ion-item> <button ion-button Outline color="dark" class="boot-add"> <ion-icon name="add"></ion-icon> </button> </ion-list>
– Diego Estacho