components icon indicating copy to clipboard operation
components copied to clipboard

[Table] Add example with dynamic data (add/remove/edit rows)

Open tyler2cr opened this issue 9 years ago • 58 comments

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?

tyler2cr avatar Jul 20 '17 22:07 tyler2cr

@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.

shrralis avatar Oct 16 '17 23:10 shrralis

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();
.....

shalugin avatar Oct 17 '17 12:10 shalugin

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 avatar Oct 18 '17 07:10 shrralis

@Shrralis Can you create a plunker or stackblitz with your code?

shalugin avatar Oct 18 '17 08:10 shalugin

@shalugin here it is: https://stackblitz.com/edit/angular-h3ckjw

shrralis avatar Oct 19 '17 12:10 shrralis

@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).

shalugin avatar Oct 19 '17 14:10 shalugin

If I understood you right, then you only need some BehaviorSubject that will trigger changes for DataSource.

shalugin avatar Oct 19 '17 14:10 shalugin

@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.

irossimoline avatar Oct 19 '17 14:10 irossimoline

@shairez

Just change code

You're right, it worked! Thank you so much

shrralis avatar Oct 19 '17 14:10 shrralis

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 avatar Oct 19 '17 15:10 heri-g

@heri-g You can use example from https://github.com/angular/material2/issues/5917#issuecomment-337899837

shalugin avatar Oct 19 '17 16:10 shalugin

@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 👍

eltimmo avatar Oct 23 '17 21:10 eltimmo

https://github.com/angular/material2/tree/master/src/demo-app/table

What is MatTableDataSource here? Stackblitz gives me error about that.

shrralis avatar Oct 24 '17 18:10 shrralis

Your plunker code is not really working would you please check on your dependencies @irossimoline

reemyrail avatar Nov 06 '17 13:11 reemyrail

@rima-smart , I have updated the dependencies.

irossimoline avatar Nov 07 '17 16:11 irossimoline

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 = "";
  }

tg8888 avatar Jan 03 '18 05:01 tg8888

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.

ORESoftware avatar Jan 08 '18 03:01 ORESoftware

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.

armansujoyan avatar Jan 09 '18 13:01 armansujoyan

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: Alt Text

Or you can test it on gist: https://marinantonio.github.io/angular-mat-table-crud/

marinantonio avatar Jan 10 '18 21:01 marinantonio

@marinantonio cool, how did you create that GIF?

ORESoftware avatar Jan 10 '18 22:01 ORESoftware