I have a datatable in Angular 2 app where I want to custom sort a column.
<p-column field="eligible" header="Eligible" sortable="custom" (sortFunction)="sortColumn($event)"></p-column>
In my component file, I'm making an API call to get the sorted results from the backend based on some logic.
sortColumn(colName: any) {
let columnName = undefined !== colName.field ? colName.field : colName;
let sortObject: any = {};
if (this.sortedColumn === columnName) {
if (!this.sortAsc) {
this.sortAsc = true;
sortObject[columnName] = 'DESC';
} else {
this.sortAsc = false;
sortObject[columnName] = 'ASC';
}
} else {
this.sortedColumn = columnName;
this.sortAsc = false;
sortObject[columnName] = 'ASC';
}
this.getData(sortObject);
}
This API in return gets the whole data back and reorders the view. Now what's happening here is that this method sortColumn() keeps getting called repeatedly.
Can anyone please help me understand what might be causing this issue and how to resolve it?