1

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?

1 Answer 1

2

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 });

}
Sign up to request clarification or add additional context in comments.

4 Comments

Are you able to provide a full code? I'm not understanding what is "this.table".
No, that piece of code is long gone - but this.table points to #myTable - the ngx-datatable instance.
No worries mate, I found a solution. Thank you for your replay
@MikNiller dude do you know how to fix the resize issue with misplaced columns?

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.