Skip to content

Commit a328285

Browse files
committed
fix(table): data not being re-rendered when sortingDataAccessor is changed
Fixes the table's data not re-rendering when a new `sortingDataAccessor` is assigned to the data source. Fixes #15888.
1 parent c0d493f commit a328285

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/material/table/table-data-source.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ export class MatTableDataSource<T> extends DataSource<T> {
110110
* @param data Data object that is being accessed.
111111
* @param sortHeaderId The name of the column that represents the data.
112112
*/
113-
sortingDataAccessor: ((data: T, sortHeaderId: string) => string|number) =
113+
get sortingDataAccessor() { return this._sortingDataAccessor; }
114+
set sortingDataAccessor(value: (data: T, sortHeaderId: string) => string|number) {
115+
this._sortingDataAccessor = value;
116+
this._updateChangeSubscription();
117+
}
118+
private _sortingDataAccessor: ((data: T, sortHeaderId: string) => string|number) =
114119
(data: T, sortHeaderId: string): string|number => {
115120
const value = (data as {[key: string]: any})[sortHeaderId];
116121

src/material/table/table.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@ describe('MatTable', () => {
341341
]);
342342
});
343343

344+
it('should emit if the sortingDataAccessor changes', () => {
345+
const spy = jasmine.createSpy('data changes spy');
346+
const subscription = dataSource.connect().subscribe(spy);
347+
348+
// Reset the `calls` since the data source emits upon subscription as well.
349+
spy.calls.reset();
350+
dataSource.sortingDataAccessor = () => '';
351+
352+
expect(spy).toHaveBeenCalled();
353+
subscription.unsubscribe();
354+
});
355+
344356
it('should by default correctly sort an empty string', () => {
345357
// Activate column A sort
346358
dataSource.data[0].a = ' ';

0 commit comments

Comments
 (0)