Skip to content

Commit acd8e74

Browse files
crisbetoannieyw
authored andcommitted
fix(table): filter predicate not called for falsy values (#19094)
We had a simple falsy check to determine whether to call the filter predicate. I'm guessing it was written this way so that if somebody were to clear a filter input, all items will be shown. This seems a bit too loose since it could catch things like 0 which technically aren't allowed due to the `string` type, but could still end up inside the data source if values are proxied in directly through the view. Ideally we'd just call the predicate for all values and let it decide whether or not to filter, but I left in special cases for undefined, null and empty strings, because I expect a lot of apps to be broken if we were to change the behavior. Fixes #19092. Fixes #9967. (cherry picked from commit 6ec1551)
1 parent fab6880 commit acd8e74

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ describe('MDC-based MatTable', () => {
352352
['a_2', 'b_2', 'c_2'],
353353
['Footer A', 'Footer B', 'Footer C'],
354354
]);
355+
356+
// Change the filter to a falsy value that might come in from the view.
357+
dataSource.filter = 0 as any;
358+
fixture.detectChanges();
359+
expectTableToMatchContent(tableElement, [
360+
['Column A', 'Column B', 'Column C'],
361+
['Footer A', 'Footer B', 'Footer C'],
362+
]);
355363
}));
356364

357365
it('should not match concatenated words', fakeAsync(() => {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ export class MatTableDataSource<T> extends DataSource<T> {
261261
// If there is a filter string, filter out data that does not contain it.
262262
// Each data object is converted to a string using the function defined by filterTermAccessor.
263263
// May be overridden for customization.
264-
this.filteredData =
265-
!this.filter ? data : data.filter(obj => this.filterPredicate(obj, this.filter));
264+
this.filteredData = (this.filter == null || this.filter === '') ? data :
265+
data.filter(obj => this.filterPredicate(obj, this.filter));
266266

267267
if (this.paginator) { this._updatePaginator(this.filteredData.length); }
268268

src/material/table/table.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,14 @@ describe('MatTable', () => {
353353
['a_2', 'b_2', 'c_2'],
354354
['Footer A', 'Footer B', 'Footer C'],
355355
]);
356+
357+
// Change the filter to a falsy value that might come in from the view.
358+
dataSource.filter = 0 as any;
359+
fixture.detectChanges();
360+
expectTableToMatchContent(tableElement, [
361+
['Column A', 'Column B', 'Column C'],
362+
['Footer A', 'Footer B', 'Footer C'],
363+
]);
356364
}));
357365

358366
it('should not match concatenated words', fakeAsync(() => {

0 commit comments

Comments
 (0)