Skip to content

Instantly share code, notes, and snippets.

@navanathjadhav
Created December 26, 2020 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save navanathjadhav/2b8f8332fa9f1583b51bb9dff91455fa to your computer and use it in GitHub Desktop.
Save navanathjadhav/2b8f8332fa9f1583b51bb9dff91455fa to your computer and use it in GitHub Desktop.
6. Added search & records per page input
constructor(private fb: FormBuilder, private cdr: ChangeDetectorRef) {}
// Defaults
form!: FormGroup;
students: any = [];
searchTerm: string = '';
reload: EventEmitter<boolean> = new EventEmitter();
isLoadingStudents: boolean = false;
recordsPerPage: number = 5;
// On init
ngOnInit(): void {
this.buildForm();
}
// On After view checked, detect changes manually
ngAfterViewChecked(): void {
this.cdr.detectChanges();
}
// Init form
buildForm() {
this.form = this.fb.group({
term: ['', [Validators.required]],
recordsPerPage: [''],
});
}
// On form submit => assign search term
submitForm(): void {
if (this.form.invalid) {
return;
}
this.searchTerm = this.form.value.term;
}
// Clear search results on search box empty
clearSearchResult() {
if (this.form.controls.term.value === '' && this.searchTerm) {
this.searchTerm = '';
setTimeout(() => {
this.reload.emit(true);
this.reload.emit(false);
}, 100);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment