0

I have a problem with Angular material Table sorting, I get the data from the serven in the onInit function, then when the application got the data, i put it into the dataSource and then set the pagination and sorting. It just doesn't work, i can click the arrows on the table but it doesn't sort the elements.

I tried to set it right after the response, to callback, or to the onAfterViewInit function.

  public displayedColumns: string[] = ['CreatedDate', 'CreatedBy', 'Name', 'Version', 'URL', 'Language', 'deleted','download'];
  public dataSource = new MatTableDataSource();
  public noDataMessage;
  @ViewChild(MatSort) sort: MatSort;
  @ViewChild(MatPaginator) paginator: MatPaginator;

  constructor(
    private _http: HttpClient, 
    private _snackBar: MatSnackBar, 
    private _router: Router,
    private _errorHandlingService: ErrorHandlingService,
    public _permissionCkecher: PermissionCheckerService) {
  }

  ngOnInit() {
    this.getDocumentList();
  }

  /**
   * Applys filter
   * @param filterValue 
   */
  public applyFilter(filterValue: string): void {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

  /**
   * Gets document list
   */
  private getDocumentList(): void {
    this._http.get('/api/Document/WithParam?ignoreGlobalFilter=true').subscribe(data => {
      if(data && (<any>data).length !== 0) {
        let listedDocumentData = (<any>data);
        this.addCategoryToDoc(listedDocumentData);
        this.dataSource.sort = this.sort;
      } else {
        this.noDataMessage = true;
      }
    },error => {
      this._errorHandlingService.handlingError(error);
    }
  )
  }

  /**
   * Gets Doc Categories
   */
  private addCategoryToDoc(DocList): any {
    this._http.post('/api/DocumentCategory/ListAll?ignoreCultureInfo=true',{}).subscribe(data => {
      let docCats;
        if(data) {
          docCats = (<any>data).filteredData;
        }
        DocList.forEach(LinkElement => {
          LinkElement.Category = docCats.filter(CatElement => CatElement.id === LinkElement.categoryId)[0]
        });
   //Here i call the callback
        this.addDataToDataSource(DocList);
      }, error => this._errorHandlingService.handlingError(error)
    )
  }

 //Here is the callback
  private addDataToDataSource(listedDocData) {
    this.dataSource = new MatTableDataSource(listedDocData);
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
 }

And Here is the HTML:

<div class="main-container">
    <section class="listing document">
    <mat-form-field class="search-field">
        <input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search" autocomplete="off">
    </mat-form-field>
    <table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">

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

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

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

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

        <ng-container matColumnDef="URL">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Document Category</th>
            <td mat-cell *matCellDef="let element"> {{((element.Category)?element.Category.name:'')}} </td>
        </ng-container>

        <ng-container matColumnDef="deleted">
            <th mat-header-cell *matHeaderCellDef mat-sort-header>Deleted element</th>
            <td mat-cell *matCellDef="let element"> {{((element.isDeleted)?'igen':'nem')}} </td>
        </ng-container>

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

        <ng-container matColumnDef="download">
            <th mat-header-cell *matHeaderCellDef>Download</th>
            <td mat-cell *matCellDef="let element" (click)="$event.stopPropagation()">
                <a href="{{element.url}}" download>Download</a>
            </td>
        </ng-container>

        <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
        <tr class="document-row" mat-row *matRowDef="let row; columns: displayedColumns;" (click)="navigateForDocumentComp(row)"></tr>
        </table>
        <div *ngIf="noDataMessage" class="empty-message">Üres adathalmaz</div>
        <mat-paginator 
        #paginator
        [pageSizeOptions]="[5, 10, 25, 50]"                 
        showFirstLastButtons></mat-paginator>
    </section>
</div>
3
  • Looks like it's a duplicate question and here is an answer stackoverflow.com/a/51787040/1906096 Commented Mar 26, 2019 at 12:04
  • Unfortunatelly that solution is not working in my case Commented Mar 26, 2019 at 12:11
  • Can you please share an example? Commented Mar 26, 2019 at 13:50

1 Answer 1

2

Solution, matColumnDef property has to be the same as the object key.

Sign up to request clarification or add additional context in comments.

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.