对象在填充 html table 时是一个数组,但在打字稿中迭代时不是
Object is an array when populating html table but not when iterating in typescript
我有一个后端数据库,将学生记录作为 JSON 文件提供。然后将这些对象填充到 PrimeNG table 中以供显示和选择。
student.component.html
<p-table class="table" [columns]="cols" [value]="searchResults">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData>
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of columns">
{{rowData[col.field]}}
</td>
</tr>
</ng-template>
</p-table>
然而,当我尝试在 TypeScript 中读取或遍历学生记录时,我得到 "TypeError: Cannot read property XXX of undefined"。
student.component.ts
export class StudentComponent implements OnInit {
searchResults: Student[];
cols: any[];
constructor(private router: Router, private studentService: StudentService, private http: HttpClient) {
}
ngOnInit(): void {
this.studentService.getStudents().subscribe(data => {
this.searchResults = data;
});
// table columns
this.cols = [
{ field: 'studentId', header: 'Student ID'},
{ field: 'name', header: 'Name'},
{ field: 'dob', header: 'Date of Birth'},
{ field: 'status', header: 'Status'}
];
// tested the object with these
alert('1: ' + JSON.stringify(this.searchResults)); // undefined
alert('2: ' + this.searchResults); // undefined
alert('3: ' + this.searchResults.toString); // undefined
}
// This is what I am trying to accomplish
getSelected(selected: Student) {
for (const result of this.searchResults) {
if (selected.studentID === result.studentID) {
// do some other stuff
}
}
}
那么为什么 PrimeNG table 能够用对象填充 table,但我不能在 TypeScript 中迭代它?如何让它将对象视为数组?
您的 ts
文件中显然没有 columns
声明
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of columns">
{{car[col.field]}}
</td>
</tr>
到
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of cols">
{{car[col.field]}}
</td>
</tr>
编辑:您正在使用订阅订阅异步方法,但您试图在订阅后进行分配。因此,您的功能不会等待订阅完成。您的问题的解决方案是:
this.studentService.getStudents().subscribe(data => {
this.searchResults = data;
// table columns
this.cols = [
{ field: 'studentId', header: 'Student ID'},
{ field: 'name', header: 'Name'},
{ field: 'dob', header: 'Date of Birth'},
{ field: 'status', header: 'Status'}
];
// tested the object with these
alert('1: ' + JSON.stringify(this.searchResults)); // undefined
alert('2: ' + this.searchResults); // undefined
alert('3: ' + this.searchResults.toString); // undefined
});
我有一个后端数据库,将学生记录作为 JSON 文件提供。然后将这些对象填充到 PrimeNG table 中以供显示和选择。
student.component.html
<p-table class="table" [columns]="cols" [value]="searchResults">
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns">
{{col.header}}
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData>
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of columns">
{{rowData[col.field]}}
</td>
</tr>
</ng-template>
</p-table>
然而,当我尝试在 TypeScript 中读取或遍历学生记录时,我得到 "TypeError: Cannot read property XXX of undefined"。
student.component.ts
export class StudentComponent implements OnInit {
searchResults: Student[];
cols: any[];
constructor(private router: Router, private studentService: StudentService, private http: HttpClient) {
}
ngOnInit(): void {
this.studentService.getStudents().subscribe(data => {
this.searchResults = data;
});
// table columns
this.cols = [
{ field: 'studentId', header: 'Student ID'},
{ field: 'name', header: 'Name'},
{ field: 'dob', header: 'Date of Birth'},
{ field: 'status', header: 'Status'}
];
// tested the object with these
alert('1: ' + JSON.stringify(this.searchResults)); // undefined
alert('2: ' + this.searchResults); // undefined
alert('3: ' + this.searchResults.toString); // undefined
}
// This is what I am trying to accomplish
getSelected(selected: Student) {
for (const result of this.searchResults) {
if (selected.studentID === result.studentID) {
// do some other stuff
}
}
}
那么为什么 PrimeNG table 能够用对象填充 table,但我不能在 TypeScript 中迭代它?如何让它将对象视为数组?
您的 ts
文件中显然没有 columns
声明
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of columns">
{{car[col.field]}}
</td>
</tr>
到
<tr [pSelectableRow]="rowData" class="tr-click" (click)="getSelected(rowData)">
<td *ngFor="let col of cols">
{{car[col.field]}}
</td>
</tr>
编辑:您正在使用订阅订阅异步方法,但您试图在订阅后进行分配。因此,您的功能不会等待订阅完成。您的问题的解决方案是:
this.studentService.getStudents().subscribe(data => {
this.searchResults = data;
// table columns
this.cols = [
{ field: 'studentId', header: 'Student ID'},
{ field: 'name', header: 'Name'},
{ field: 'dob', header: 'Date of Birth'},
{ field: 'status', header: 'Status'}
];
// tested the object with these
alert('1: ' + JSON.stringify(this.searchResults)); // undefined
alert('2: ' + this.searchResults); // undefined
alert('3: ' + this.searchResults.toString); // undefined
});