2

I am using Angular Material datatable with Http request to get data.

However, it is not sorting data in Data table.

export class ContactComponent implements OnInit, AfterViewInit {
  displayedColumns: string[] = [
    "firstname",
    "middlename",
    "lastname",
    "status",
    "dateofbirth"
  ];     
  selected;
  dataSource = new MatTableDataSource<Yuvak>();

  constructor(
    private router: Router,
    private dialogService: NbDialogService,
    private contactService: ContactService
  ) { }

  @ViewChild(MatPaginator) paginator: MatPaginator;
  @ViewChild(MatSort) sort: MatSort;

  ngOnInit() {
    this.contactService.getAllUser().subscribe(
      data => {
        this.dataSource.data = data.data.user;
      },
      error => {
        console.log("There was an error while retrieving Users !!!" + error);
      }
    )
  }

  ngAfterViewInit() {
    this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
  }
      }

above is my ts file. Previously when i was loading data from local json file it was working fine inside constructor. However, now i am getting data from GET request.

2 Answers 2

1

Check for you displayed columns names with the one you are using as columnheader. Every thing else looks ok. If possible post your template file.

Try this after checking your html file.

ngOnInit() {
    this.contactService.getAllUser().subscribe(
      data => {
        this.dataSource.data = data.data.user;
this.dataSource.paginator = this.paginator;
    this.dataSource.sort = this.sort;
      },
      error => {
        console.log("There was an error while retrieving Users !!!" + error);
      }
    )
  }
Sign up to request clarification or add additional context in comments.

Comments

0

try to replace dataSource = new MatTableDataSource<Yuvak>(); with dataSource: Yuvak[] = [] and this.dataSource.data = data.data.user; with this.dataSource = data

1 Comment

i am using this "data.data.user "because i am accessing my inner nested object.

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.