Skip to content
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

fix(table): empty string should be sorted right #8011

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/lib/table/table-data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ export class MatTableDataSource<T> implements DataSource<T> {
* @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; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional is a bit long to be single-lined


return isNaN(+value) ? value : +value;
}

Expand Down
15 changes: 15 additions & 0 deletions src/lib/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down