I am getting a list of users from a MySQL database to display it in <mat-table>
using the dataSource
property:
The following is the TypeScript method getUnitData()
, where I am setting the dataSource value as the returned JSON array from PHP script:
export class RegistrationComponent implements OnInit {
dataSource: MatTableDataSource<units>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
constructor(private auth: AuthApiService) { }
ngOnInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
applyFilter(filterValue: string){
this.dataSource.filter = filterValue.trim().toLowerCase();
if(this.dataSource.paginator)
{
this.dataSource.paginator.firstPage();
}
}
getUnitData(){
this.auth.getUnitDataService().subscribe(
(data)=>
{
this.dataSource = new MatTableDataSource(data);
},
(error)=>
{
}
)
}
}
And here is the main method from the AuthApiService
:
getUnitDataService(){
if(this.checkIsLogin){
this.headerOptions.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
return this.http.get(this.globalVar.getUnitData,{
headers: this.headerOptions
});
}
else{
this.router.navigate(['/login']);
}
}
The PHP script is:
public function getUnitData($conn)
{
try{
$sql = "SELECT * FROM unit ORDER BY unit_id DESC";
$getData = $this->conn->prepare($sql);
$getDataResult = $getData->execute();
$getDataSize = $getDataResult->rowCount();
$getDataResult->fetchAll();
return $getDataResult;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
And then I am calling in another file using the following URL: this.globalVar.getUnitData
.
The errors I am getting is as the following:
1.
[ts] Cannot find name 'units'.
2.
Argument of type 'Object' is not assignable to parameter of type 'any[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead? Property 'length' is missing in type 'Object'.
I tried this solution on Stack Overflow, but still I get the same error.