teddy1995
teddy1995

Reputation: 61

Why angular table didn't sort?

here is the table.component.ts file. I want to sort the table like in this example of angular: https://material.angular.io/components/sort/overview but in this case, it didn't sort anything. I don't know what the problem is.

First I imported in the child.module.ts the modules:

import { MatTableModule } from '@angular/material/table';
import { MatSortModule } from "@angular/material/sort";

Import the module:

    import: [
        MatTableModule,
        MatSortModule
]

here is the table.component.ts file:

 import { MatPaginator } from '@angular/material/paginator';
    import { MatSort } from '@angular/material/sort';
    import { MatTableDataSource } from '@angular/material/table';

    export interface PeriodicElement {
      timestamp: string;  
      key: string;
      value: number;
    
    }
export class TableComponent implements OnInit {

  
  loadingData = false;


  displayedColumns: string[] = ['data', 'key', 'value'];

  ELEMENT_DATA: PeriodicElement[] = new Array<PeriodicElement>();

  dataSource = new MatTableDataSource<PeriodicElement>(this.ELEMENT_DATA);

  datastructure: Array<dataschema> = [];

  sortedData: PeriodicElement[];


   //sort data 
  @ViewChild(MatPaginator, { static: false }) paginator: MatPaginator;  
  @ViewChild(MatSort, { static: false }) sort: MatSort; 

  constructor(private Service: Service) {
this.sortedData = this.ELEMENT_DATA.slice() }

  exportTable() {
    TableUtil.exportTableToExcel("ExampleTable");
  }


  ngAfterViewInit(): void {
    this.dataSource.sort = this.sort;
    
}


  ngOnInit(): void {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;

    
    this.loadingData = true;

    this.Service.getContent().then(results => {

      this.datastructure = results;
     
     
      this.loadingData = false;
      
      
      for (let i = 0; i < this.datastructure.length; i++) {

        

        cut off code 
      }

    })

  }

sortData(sort: Sort) {
const data = this.ELEMENT_DATA.slice();
if (!sort.active || sort.direction === '') {
  this.sortedData = data;
  return;
}
this.sortedData = data.sort((a, b) => {
  const isAsc = sort.direction === 'asc';
  switch (sort.active) {
    case 'data': return compare(a.data, b.data, isAsc);
    case 'key': return compare(a.key, b.key, isAsc);
    case 'value': return compare(a.value, b.value, isAsc);
    default: return 0;


   }
    })

    
  }
      
    
}
function compare(a: number | string, b: number | string, isAsc: boolean) {
  return (a < b ? -1 : 1) * (isAsc ? 1 : -1);
}

The html view:

    <div *ngIf="!loadingData" class="mat-elevation-z8">
       <table id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort (matSortChange)="sortData($event)" class="mat-elevation-z8">
<!--Example Column/Row-->



<ng-container matColumnDef="data">
      <th mat-header-cell mat-sort-header *matHeaderCellDef > data </th>
      <td mat-cell *matCellDef="let element"> {{element.data}} </td>
    </ng-container>

    </table>
    </div>

Upvotes: 0

Views: 212

Answers (2)

Eliseo
Eliseo

Reputation: 57929

if you has the data under a *ngIf Angular can not reach it so I suggest give to Angular a breath (Enclosed the dataSource.paginator and this.dataSource.sort in a setTimeout

this.Service.getContent().then(results => {
    this.datastructure = results;
    this.loadingData = false;
    setTimeout(()=>{
      this.dataSource.paginator = this.paginator;
      this.dataSource.sort = this.sort;
    })
})

Anyway, material Angular sort the data for you, I can not understand your functions sort and your variable sortedData. you only need equal datasource.data to the response of your API

Upvotes: 1

Abdun Nahid
Abdun Nahid

Reputation: 311

The problem is in the ViewChild. There is a typo. You are trying to grab MatSort. But it is matSort.

TS

@ViewChild(MatSort, { static: false }) sort: MatSort; 

HTML

<table id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort class="mat-elevation-z8">

Instead, try the code below,

TS

@ViewChild('exampleTableTableSort, { static: false }) sort: MatSort; 

HTML

<table #exampleTableTableSort='matSort' id="ExampleTable" mat-table matTableExporter [dataSource]="dataSource"  matSort class="mat-elevation-z8">

Upvotes: 1

Related Questions