'Sorting not getting happened in angular material

I'm trying to add the sorting feature for the mat-table with angular material. I had got the hand cursor over my columns, When I clicked I'm unable to see it's action. i had added the attribute "matsort" to the table level & "mat-sort-header" for the mat-header-cell selector. 2) Secondly, I'm not able to view my "Actions" columnn for edit and delete. The following is my code :

app-component.html :

  <mat-table [dataSource]="emplist" matSort>
    <ng-container matColumnDef="Emp_Id">
      <mat-header-cell *matHeaderCellDef mat-sort-header> ID </mat-header-cell>
      <mat-cell *matCellDef="let data"> {{data.Emp_Id}} </mat-cell>
      </ng-container>
    
      <ng-container matColumnDef="Emp_Name">
      <mat-header-cell *matHeaderCellDef mat-sort-header> Name </mat-header-cell>
      <mat-cell *matCellDef="let data"> {{data.Emp_Name}} </mat-cell>
      </ng-container>
     
      <ng-container matColumnDef="Emp_Gender">
      <mat-header-cell *matHeaderCellDef mat-sort-header>  Gender </mat-header-cell>
      <mat-cell *matCellDef="let data"> {{data.Emp_Gender | settingGender}} </mat-cell>
      </ng-container>

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

        <ng-container matColumnDef="Emp_Salary">
          <mat-header-cell *matHeaderCellDef mat-sort-header> Salary </mat-header-cell>
          <mat-cell *matCellDef="let data"> {{data.Emp_Salary}} </mat-cell>
          </ng-container>
     
      
      <ng-container matColumnDef="action">
      <mat-header-cell *matHeaderCellDef> Actions </mat-header-cell>
      <mat-cell *matCellDef="let data">
      <button mat-button color="primary" (click)="EditEmployee(data.Emp_Id)">Edit</button>
      <button mat-button color="warn" (click)="DeleteEmployee(data.Emp_Id)">Delete</button>
      </mat-cell>
      </ng-container>
     
      <mat-header-row *matHeaderRowDef="columndefs"></mat-header-row>
      <mat-row *matRowDef="let row; columns: columndefs;"></mat-row>

      <!-- <mat-paginator [pageSizeOptions]="[5, 10, 25, 100]"></mat-paginator> -->
     </mat-table>

app-component.ts :

import { Component, OnInit, ViewChild } from '@angular/core';
import { EmployeeinfoService } from './employeeinfo.service';
import { FormBuilder, FormGroup, FormControl,Validators } from '@angular/forms';
import {Sort,MatSort} from '@angular/material/sort';
import { Employee } from './employee';
import {MatTableDataSource} from '@angular/material/table';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  columndefs : any[] = ['Emp_Id','Emp_Name',"Emp_Gender","Emp_Deptname","Emp_Salary"];
  @ViewChild(MatSort) sort: MatSort;
  title = 'HttpClient';
  emplist : any;
  deptlist :any;
  myform : FormGroup;
  result : string;
  submitted : boolean = false;
  btntxt : string = "Create";
  public dataSource = new MatTableDataSource<Employee>();
  //constructor
  constructor(public serviceref : EmployeeinfoService, frmbldr : FormBuilder){
    this.myform = frmbldr.group({
      Emp_Name : new FormControl("",Validators.required),
      Emp_Gender : new FormControl("",Validators.required),
      Emp_Deptno : new FormControl("",Validators.required),
      Emp_Salary : new FormControl("",Validators.required),
      
    })
  }

  //ngOnInit
  ngOnInit(){
    this.dataSource.sort = this.sort;
    //Get Employees:
    this.GetEmployeesData();
    
   }
}

Currently, I'm able to view the data and columns with sort effect on mouseover, but the sorting is not getting happend



Solution 1:[1]

Mb try put sort after this.dataSource = new MatTableDataSource('data'); //geted data from request or another source! this.dataSource.sort = this.sort;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ?????? ???????