I have an api which send data from backend, so when I try to get this data in my angular the data is not loaded in the first time, and my table stay empty, but if for example print the data that come from banckend before setting it in my DataSource, my table is fill correctly.
My code is:
user-datasource.ts
export class UserListDataSource extends DataSource<User> {
data: User[];
paginator: MatPaginator;
sort: MatSort;
constructor(private usersService: UsersService) {
super();
}
connect(): Observable<User[]> {
this.data = this.usersService.usersList();
//this.data.forEach(u => console.log(u.id)); <<---------- When I log this, My data is fill corectly
const dataMutations = [
observableOf(this.data),
this.paginator.page,
this.sort.sortChange
];
return merge(...dataMutations).pipe(map(() => {
return this.getPagedData(this.getSortedData([...this.data]));
}));
}
...
}
userService
export class UsersService {
users: User[];
constructor(private http: HttpClient) {}
usersList() {
this.http.get(`${environment.apiUrl}/users`).pipe(
map((jsonArray: Object[]) => jsonArray.map(jsonItem => User.fromJson(jsonItem)))
).subscribe((users: User[]) => { this.users = users; });
return this.users;
}
}
component
export class UserListComponent implements AfterViewInit, OnInit {
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatTable) table: MatTable<User>;
dataSource: UserListDataSource = new UserListDataSource(this.usersService);
displayedColumns = ['id', 'email', 'username'];
constructor(private usersService: UsersService) { }
ngOnInit() {}
ngAfterViewInit() {
this.dataSource.sort = this.sort;
this.dataSource.paginator = this.paginator;
this.table.dataSource = this.dataSource;
}
}
I'm not sure if I use the correct way to fill my table, or I miss something.
From what I see, I think the backend took a long time than the frontend to load data, in another word, frontend is not waiting the necessary time to get data from backend.
Is there any way to fix this please?
Note: I use angular material schematics to create all this, in the default code, it use a static data, which is not the solution for me, which I need to use dynamic data.