From 5fd6c9e39becb547b8e8b89985db567a2a1613fb Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 24 Oct 2017 14:41:04 -0700 Subject: [PATCH 1/2] fix(table): empty string should be sorted right --- src/lib/table/table-data-source.ts | 7 ++++++- src/lib/table/table.spec.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/table/table-data-source.ts b/src/lib/table/table-data-source.ts index 14852a5f570f..35e979985b76 100644 --- a/src/lib/table/table-data-source.ts +++ b/src/lib/table/table-data-source.ts @@ -86,7 +86,12 @@ export class MatTableDataSource implements DataSource { * @param sortHeaderId The name of the column that represents the data. */ sortingDataAccessor = (data: T, sortHeaderId: string): string|number => { - const value: number|string = data[sortHeaderId]; + const value: any = data[sortHeaderId]; + + // If the value is a string and only whitespace, return the value. + // Otherwise +value will convert it to 0. + if (typeof value === 'string' && !value.trim()) { return value; } + return isNaN(+value) ? value : +value; } diff --git a/src/lib/table/table.spec.ts b/src/lib/table/table.spec.ts index 39949cf92b19..5c8a60afdcea 100644 --- a/src/lib/table/table.spec.ts +++ b/src/lib/table/table.spec.ts @@ -184,6 +184,21 @@ describe('MatTable', () => { ]); }); + it('should by default correctly sort an empty string', () => { + // Activate column A sort + dataSource.data[0].a = ' '; + component.sort.sort(component.sortHeader); + fixture.detectChanges(); + + // Expect that empty string row comes before the other values + expectTableToMatchContent(tableElement, [ + ['Column A\xa0Sorted by a ascending', 'Column B', 'Column C'], + ['', 'b_1', 'c_1'], + ['a_2', 'b_2', 'c_2'], + ['a_3', 'b_3', 'c_3'], + ]); + }); + it('should be able to page the table contents', fakeAsync(() => { // Add 100 rows, should only display first 5 since page length is 5 for (let i = 0; i < 100; i++) { From c4f1d8aee3183da02b3114cf4e9aa382da1fa0ca Mon Sep 17 00:00:00 2001 From: Andrew Seguin Date: Tue, 24 Oct 2017 15:08:47 -0700 Subject: [PATCH 2/2] newline --- src/lib/table/table-data-source.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib/table/table-data-source.ts b/src/lib/table/table-data-source.ts index 35e979985b76..32a19e63345e 100644 --- a/src/lib/table/table-data-source.ts +++ b/src/lib/table/table-data-source.ts @@ -90,7 +90,9 @@ export class MatTableDataSource implements DataSource { // If the value is a string and only whitespace, return the value. // Otherwise +value will convert it to 0. - if (typeof value === 'string' && !value.trim()) { return value; } + if (typeof value === 'string' && !value.trim()) { + return value; + } return isNaN(+value) ? value : +value; }