How to sort a primeng datatable programmatically
angular, primeng
Solution
gridObject.sortColumn = gridObject.columns.find(col => col.field === paginationOptions.SortColumn);
gridObject.sortField = paginationOptions.SortColumn;
gridObject.sortOrder = (paginationOptions.SortOrder == "ASC" ? 1 : -1);
gridObject.sortSingle();
`gridObject`: it's my Datatable `paginationOptions`: it's pagination option, like sort column and sort order. `1` is used for ascending and `-1` is used for descending.
Above 4 statement will execute the sort action.
Problem
I need to be able to trigger a sort event in a p-dataTable component from its parent component. This kind of violates the "data down, actions up" principle that has guided modern web development, but I have a separate component that users will click on to trigger sorting in the primeng table, and I don't see another way to trigger this in the dataTable component API.