1

I am adding a row into table, in order to add a new record and i want to keep that row at top regardless the sort header (asc, desc or none).

When no order or asc is defined the row is at top but when desc order is clicked the row goes to bottom.

Any ideas on how to keep null values (new row object in data source has null values for column sort) always at top?

Order None is OK: https://i.sstatic.net/J3Ou1.png

Order Asc is OK too: https://i.sstatic.net/xFolC.png

But Order Desc IS NOT ok: https://i.sstatic.net/bsksj.png

3 Answers 3

2

I had a similar problem, which was "ignoring the null values when one clicks on the sort button, so that they are always printed at the end of the table". I solved the problem this way:

  • overhiding the sortingDataAccessor method of the MatTableDataSource
  • checking the item protery value
  • if the value is null, then checking the sort order
  • according to sort order: returning a (possible) highest / lowest value so that the lines with null values are always at the end of the table (in your case, you would invert the returned values).

Here is a sample of code:

this.rankingData = new MatTableDataSource();
this.rankingData.sortingDataAccessor = (item, property) => {
    if(null == item[property]) {
      if("desc" == this.sort.direction) return '\u0000'+'\u0000'+'\u0000';
    return '\uFFFF'+'\uFFFF'+'\uFFFF';
  }
  return item[property];
};
Sign up to request clarification or add additional context in comments.

1 Comment

Second if condition should be like this if("asc" === this.sort.direction) ...
1

When the value is null, you can 'replace' it by a character that sorts after Z, for example omega (Ω), by overwriting the sorting mechanism.

this.dataSource.sortingDataAccessor = (item, key) => item[key] || 'Ω';

The sorting mechanism will return the key/value pair, but when no value is found, it will return the omega character, but for sorting only, so it won't show up in your table.

Comments

0

The angular MatSortModule has a property called 'deregister'. You can try adding this property to the line you want to remove from MatSortables

Source: https://material.angular.io/components/sort/api

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.