1

i need to send every employee i checked to postCouncilAbsence function parameter, i want to make a loop to loop each employee i checked then send him as parameter .. any help using typescript?


component.ts:

onattendanceSave(form:NgForm){
    this.index = this.attendanceForm.value
    console.log(this.index);
    Object.keys(this.index).forEach( key => {
    this.dataStorageService.postCouncilAbsence(this.index,this.Id)
    .subscribe(
        response => {
            console.log('save'+ this.index);
        }),
        error =>{ 
            console.log('error');
        }
  });
}
onChange(attendance:string, isChecked: boolean) {
    const attendanceFormArray = 
    <FormArray>this.attendanceForm.controls.isAttend;
    if(isChecked) {
        attendanceFormArray.push(new FormControl(attendance));
    } else {
      let index = attendanceFormArray.controls.findIndex(x => x.value == attendance)
      attendanceFormArray.removeAt(index);
    }
}

component.html:

<form [formGroup]="attendanceForm" >
 <div class="row">
    <table class="table table-hover table-condensed text-center table-bordered">
        <thead>
            <tr>
               <th> attendances </th>
               <th> check </th>
             </tr>
         </thead>
         <tbody>
          <tr *ngFor="let attendance of attendances"  >
             <td hidden>{{attendance.Employee_ID}}</td>
             <td > {{attendance.Emp_Name}} </td>
              <td> 
                <div class="form-check">
                    <label class="form-check-label">
                      <input type="checkbox (change)="onChange(attendance.Employee_ID,$event.target.checked)" >                                                                              {{attendance.isAttend}}
                    </label>
                </div>
              </td>
            </tr>
        </tbody>
     </table>
   </div>
  <div class="modal-footer">
      <button type="submit" class="btn btn-success" (click)="onattendanceSave(attendanceForm.value)"> save </button>
  </div>
  </form>

i want to send ID of each employee to this function :

postCouncilAbsence(absence, userId){
    let url = 'http://api.azharcouncil.com/api/CouncilAbsences/PostCouncilAbsence?Council_Id='+13+'&Emp_Id='+absence+'&User_Id='+userId;
    let headers = new Headers({ 'Content-Type': 'text/plain' });
    let options = new RequestOptions({ headers: headers }); 
    return this.http.post(url, JSON.stringify(absence), options);
}
4
  • Where are you calling onattendanceSave()? Commented Oct 8, 2017 at 15:40
  • sorry, i edited my question Commented Oct 8, 2017 at 15:42
  • Why not create a variable in your *.ts file to manipulate during your call to onChange() and then use that when you call postCouncilAbsence()? Commented Oct 8, 2017 at 15:55
  • i don't understand ! :/ Commented Oct 8, 2017 at 15:56

1 Answer 1

2

It looks like you are already sending the ID back when you call onChange(). You could capture it then in a variable and then use it on when you call postCouncilAbsence()

component.ts

employeeId: string;

onChange(attendance:string, isChecked: boolean) {
  //your code
  this.employeeId = attendance;
}

postCouncilAbsence(absence, userId){
  const employeeId = this.employeeId; // don't need this but it's here to show you
  //your code and you can use this.employeeId
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.