I am trying to have filterable table. I have ListComponent like this:
export class CompanyListComponent implements OnInit {
companies$: Observable<Company[]>;
private searchTerms = new Subject<string>();
constructor(private companyService: CompanyService) { }
ngOnInit() {
this.companies$ = this.searchTerms.pipe(
// wait 300ms after each keystroke before considering the term
debounceTime(300),
// ignore new term if same as previous term
distinctUntilChanged(),
// switch to new search observable each time the term changes
switchMap((term: string) => this.companyService.searchCompanies(term)),
);
}
search(term: string): void {
this.searchTerms.next(term);
}
And my html looks like that:
<input #searchBox type="text" class="form-control" id="search-box" (keyup)="search(searchBox.value)">
<table class="table">
<tr *ngFor="let company of companies$ | async">
<td>{{company.name}}</td>
</tr>
</table>
When I am writing inside the search input, table is filtered as it should be. But when I refresh page there is no data in the table at all. It appears after I put something in the input. What should I do to get all table data when the page is loaded? I tried to just start search in ngOnInit but those tries went to nowhere.