Seeking guidance for updating a MatTable datasource within an Angular application. The current Typescript code involves fetching data from an API using AdminService, iterating over the results to make additional API calls for more information about a fleet.
The existing method successfully updates the datasource without any issues, even though there are complexities involved in acquiring the supplementary data.
Your insights and assistance on this matter would be highly appreciated.
refresh(): void {
const data = {};
const that = this;
this.adminService.fleets(data).subscribe(result => {
that.fleetTableData = [];
result.forEach(function (fleetObject){
if (typeof fleetObject.fleetTotals === 'undefined'){
fleetObject.fleetTotals = {};
fleetObject.fleetTotals.totalVehicles = 0;
fleetObject.fleetTotals.totalActiveVehicles = 0;
fleetObject.fleetTotals.totalStatusTestedVehicles = 0;
}
that.fleetTableData[fleetObject.fleetId] = fleetObject;
that.adminService.vehicles(
{fleetId: fleetObject.fleetId}
).subscribe(result2 => {
result2.forEach(function (vehicleForFleet) {
var fleetId = vehicleForFleet.fleetId;
that.fleetTableData[fleetId].fleetTotals.totalVehicles = (that.fleetTableData[fleetId].fleetTotals.totalVehicles + 1);
if (typeof vehicleForFleet.commissioning !== 'undefined') {
if (vehicleForFleet.commissioning.commissionStatus === 'tested') {
that.fleetTableData[fleetId].fleetTotals.totalStatusTestedVehicles = (that.fleetTableData[fleetId].fleetTotals.totalStatusTestedVehicles + 1);
}
if (vehicleForFleet.commissioning.commissionStatus === 'active') {
that.fleetTableData[fleetId].fleetTotals.totalActiveVehicles = (that.fleetTableData[fleetId].fleetTotals.totalActiveVehicles + 1);
}
}
});
});
});
that.dataSource.data = new MatTableDataSource(that.fleetTableData);
that.dataSource.sort = that.sort;
console.log(that.fleetTableData);
});
}