mat-table 'data-source' bug/inconsistency
Bug, feature request, or proposal:
<mat-table> datasource doesn't work on async/change detection-OR unify change detection process
What is the expected behavior?
datasource should trigger change detection regardless of whether hard set or set after async return of data (within a 'subscribe')
What is the current behavior?
datasource works one way when set to a const. eg: WORKS
// where ELEMENT_DATA is a simple array of objects):
datasource = new MatTableDataSource(ELEMENT_DATA); :`
but DOESN'T work another way (SHOULD WORK) when setting datasource on async return of a (eg) service:
`this.myService.getData().subscribe( (response) => {
this.datasource.data = response.data; <======== mat-table change detection not triggered
});
but DOES work this way when using:
dataSource = new UserDataSource(this.workflowService); where
export class UserDataSource extends DataSource<any> {
constructor(private service: Service) {
super();
}
connect(): Observable<IDataArray[]> {
return this.service.getData()
.map( (dataArray) => {
return dataArray;
},
(error) => {
return error;
})
.catch( (exception) => {
return of(exception);
});
}
disconnect() {}
}
The variations above are inconsistent...
What are the steps to reproduce? see above
What is the use-case or motivation for changing an existing behavior?
conventional Angular 2/5 change detection mechanism by passing in new object reference to datasource.
Which versions of Angular, Material, OS, TypeScript, browsers are affected?
"@angular/core": "^5.1.2",, "@angular/material": "^5.0.3",
Is there anything else we should know?
Stackoverflow question on something that should be exceedingly simple: https://stackoverflow.com/questions/48411177/angular-material-table-mat-table-with-datasource-with-multiple-inner-array-a
When the table receives new data, it checks to see if there are changes since the last data array. If there are changes, it will add/remove/move rows accordingly. If a row is inserted, it will mark for check so that cells fill in with the right content.
In your example, you are saying that when the data changes, the table does not check for changes. Can you reproduce this in a stackblitz so we can explore what's happening here?
Also, to clarify, I assume this line was a typo:
this.datasource = response.data;
and you meant:
this.datasource.data = response.data
@andrewseguin yes, that was a typo..fixed it. Note, my service is returning a mocked data observable...(in-mem-web-api) if it helps. BTW, I've tried that, in addition to, shallow copy and lodash's _.clonedeep() as well. None trigger change detection:
this.datasource.data = [...,response.data];
or
this.datasource.data = _.deepclone(response.data)
As for providing a stackblitz I will try to make time. I stress, the above examples use the same exact async service...the
export class UserDataSource extends DataSource<any>
works...
but the one setting the datasource within service.subscribe() doesn't.
Also I would appreciate it greatly if you look at my SO link and provide comments there..my returned data is an object of 2 arrays used to populate two adjacent tables...since the UserDataSource mode is working, how do I access the two arrays and pass down to the <mat-table>?
So I have this working in an 'Angular change detected' the way like it should. There are a couple of nuances which would've helped if documented. Basically, the datasource CAN be set within a simple async service **map()** (**not subscribe()**). No need for that extraneous
export class UserDataSource extends DataSource<any>. So the below works:
dataSource = new MatTableDataSource(); <==== NUANCE ...not clearly mandated in examples
ngOnInit() {
this.service.itemList()
.map( (arrayItems) => {
return this.dataSource.data = arrayItems; <====== **NUANCE** .data attribute should be set in map() operator.
})
.subscribe( (workflows) => {
} );
}
The above is simple and clean. Not sure yet how to do this if service returns object of TWO arrays, one <mat-table> for each array.
@momoadeli, as @andrewseguin proposed above, try to reproduce it in a stackblitz. Why would the behavior be any different in each case (code is in map and in subscribe)?
Not sure what you are struggling with but it doesn't seem to be an issue. If you find something wrong with the table then feel free to open up the issue with a stackblitz
I also face the same. Changes are not getting reflected when changing the data source of mat table.
Same here. When I resize the window it triggers change detection correctly. I trtied subscribing to the data source's observable and call the changeDetector's markForCheck() method as well, didnt make any difference.
I did notice that there is a difference between updating the properties of an object but keep the object reference itself same versus copying th eobject reference, make changes to those properties and replace the item in the array with the new reference.
Both approaches didnt give the desired effect. The approach where I replace the reference it produced an empty row and no update in the affected row. I don't have time to create a stackblitz atm. Will try to come up with one soon. Both render the table correctly again after a resize so the data is correct in both cases I would assume..
How is this still such a problem today? Where's the proper documentation for all this stuff? Frustrating as hell.
I am stuck on this.. it's a shame there is no proper documentation on this issue to this day (Jan 2019)
I got it working by calling renderRows manually. About to leave my desk but some pseudo code:
@ViewChild(MatTable) table: MatTable<any>;
ngOnInit() {
this.tableSource.valueChanges.subscribe(() => {
this.table.renderRows();
});
}
edit: btw, im using angular/material @ 5.2.0
I am using angular/material 7.0.2 and your solution is not working..
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.