I am using a mat-table to list the content of the users chosen languages. They can also add new languages using dialog panel. After they added a language and returned back.
npm install @matheo/datasource
I released a library aimed to be the official Material DataSource in the future, supporting any kind of input streams (sort, pagination, filters), and some configuration with debugging to see how it works while you're coding.
import { MatDataSourceModule } from '@matheo/datasource';
You can find the StackBlitz demo and further information here:
https://medium.com/@matheo/reactive-datasource-for-angular-1d869b0155f6
I'd be glad to hear your opinion and support your use cases if necessary.
Happy coding!
This worked for me:
refreshTableSorce() {
this.dataSource = new MatTableDataSource<Element>(this.newSource);
}
I don't know if the ChangeDetectorRef
was required when the question was created, but now this is enough:
import { MatTableDataSource } from '@angular/material/table';
// ...
dataSource = new MatTableDataSource<MyDataType>();
refresh() {
this.myService.doSomething().subscribe((data: MyDataType[]) => {
this.dataSource.data = data;
}
}
Example:
StackBlitz
So for me, nobody gave the good answer to the problem that i met which is almost the same than @Kay. For me it's about sorting, sorting table does not occur changes in the mat. I purpose this answer since it's the only topic that i find by searching google. I'm using Angular 6.
As said here:
Since the table optimizes for performance, it will not automatically check for changes to the data array. Instead, when objects are added, removed, or moved on the data array, you can trigger an update to the table's rendered rows by calling its renderRows() method.
So you just have to call renderRows() in your refresh() method to make your changes appears.
See here for integration.
You can easily update the data of the table using "concat":
for example:
language.component.ts
teachDS: any[] = [];
language.component.html
<table mat-table [dataSource]="teachDS" class="list">
And, when you update the data (language.component.ts):
addItem() {
// newItem is the object added to the list using a form or other way
this.teachDS = this.teachDS.concat([newItem]);
}
When you're using "concat" angular detect the changes of the object (this.teachDS) and you don't need to use another thing.
PD: It's work for me in angular 6 and 7, I didn't try another version.
You can also use renderRows() method.
@ViewChild(MatTable, {static: false}) table : MatTable // initialize
then this.table.renderRows();
for reference check this out -: https://www.freakyjolly.com/angular-7-8-edit-add-delete-rows-in-material-table-with-using-dialogs-inline-row-operation/