Checkbox calling button with Ionic 2

Asked

Viewed 290 times

0

I’m simulating something similar to a shopping cart using ionic 2. Basically you write the item name and value and create a checkbox list as in the image below.

inserir a descrição da imagem aqui

But I wanted the option inserir a descrição da imagem aqui only appeared when selecting one of the checkbox and did not become static on the screen as it is now. How can I do this?

grid calling the Checkbox:

<ion-grid>
    <ion-row *ngFor="let item of produto">
      <ion-item>
        <ion-label (click)="clicou(item.desc)">
          {{ item.desc }} {{ item.valor }}
        </ion-label>
        <ion-checkbox checked="false"></ion-checkbox>
      </ion-item>
    </ion-row>
  </ion-grid>

part that arrow the button in the code:

<button ion-button block (click)="remove()" color="danger" style="transition: none 0s ease 0s;">
    <span class="button-inner">
      <ion-icon name="close"></ion-icon>
      Remover Selecionados
    </span>
    <div class="button-effect"></div>
  </button>
  • Managed to solve the problem?

  • @Viana part of him yes

1 answer

0


Add the (ionChange) calling a function and passing as parameter the $event and the item. Within its function it will be necessary to click on a vector the selected items and set the variable selecionado as true to show the button. After that, it is also necessary to check if any item has been deselected and it is necessary to remove the item from the array of selected items. If the vector is totally empty, it arrow the variable bool selecionado as false and then the button disappears.

In its component.ts

export class componente {
  private produtos: any = [
    {nome: "sabao", valor: "10"},
    {nome: "miojo", valor: "12"},
    {nome: "pasta", valor: "15"},
    {nome: "cup nudes", valor: "14"}
  ]
  private itensSelecionados: any [] = [];
  private selecionado: boolean = false;
  constructor(){}

  datachanged(e:any, item: any){
    if(e.checked){
      for (let x = 0; x < this.produtos.length; x++) {
        if(this.produtos[x].nome == item.nome){
          this.itensSelecionados.push(item);
          this.selecionado = true;   
        }      
      }
    }
    else{
      for (let x = 0; x < this.itensSelecionados.length; x++) {
        if(this.itensSelecionados[x].nome == item.nome){
          this.itensSelecionados.splice(x,1);
          if(this.itensSelecionados.length == 0){
            this.selecionado = false;
            return false;
          }
        }        
      }
    }
  }

On your grid.


    <ion-content padding>
  <ion-grid>
    <ion-row *ngFor="let item of produtos">
      <ion-item>
        <ion-label>
          {{ item.nome }} {{ item.valor }}
        </ion-label>
        <ion-checkbox checked="false" (ionChange)="datachanged($event, item)"></ion-checkbox>
      </ion-item>
    </ion-row>
  </ion-grid>
</ion-content>

On your button


 <ion-footer>
  <button *ngIf="selecionado" (click)="remove()" ion-button block color="danger" style="transition: none 0s ease 0s;">
    <span><ion-icon name="close"></ion-icon>Remover Selecionados</span>
  </button>
</ion-footer>

This solves your problem, just adjust your code.

  • Hello friend, I copied and pasted as Oce did but my button is not showing when I select the checkbox

  • your version of the angular?

    1. I took what you sent and put it on top of my old code
  • could post as is your . ts?

  • https://codepen.io/anon/pen/QBYyOY I put it here just to show you how it is

  • could visualize the code?

  • yes, I did. I’ll post the solution over the night.

  • I’ll be waiting, buddy!

  • Good morning my dear. I’m sorry I didn’t fix it yesterday, I was overwhelmed. I edited the solution, give a look. Now I certify that works, because I tested it here. Any doubt ask me.

  • I don’t know why it’s not working. I’m a beginner, I’m copying and pasting what you say, but it still doesn’t work. now it simply appears products in the list and does not erase

  • just copy and paste really won’t work. I changed it here according to your code. Try it there. https://codepen.io/anon/pen/zLbXEV

  • in your ai it really excludes the selected? even with your code here, it only removes in order. What I added first goes out and so on

  • good.. the variable itensSelected receives the items to be deleted from your list. From this variable I check if this empty (does not show the delete button) and if you have something (shows the delete button). This solves your problem of showing or not the button according to your checkbox.

  • Your delete button calls the Remove() method. Within its function it removes the selected items from its product variable. Within this function, you need to check if there is something within the variable and compare if that item exists within the product. If it exists, you eliminate it.

  • @hunterxhunter all right there?

  • @jrspante to trying to mount the condition in remove() that you commented above

Show 12 more comments

Browser other questions tagged

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