jeudi 28 octobre 2021

angular : Limit number of checkbox selections

I'm trying to limit base input checkbox

In the code, the limit is informed in ( this.bases.length === 2 ) is work but after the function work I can't update the checkbox

enter image description here

I tried the code but did not works.

HTML :

<div class="form-group row line">
        <div class="p-field-checkbox col-lg-6" *ngFor="let base of listBases">
            <p-checkbox name="group1" value="base" [value]="base" [(ngModel)]="bases (change)="functionBase($event)" [disabled]="checked"  inputId="sv" ></p-checkbox>
            <label for="sv"></label>
        </div>
</div>  

TS :

public checked: boolean = false
public numberOfBase: number = 0
public totalOfBase: number = 0
public bases: string[] = []
public listBases = ['Salade verte','Pate', 'Riz', 'Sans base']

functionBase(e:any){
if (e.target.checked === true){
      this.numberOfBase ++
      if (this.bases.length === 2){
        this.checked = true
      } else { 
              this.checked = false 
             }
    } else {
      this.numberOfBase --
    } 
}

The




Aucun commentaire:

Enregistrer un commentaire