[Table] Add example with dynamic data (add/remove/edit rows)
Bug, feature request, or proposal:
cdk table API docs need instructions and/or an example of updating the dataSource in the table connecting the table to a data source
What is the expected behavior?
seeing implementation instructions and/or an example of how to send the updated dataSource to the table so that the connect function is called
What is the current behavior?
The docs only cover rendering data on initial load of the component, but if a service updates the data, there isn't a documented way to update the data-table
What are the steps to reproduce?
Providing a Plunker (or similar) is the best way to get the team to see your issue. Plunker template: https://goo.gl/DlHd6U
What is the use-case or motivation for changing an existing behavior?
the md-table API doc hints at an approach but also lacks clarity
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
angular 4 material 2 typescript 2.4 windows
Is there anything else we should know?
@shalugin for my sorry unfortunately no. I already have something like that:
public addManufacturer(manufacturer: Manufacturer): void {
let newManufacturers: Manufacturer[] = [];
this.data.subscribe((manufacturers: Manufacturer[]) => newManufacturers = manufacturers);
newManufacturers.push(manufacturer);
this.manufacturerDataSubject.next(newManufacturers);
}
I know the code looks strange but this is because of my experiments on this to make it work. I can make plnkr with my code.
Hmmm..
It shows no changes until I click on header for sorting or paginator and only after that table will re-render.
Maybe this indicating a problem. Try insert ChangeDetectorRef into contructor and trigger changes manually.
constructor(private _cdr: ChangeDetectorRef) {
}
.....
this.manufacturerDataSubject.next(newManufacturers);
this._cdr.detectChanges();
.....
Try insert ChangeDetectorRef into contructor and trigger changes manually.
I'm trying with ApplicationRef (tick() method) because of I'm using a service. But there is no effect.
public addManufacturer(manufacturer: Manufacturer): void {
let newManufacturers: Manufacturer[] = [];
this.data.subscribe((manufacturers: Manufacturer[]) => newManufacturers = manufacturers);
newManufacturers.push(manufacturer);
this.manufacturerDataSubject.next(newManufacturers);
this._ar.tick();
}
public getManufacturers(): Observable<Manufacturer[] | Error> {
console.log('getManufacturers started!');
return this.lookupService.getAllManufacturers();
}
public submitManufacturers(manufacturers: Observable<Manufacturer[]>): void {
manufacturers.forEach((m: Manufacturer[]) => {
console.log('submitManufacturers started! manufacturers: ' + JSON.stringify(m));
});
manufacturers.subscribe((m: Manufacturer[]) => this.manufacturerDataSubject.next(m));
this._ar.tick();
console.log('submitManufacturers finished!');
}
@Shrralis Can you create a plunker or stackblitz with your code?
@shalugin here it is: https://stackblitz.com/edit/angular-h3ckjw
@Shrralis
Just change code:
const displayDataChanges = [
this._filterChange,
this._sort.sortChange,
this._paginator.page,
];
to
const displayDataChanges = [
this._filterChange,
this._sort.sortChange,
this._paginator.page,
this.manufacturersLocalService.data,
];
You don't need to use ApplicationRef (tick() method).
If I understood you right, then you only need some BehaviorSubject that will trigger changes for DataSource.
@tyler2cr , I have created a package that extends angular cdk DataSource implementing row addition, edition and deletion. You can check it here.
It only adds structure to operate with rows, and manage datasource logic to update elements.
This is the npm package.
@shairez
Just change code
You're right, it worked! Thank you so much
I have a similar issue. My Angular 4 app has a service pulling data from a Laravel backend thats connected to a MySQL DataBase.
I am able to display the data through ngFor with a Bootstrap 4 table so I know it works.
My question is.. how in the world do I do this with the Material table??
If anyone could provide some guidance i would truly appreciate it.
@heri-g You can use example from https://github.com/angular/material2/issues/5917#issuecomment-337899837
@willshowell Is it possible to get that short example added to the material.angular.io docs?. It's really good and easy to follow. All the other examples are complex and muddy the water with techniques that make it hard to get the basics. Your example showed me what I need to know in 10 minutes, the other just confused me for the last 10 hours, grrr if only I'd found this sooner 👍
https://github.com/angular/material2/tree/master/src/demo-app/table
What is MatTableDataSource here? Stackblitz gives me error about that.
Your plunker code is not really working would you please check on your dependencies @irossimoline
@rima-smart , I have updated the dependencies.
Here is what worked for me. Stackblitz url :https://stackblitz.com/edit/angular-5zu7px Below is the code to add new element to array without using behaviorSubject.
addRow() {
alert('adding row');
this.dataSource.data.push({
position: 1,
name: "Hydrogen",
weight: 1.0079,
symbol: "H"
});
this.dataSource.filter = "";
}
Is there something less hacky than using
this.dataSource.filter = "";
after pushing data, to get a re-render?
Also, I add new data, and new rows are added, but the cells in the new row are empty...even though the new data is good.
If I open "Dev Tools" and click around and inspect, then the table will render the cells. So I know something is wrong with this lib.
You can use the method called _updateChangeSubscription(). This method will update subscription to changes that should trigger an update to the table's rendered rows. When the changes occur it processes the current state of the filter, sort, and pagination along with the base data and sends it to the table for rendering.
Took me some time but I finally got everything working. So, here's my CRUD implementation if anyone gets in trouble with this: https://github.com/marinantonio/angular-mat-table-crud
Screenshot:

Or you can test it on gist: https://marinantonio.github.io/angular-mat-table-crud/
@marinantonio cool, how did you create that GIF?