angular - Angular MatTable 中没有数据源检测
问题描述
我的数据源有问题,因为我需要在表的标题中显示一个 selectAll 复选框。
当我尝试对 dataSource 进行 console.log 时,它返回一个空数组,而 dataSource.data-length 返回零。
我需要一个输入,因为该组件是另一个组件的子组件
<image-list [imageList]="imageList"></image-list>
我试图把它放在 ngOnit 中,但它不起作用
import { Component, OnInit, Inject, OnDestroy, Input } from "@angular/core";
import { ImageModel } from "../image.model";
import { MatTableDataSource } from '@angular/material';
export class ImageListComponent implements OnInit, OnDestroy {
@Input() imageList: ImageModel[];
selection = new SelectionModel(true, [], true);
selectionModelChanged = new BehaviorSubject([]);
dataSource = new MatTableDataSource();
row: any;
constructor(private builderService: BuilderService,
@Inject(DOCUMENT) private document: any) {
this._unsubscribeAll = new Subject();
// this.dataSource = new MatTableDataSource(this.imageList); //it won't work here
}
ngOnDestroy() {
this.imageListChange.unsubscribe();
clearInterval(this.callLoop);
}
ngOnInit() {
this.getImageList();
this.dataSource = new MatTableDataSource(this.imageList);
console.log(this.dataSource.data) // returns []
this.imageListChange = this.builderService.imageRemoved$.subscribe(() => {
this.getImageList();
});
}
private getImageList() {
this.builderService.getFinishedImagesList().then(response => {
this.imageErrorMessage = null;
this.imageList = response;
console.log(response)
}).catch(() => {
this.imageErrorMessage = "Oops, there was an error getting the builds list.";
});
}
markAsSelected(device: any): void {
this.selection.isSelected(device);
}
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected(): boolean {
let numSelected = null;
let numRows = null;
numSelected = this.selectedImages.length;
numRows = this.dataSource.data.length; //always zero so, select All won't work
return numSelected === numRows;
}
}
提前感谢您的回复。
解决方案
我认为您应该在初始化列表时设置一个空数组。
@Input() imageList: ImageModel[] = [];
推荐阅读
- jenkins - 詹金斯:声明性管道中的未知阶段部分“矩阵”
- sql - SQL 问题:如果 Artikel 有多种艺术类型,但其中一种类型是“RS”,则将 Artikel 显示为“1”
- typescript - 动态导入 TypeScript 类型
- reactjs - 尝试使用 axios 从 Firebase 获取数据时出现错误
- linux - 如何从 azure 云连接到 linux 服务器并上传到 BLOB 存储?
- network-programming - 我的家用路由器给我的桥接 vm 适配器一个不同的动态 ip
- netsuite - 通过脚本调用 NetSuite 原生计算运费按钮?
- node.js - 猫鼬和快递的多租户
- javascript - 如何将 Vue/NuxtJs 与 Ezoic 集成?
- python - 在这种情况下,Python 是按引用传递还是按值传递,为什么?