nsk
nsk

Reputation: 1463

How to write custom sort logic on sort column event in ng2-smart-table

I'm looking to hook-up sort events performed on ng2-smart-table. Followed https://akveo.github.io/ng2-smart-table/#/documentation, I see bunch of events that are exposed like rowSelect, mouseover etc but I don't see sort events published/emitted by the library. I'm thinking of changing Ng2SmartTableComponent and emit an event when (sort) is called internally. May I know if anyone did it already or is there a hack I can rely upon.

Upvotes: 1

Views: 3579

Answers (2)

boyukbas
boyukbas

Reputation: 1235

I was searching for an event to sort my data remotely and I have found a solution. Also I have some logic for page change event (remote paging). Here is what works for me.

ts

source: LocalDataSource = new LocalDataSource();

ngOnInit() {
  this.source.onChanged().subscribe((change) => {
    if (change.action === 'sort') {
      this.sortingChange(change.sort);
    }
    else if (change.action === 'page') {
      this.pageChange(change.paging.page);
    }
  });
}

html

<ng2-smart-table [settings]="settings" [source]="source"></ng2-smart-table>

This solution won't replace custom logic but it might help you solve your problem.

Upvotes: 1

Lumix
Lumix

Reputation: 319

The source of the sort in ng2-smart-table is shown on GitHub (link to code).

If you want to change the compare-Function (as used by default) you can add your own custom function in your ng2-smart-table-configuration:

columns: {
    group_name: {
        title: 'Groupname',
        compareFunction(direction: any, a: any, b: any) => {
           //your code
        }
    }
}

Upvotes: 1

Related Questions