0

Dear Stackoverflow community,

According to Angular material 2 documentation you can add an mdSort directive to your table:

Sorting

Use the mdSort directive and adds a sorting UI the table's column headers. The sort headers emit events that can be used to trigger an update via the table's data source.

Code : component.html

Using the mdSort directive and the md-sort-headers

<md-table fxFlex="100%" #table [dataSource]="dataSource" mdSort>

              <ng-container mdColumnDef="category">
                <md-header-cell *mdHeaderCellDef md-sort-header> Category </md-header-cell>
                <md-cell (click)="alert(element)" *mdCellDef="let element"> {{element.category}} </md-cell>
              </ng-container>
             ...
             ...
</md-table>

Code: component.ts

Referencing the sort directive:

 @ViewChild(MdSort) sort: MdSort;

Injecting it inside the datasource:

this.dataSource = new ExampleDataSource(this.exampleDatabase, this.sort);

And using it to sort the objects:

getSortedData(): Task[] {
const data = this._exampleDatabase.data.slice();
if (!this._sort.active || this._sort.direction === '') { return data; }

return data.sort((a, b) => {
  let propertyA: number|string|boolean = '';
  let propertyB: number|string|boolean = '';

  switch (this._sort.active) {
    case 'category': [propertyA, propertyB] = [a.category, b.category]; break;
    case 'task': [propertyA, propertyB] = [a.task, b.task]; break;
    case 'favorite': [propertyA, propertyB] = [a.favorite, b.favorite]; break;
  }

  let valueA = isNaN(+propertyA) ? propertyA : +propertyA;
  let valueB = isNaN(+propertyB) ? propertyB : +propertyB;

  return (valueA < valueB ? -1 : 1) * (this._sort.direction === 'asc' ? 1 : -1);
});

}

Now I want to have another sortable table, and I have created another datasource, database etc.

But how do I differentiate to mdSort directive?

1 Answers1

1

Binding to directives works using the exportAs-attribute within a directive, see this for reference: Angular 2: Get reference to a directive used in a component

This will not work with your example though, as you are using a third-party library and MdSort doesn't have the exportAs-property


If you want to bind to components, you can give each table an unique ID using the hashtag like this:

<md-table fxFlex="100%" #table1 [dataSource]="dataSource1" mdSort>
</md-table>
<md-table fxFlex="100%" #table2 [dataSource]="dataSource2" mdSort>
</md-table>

and then you can get the childs uniquely like this:

@ViewChild('table1') table1: MdTable;
@ViewChild('table2') table2: MdTable;
tommueller
  • 2,358
  • 2
  • 32
  • 44
  • It does not work for me. If I use : `@ViewChild('table1') sort: MdSort;` instead of `@ViewChild(MdSort) sort: MdSort;` my code doesn't work – Jan Somers JanS91 Sep 26 '17 at 09:31
  • You have to change the hashtags in the template accordingly from table to table1. See the first code block of my answer – tommueller Sep 26 '17 at 09:41
  • If i use ` ` together with `@ViewChild('table1') table1: MdSort;` it starts giving an error : `ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.` on the html selector – Jan Somers JanS91 Sep 26 '17 at 09:46
  • I changed the [dataSource] in my example, because I thought you wanted to use different data for each table. You need to change [dataSource]="dataSource1" back to [dataSource]="dataSource" – tommueller Sep 26 '17 at 09:49
  • First of all, thank you for all your help, but I do have 2 datasources, and I named the first dataSource and the second orderDataSource – Jan Somers JanS91 Sep 26 '17 at 09:55
  • Like this it should work fine. If you still get the error from above it is a problem with your dataSources, not with the @ViewChild part. – tommueller Sep 26 '17 at 10:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155311/discussion-between-jan-somers-jans91-and-newnoise). – Jan Somers JanS91 Sep 26 '17 at 10:01