Skip to content

Commit b2da9fa

Browse files
committed
fix(material/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 29ac6cf commit b2da9fa

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

src/material-experimental/mdc-table/table.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,18 @@ describe('MDC-based MatTable', () => {
447447
]);
448448
});
449449

450+
it('should emit if the sortingDataAccessor changes', () => {
451+
const spy = jasmine.createSpy('data changes spy');
452+
const subscription = dataSource.connect().subscribe(spy);
453+
454+
// Reset the `calls` since the data source emits upon subscription as well.
455+
spy.calls.reset();
456+
dataSource.sortingDataAccessor = () => '';
457+
458+
expect(spy).toHaveBeenCalled();
459+
subscription.unsubscribe();
460+
});
461+
450462
it('should by default correctly sort an empty string', () => {
451463
// Activate column A sort
452464
dataSource.data[0].a = ' ';

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,14 @@ export class _MatTableDataSource<
151151
* @param data Data object that is being accessed.
152152
* @param sortHeaderId The name of the column that represents the data.
153153
*/
154-
sortingDataAccessor: (data: T, sortHeaderId: string) => string | number = (
154+
get sortingDataAccessor() {
155+
return this._sortingDataAccessor;
156+
}
157+
set sortingDataAccessor(value: (data: T, sortHeaderId: string) => string | number) {
158+
this._sortingDataAccessor = value;
159+
this._updateChangeSubscription();
160+
}
161+
private _sortingDataAccessor: (data: T, sortHeaderId: string) => string | number = (
155162
data: T,
156163
sortHeaderId: string,
157164
): string | number => {

src/material/table/table.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,18 @@ describe('MatTable', () => {
432432
]);
433433
});
434434

435+
it('should emit if the sortingDataAccessor changes', () => {
436+
const spy = jasmine.createSpy('data changes spy');
437+
const subscription = dataSource.connect().subscribe(spy);
438+
439+
// Reset the `calls` since the data source emits upon subscription as well.
440+
spy.calls.reset();
441+
dataSource.sortingDataAccessor = () => '';
442+
443+
expect(spy).toHaveBeenCalled();
444+
subscription.unsubscribe();
445+
});
446+
435447
it('should by default correctly sort an empty string', () => {
436448
// Activate column A sort
437449
dataSource.data[0].a = ' ';

tools/public_api_guard/material/table.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ export class _MatTableDataSource<T, P extends MatTableDataSourcePaginator = MatT
190190
get sort(): MatSort | null;
191191
set sort(sort: MatSort | null);
192192
sortData: (data: T[], sort: MatSort) => T[];
193-
sortingDataAccessor: (data: T, sortHeaderId: string) => string | number;
193+
get sortingDataAccessor(): (data: T, sortHeaderId: string) => string | number;
194+
set sortingDataAccessor(value: (data: T, sortHeaderId: string) => string | number);
194195
_updateChangeSubscription(): void;
195196
_updatePaginator(filteredDataLength: number): void;
196197
}

0 commit comments

Comments
 (0)