I'm having some problems trying to manuallky trigger sorting the rows in a ngx-datatable. The scenario is that on small devices i'm moving some of the columns from the header to a ngx-datatable-row-detail, but when i try to invoke a customSort function, the rows in the ngx-datatable is not being sorted as i would expect.
The template looks like this:
<ngx-datatable #myTable [columnMode]="'flex'"
[rows]="_appointments"
[rowHeight]="'auto'"
[sorts]="listProps.currentSort"
(sort)="sorted($event)"
[columns]="_tableColumns">
<ngx-datatable-row-detail [rowHeight]="'auto'" #myDetailRow>
<ng-template let-row="row" let-expanded="expanded" ngx-datatable-row-detail-template>
<div class="ngx-table-toogle hidden-md">
<div [class.visible-xs]="myCol.collapse_xs" [class.visible-sm]="myCol.collapse_sm" *ngFor="let myCol of _collapsedTableColumns">
<div class="col-xs-12 col-sm-4" [class.sortable]="myCol.sortable" (click)="onCustomSort(myCol.prop,myCol.sortable)">
<div class="detail-row-header">
{{myCol.name}}
<span [class.sort-btn]="myCol.sortable" [class.sort-desc]="isSortedBy(myCol.prop,'desc')" [class.sort-asc]="isSortedBy(myCol.prop,'asc')"></span>
</div>
</div>
<div class="col-xs-12 col-sm-8">
<span class="detail-row-data">{{getVal(row,myCol.prop)}}</span>
</div>
</div>
</div>
</ng-template>
</ngx-datatable-row-detail>
And the onCustomSort method like:
onCustomSort(sortCol: string, sortable:boolean) {
if (!sortable)
return;
let curdir = this.listProps.currentSort[0].dir === SortDirection.desc ? SortDirection.asc : SortDirection.desc;
// Hmm, doesn't trigger a resorting of the table.
this.listProps.currentSort = [{ prop: sortCol, dir: curdir }];
this.table.sorts = this.listProps.currentSort;
}
I would expect that modifying this.listProps.currentSort
would trigger a resort of the table because it is bound to [sorts]="listProps.currentSort"
but nothing happens.
Any ideas?
Actually the solution was super simple, just had to add
this.table.onColumnSort({ sorts: this.listProps.currentSort });
to the onCustomSort method so it now looks like this:
onCustomSort(sortCol: string, sortable:boolean) {
if (!sortable)
return;
let curdir = this.listProps.currentSort[0].dir === SortDirection.desc ? SortDirection.asc : SortDirection.desc;
this.listProps.currentSort = [{ prop: sortCol, dir: curdir }];
this.table.onColumnSort({ sorts: this.listProps.currentSort });
}