I am trying to implement a table with a input filter in a Angular 11 project. The table is populated by a SERVICE:
export class Service{
url='myUrl';
http;
constructor(private http:HttpClient){
this.http=http
}
loadAll(): Observable<Author[]> {
return this.http.get<Author[]>(this.url)
}
in my component I have a variable "filteredAuthors$" that will represent all data showed in table.
export class MyComponent{
authors$:Observable<Author[]>
filteredAuthors$:Observable<Author[]>
//form that receive the input filter
filterForm:FormControl
constructor(private service:Service){
this.authors$=service.loadAll()
filterForm=new ForControl('')
this.filteredAuthors$=filterForm.valueChanges.pipe(
startWith('').withLatestFrom(this.authors$),
map(
([typedValue, authors])=>!typedValue ? authors :
authors.filter(author=>author.name.includes(typedValue))
)))}}
With this code, I always start my table with 0 rows and after I type something on input, then the filter works great. Another weird thing is that when I replace my service with a mock observable created with "of" method, then the table start with all rows and is filtered after the first input in filter. What I am not seeing? The author type is:
export type Author={
id:string,
name:string
}