Last active
October 17, 2018 15:55
-
-
Save ssuperczynski/451d0109cbce87e8426a6af2965d8a0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OnInit } from '@angular/core'; | |
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; | |
export interface CommonAddress { | |
id: number; | |
name: string; | |
} | |
export const FORM_PARAMS = { | |
supervisors: 'supervisors', | |
}; | |
export class RecruitmentEditComponent implements OnInit { | |
public recruitmentForm: FormGroup; | |
constructor(private fb: FormBuilder) {} | |
ngOnInit() { | |
this.recruitmentForm = this.fb.group({ | |
supervisors: this.fb.array([]), | |
}); | |
} | |
get supervisors(): FormArray { | |
return this.recruitmentForm.get(FORM_PARAMS.supervisors) as FormArray; | |
} | |
findBy<C, K extends keyof C>(collection: C[], key: K, value: C[K]): boolean { | |
return collection.some((item) => item[key] === value); | |
} | |
selectSupervisor(item: any) { | |
const defined = this.findBy(this.supervisors.getRawValue(), 'id', item.id); | |
if (!defined) { | |
this.supervisors.push(new FormControl({ | |
id: item.id, | |
name: item.name, | |
})); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment