CheckBox Angular 4 ngfor problem when selecting

3

I am designing a Modal with boostrap and inside it I have a checkBox list that is loaded by running an NGFOR.

The problem is the following: At the time of selecting one of those elements all the checkboxes are selected, besides I do not have very clear how to receive in information of the checkbox selections in the component.ts

How can I choose only the values I want? And also send the information to my component?

This is my code:

<div class="modal-body">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-6 col-lg-6 col-6" *ngFor="let data of sensor">
        <input class="form-check-input" type="checkbox" id="inlineCheckbox1" value="data.name" name= "miDataInterior" [(ngModel)]="miDataInterior">
        <label class="form-check-label" for="inlineCheckbox1">{{data.name}}</label>
      </div>
    </div>
  </div>

</div>

EDIT01

Hello,

The information I'm painting in the ngfor is the following:

["var_TempExt","var_HumRExt","var_HumRInt","var_Humedad_Sust1","var_Humedad_Sust2","var_PotActInv","var_PotActBio","var_ContadorRiego","var_RadiacionGlobalInt","var_RadiacionPARInt","var_CO2InvVaisala","var_TempInt","var_RadiacionExt","var_CO2Exterior","var_VVExt","var_DVExt","var_Lluvia","act_AbrirVentanaCen","act_AbrirVentanaLatNS","act_Aerotermo","act_BombaCalefaccion","act_Deshumificacion","act_Humidification","act_LuzArtificial","act_ValvulaTresVias"]

I need to be able to select any of those names and send that name in an array to my component and then pass them to the service.

In the ts I have a simple method to show me the information that the modal has, but I only recover a true

public guardar(){
      console.log('formulario posteado: ' + this.checkbox);
    } 
    
asked by Manolait 25.07.2018 в 13:12
source

2 answers

0

My idea to solve the problems:

Create a structure in the component with the fields of the checkboxes:

//asumo que sensor es el array ["var_TempExt", "var_HumRExt", "var_HumRInt", ... ]
checkboxes= {};
sensor.forEach(nombre => checkboxes[nombre]=false);

With which you would have an object with attributes called like every checkbox initialized to false.

And then you do something like:

<div class="col-md-6 col-lg-6 col-6" *ngFor="let data of sensor; let i= index">
    <input class="form-check-input" type="checkbox" name= "miDataInterior" 
        [(ngModel)]="checkboxes[data.name]" id="{{'inlineCheckbox' + i}}">

    <label class="form-check-label" for="{{'inlineCheckbox' + i}}">{{data.name}}</label>
</div>
    
answered by 25.07.2018 / 13:35
source
0
  

TL; DR: You're linking miDataInterior to all checkboxes

To solve it there are an infinite number of possible solutions. What you have to understand is that when you do [(ngModel)]="miDataInterior" in each checkbox, you are linking that variable to all of them. If miDataInterior changes, they will all change.

A simple solution is not to use the link, but the DOM events. When a user changes the value of the checkbox, add or remove that element to an array that will be the result. This is achieved with the event (change) and check if the target.checked of the event is active:

<input class="form-check-input" type="checkbox" 
  name= "miDataInterior" (change)="$event.target.checked ? agregar(data) : quitar(data)">

And in the component:

  agregar(data: string) { // Agregamos el elemento
    this.miDataInterior.push(data);
  }

  quitar(data) { // Filtramos el elemento para que quede fuera
    this.miDataInterior = this.miDataInterior.filter(s => s !== data);
  }

This way you can use arrays of strings or any type of data. You can find the solution working in this Stackblitz . There you can see that it also works with arrays of objects.

You can surely find other solutions, but you must understand that each checkbox has to have its variable separately.

    
answered by 27.07.2018 в 00:35