我有一个使用Angular Material构建的动态数据表,在这里我正在使用API向表提供数据。
我想使用添加复选框列来选择特定的行和/或多个行,但是在显示复选框行以进行选择时遇到了问题。
现在我出现了错误
ViewpageComponent.html:19错误TypeError:无法读取未定义的属性'ColumnObjects'
但最重要的是,我的复选框完全没有显示。第一个ng-container列未显示**我不确定是什么问题?
我将Angular 7与Angular Material一起使用
view.component.html
<table mat-table [dataSource]="fetchedData" matSort matSortActive="{{defaultSortCol}}" matSortDirection="asc">
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef style="width:40px;">
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()" (change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
</ng-container>
<ng-container [matColumnDef]="column.columnId" *ngFor="let column of viewData.ColumnObjects">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ column.propertyName }}
</th>
<td mat-cell *matCellDef="let action">{{ action[column.columnId] }}</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="viewData.ColumnIds; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: viewData.ColumnIds"></tr>
</table>
view.component.ts
@Component({
templateUrl: 'viewpage.component.html'
})
export class ViewpageComponent implements AfterViewInit, OnInit, OnDestroy {
viewData: any;
viewName: string;
viewTag: number;
fetchedData: any;
dataSource: ViewData
Source;
pageSizeOptions: number[] = [10, 20, 50];
defaultSortCol = "1";
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
selection = new SelectionModel<TableRow>(true, []);
navSub: any;
constructor(private actionService: ActionService, private route: ActivatedRoute, private router: Router) { }
ngOnInit() {
// Init the component the first time it is navigated to.
this.initData();
// Subscribe to the router, so that any new navigation to this component loads new data.
this.navSub = this.router.events.subscribe((e: any) => {
if(e instanceof NavigationEnd){
this.initData();
}
});
}
initData(){
this.viewTag = +this.route.snapshot.paramMap.get("tag");
this.dataSource = new ViewDataSource(this.actionService);
// Load the View from the DataSource with default params
this.dataSource.loadView(this.viewTag, 0, 10, this.defaultSortCol, "asc");
// Subscribe to the View in the DataSource
this.dataSource.view.subscribe(x => {
if (x.ActionName) {
this.viewName = x.ActionName;
this.viewData = x;
this.fetchedData = this.viewData.TableData;
console.log(`View Data ` + JSON.stringify(this.viewData.ViewData.DbrAction));
// this.viewData = ['select'].concat(this.viewData);
}
});
}
ngAfterViewInit() {
// After sorting, jump back to first page of newly sorted data.
this.sort.sortChange.subscribe(
() => {
this.paginator.pageIndex = 0
});
// Sort changes and pagination events should reload the page.
merge(this.sort.sortChange, this.paginator.page)
.pipe(
tap(() => this.loadPage())
).subscribe();
}
loadPage() {
this.dataSource.loadView(
this.viewTag,
//'',
this.paginator.pageIndex,
this.paginator.pageSize,
this.sort.active,
this.sort.direction);
}
您的表在初始设置之前尝试使用viewdata
,实际上viewData
未定义。
这就是viewData.ColumnObjects
抛出TypeError
的原因。
尝试使用?
检查,例如viewData?.ColumnObjects
或使用*ngIf="viewData"
]保护您的桌子
...
<ng-container [matColumnDef]="column.columnId" *ngFor="let column of viewData?.ColumnObjects">
...