Skip to content

Instantly share code, notes, and snippets.

@ssuperczynski
Last active October 17, 2018 15:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ssuperczynski/451d0109cbce87e8426a6af2965d8a0f to your computer and use it in GitHub Desktop.
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