Skip to content

fix(material/table): data not being re-rendered when sortingDataAccessor is changed #15922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/material-experimental/mdc-table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,18 @@ describe('MDC-based MatTable', () => {
]);
});

it('should emit if the sortingDataAccessor changes', () => {
const spy = jasmine.createSpy('data changes spy');
const subscription = dataSource.connect().subscribe(spy);

// Reset the `calls` since the data source emits upon subscription as well.
spy.calls.reset();
dataSource.sortingDataAccessor = () => '';

expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
});

it('should by default correctly sort an empty string', () => {
// Activate column A sort
dataSource.data[0].a = ' ';
Expand Down
28 changes: 27 additions & 1 deletion src/material/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,32 @@ export class _MatTableDataSource<
* @param data Data object that is being accessed.
* @param sortHeaderId The name of the column that represents the data.
*/
sortingDataAccessor: (data: T, sortHeaderId: string) => string | number = (
sortingDataAccessor: (data: T, sortHeaderId: string) => string | number;

/**
* `sortingDataAccessor` is currently a regular property, however we need to know when it is
* updated so that we can re-render the data. The problem is that we can't turn it into a
* getter/setter, because it'll break existing users that define the accessor by extending the
* class and initializing it to a new value. This is a workaround that'll preserve the old
* behavior for users who override it and apply the fix for ones who don't. Eventually we should
* reconcile these behaviors.
* @breaking-change 15.0.0
*/
private _defineSortingDataAccessor() {
if (!this.sortingDataAccessor) {
Object.defineProperty(this, 'sortingDataAccessor', {
get: () => this._sortingDataAccessor,
set: accessor => {
this._sortingDataAccessor = accessor;
if (this._updateChangeSubscription) {
this._updateChangeSubscription();
}
},
});
}
}

private _sortingDataAccessor: (data: T, sortHeaderId: string) => string | number = (
data: T,
sortHeaderId: string,
): string | number => {
Expand Down Expand Up @@ -258,6 +283,7 @@ export class _MatTableDataSource<
constructor(initialData: T[] = []) {
super();
this._data = new BehaviorSubject<T[]>(initialData);
this._defineSortingDataAccessor();
this._updateChangeSubscription();
}

Expand Down
12 changes: 12 additions & 0 deletions src/material/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ describe('MatTable', () => {
]);
});

it('should emit if the sortingDataAccessor changes', () => {
const spy = jasmine.createSpy('data changes spy');
const subscription = dataSource.connect().subscribe(spy);

// Reset the `calls` since the data source emits upon subscription as well.
spy.calls.reset();
dataSource.sortingDataAccessor = () => '';

expect(spy).toHaveBeenCalled();
subscription.unsubscribe();
});

it('should by default correctly sort an empty string', () => {
// Activate column A sort
dataSource.data[0].a = ' ';
Expand Down