vendredi 7 septembre 2018

how to accept default multiple checked checkbox in Angular and send to database ?

I have multiple checked checkbox which comes in(ngOninit) now i want to edit and make to some checkbox unchecked and some checkbox remains checked but and some times i dont want to make change, but (this.checkedlist is always comes empty) kindly help me for this

<label *ngFor="let statusCategoryObj of statusCategoryObj" style="padding-left: 20px;">
 <mat-checkbox value="" name="categories"
   [checked]="checkedCategories(statusCategoryObj.id)" (change)="onCheckboxChange(statusCategoryObj,$event)">
    </mat-checkbox>
</label>

edit-status.component.ts

ngOnInit(){
  this.statusService.editStatusDetailsById({'id': id}).subscribe(
    (data) => {
      if(data.status == 28){
        this.editStatusObj.id = data.payload[0].id;
        this.editStatusObj.categories = data.payload[0].categories;
        this.allCategories = this.editStatusObj.categories.split(',');
      }        
    }
  )
}

checkedCategories(id){
  for(var i = 0 ; i < this.allCategories.length; i++){
    if(id == this.allCategories[i]){
      return true;
    }
  }  
  return false;
}

onCheckboxChange(statusCategoryObj, event) {
  if(event.checked) {
    this.checkedList.push(statusCategoryObj.id);
  }else{
    for(var i=0 ; i < this.statusCategoryObj.length; i++) {
     if(this.checkedList[i] == statusCategoryObj.id){
       this.checkedList.splice(i,1);
     }
    }
  }
}

updateStatusDetailsById(id){
  const formData = new FormData();
  formData.append('categories',this.checkedList.toString());
  this.statusService.updateStatusDetailsById(formData).subscribe(
    (data) => {
      if(data.status == 29){
        console.log(data.payload);
      }  
    }
  )
} 




Aucun commentaire:

Enregistrer un commentaire