我有来自后端的数据流,我看到它在控制台中打印现在我正在尝试将事件推送到dataSource
它的抛出错误dataSource未定义。有人可以帮助如何动态添加数据以实现表格吗?
stream.component.html
<mat-table #table [dataSource]="dataSource"></mat-table>
stream.component.ts
import {
Component,
OnInit
} from '@angular/core';
import {
StreamService
} from '../stream.service';
import {
MatTableDataSource
} from '@angular/material';
import * as io from 'socket.io-client';
@Component({
selector: 'app-stream',
templateUrl: './stream.component.html',
styleUrls: ['./stream.component.css']
})
export class StreamComponent implements OnInit {
displayedColumns = ['ticketNum', "assetID", "severity", "riskIndex", "riskValue", "ticketOpened", "lastModifiedDate", "eventType"];
dataSource: MatTableDataSource < Element[] > ;
socket = io();
constructor(private streamService: StreamService) {};
ngOnInit() {
this.streamService.getAllStream().subscribe(stream => {
this.dataSource = new MatTableDataSource(stream);
});
this.socket.on('newMessage', function(event) {
console.log('Datasource', event);
this.dataSource.MatTableDataSource.filteredData.push(event);
});
}
}
export interface Element {
ticketNum: number;
ticketOpened: number;
eventType: string;
riskIndex: string;
riskValue: number;
severity: string;
lastModifiedDate: number;
assetID: string;
}
答案 0 :(得分:22)
我找到了解决这个问题的方法,基本上如果你这样做了:
this.dataSource.data.push(newElement); //Doesn't work
但是如果你替换完整的数组,那么它可以正常工作。所以你的最终代码必须是:
this.socket.on('newMessage', function(event) {
const data = this.dataSource.data;
data.push(event);
this.dataSource.data = data;
});
您可以在此处查看问题 - &gt; https://github.com/angular/material2/issues/8381
答案 1 :(得分:1)
以下解决方案适用于 Angular 5.2.3 和 Angular Material 5.1.1 :
this.socket.on('newMessage', function(event) {
this.dataSource.data.push(event);
this.dataSource.data = this.dataSource.data.slice();
});
答案 2 :(得分:1)
来自docs
由于表针对性能进行了优化,因此不会自动检查数据数组的更改。相反,当在数据数组上添加,删除或移动对象时,可以通过调用表的renderRows()方法来触发对表的渲染行的更新。
因此,您可以使用ViewChild和refreshRow()
@ViewChild('table', { static: true }) table
add() {
this.dataSource.data.push(ELEMENT_DATA[this.index++])
this.table.renderRows()
}
中放了一个简单的例子
答案 3 :(得分:1)
除了不使用单独的数组然后将其分配给// Array of double
ArrayUP<double> pBuffer = MemHelper::MakeAUP<double>(2ui64 * nItems);
// Single Connection
SingleUP<Connection> pConn = MemHelper::MakeSUP<Connection>(ioContext);
数据外,我们还可以使用ES6扩展运算符;如果您不使用ES6 +分配{{1},则可以使用dataSource
}数据保存到新数组。
在ES6 +中
concat
ECMAscript版本3 +
dataSource
答案 4 :(得分:1)
对我来说,这些答案都没有用。 可以观察到我订阅了获取新数据。 唯一适合我的解决方案是:
this.dataService.pointsNodesObservable.subscribe((data) => {
this.dataSource = new InsertionTableDataSource(this.paginator, this.sort, this.dataService);
this.dataSource.data = data;
});
呈现像魅力一样!
答案 5 :(得分:0)
在创建选择行并对行数据应用某些操作时,我陷入了同样的问题。解决您问题的方法
imports..................
import { MatTableDataSource } from '@angular/material';
@component({ ...
export class StreamComponent implements OnInit {
// Initialise MatTableDataSource as empty
dataSource = new MatTableDataSource<Element[]>();
constructor() {}
...
// if you simply push data in dataSource then indexed 0 element remain undefined
// better way to do this as follows
this.dataSource.data = val as any;
// for your problem
ngOnInit() {
// ** important note .... I am giving solution assuming the subscription data is array of objects. Like in your case stream and in second event(parameter as callback)
this.streamService.getAllStream().subscribe(stream => {
// do it this way
this.dataSource.data = stream as any;
// note if you simply put it as 'this.dataSource.data = stream' then TS show you error as '[ts] Type 'string' is not assignable to type '{}[]''
});
this.socket.on('newMessage', (event) => {
console.log('Datasource', event);
// for this value
// this.dataSource.MatTableDataSource.filteredData.push(event); // I couldn't get what you are doing here
// SO try to explain what you are getting in this callback function val as event parameter ?????????????????
// but you can get it this ways
this.dataSource.filteredData = event as any;
});
}
希望这对您有帮助。如果您有任何问题,请ping我。
答案 6 :(得分:0)
这是一个非常简单容易的解决方案:
displayedColumns = ['ticketNum', 'assetID', 'severity', 'riskIndex', 'riskValue', 'ticketOpened', 'lastModifiedDate', 'eventType'];
dataSource: any[] = [];
constructor() {
}
ngOnInit() {
}
onAdd() { //If you want to add a new row in the dataSource
let model = { 'ticketNum': 1, 'assetID': 2, 'severity': 3, 'riskIndex': 4, 'riskValue': 5, 'ticketOpened': true, 'lastModifiedDate': "2018-12-10", 'eventType': 'Add' }; //get the model from the form
this.dataSource.push(model); //add the new model object to the dataSource
this.dataSource = [...this.dataSource]; //refresh the dataSource
}
希望这会有所帮助:)