Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatable custom sort or sort on hidden span #521

Closed
elmartino opened this issue Apr 25, 2017 · 2 comments · Fixed by #1225
Closed

Datatable custom sort or sort on hidden span #521

elmartino opened this issue Apr 25, 2017 · 2 comments · Fixed by #1225
Milestone

Comments

@elmartino
Copy link

elmartino commented Apr 25, 2017

Hi guys,

I think it's a feature request, but maybe this is already possible: Is there a possibility to sort on a hidden span element of the <ng-template tdDataTableTemplate="column"> ?

I have a column displaying date data transformed by a pipe like:
row1: 6 years ago
row2: 3 months ago
row3: 3 hours ago
row4: 6 weeks ago

The sorting here doesn't do what it's supposed to do as you can imagine. If i could add a hidden span to the ng-template with the original date value and this is sortable, it would really help!

I hope you understand my question and you can point me in the right direction.

Thanks!!

Regards,
Martin

@michael-lang
Copy link

In your sort function, you can do whatever logic you want for sort on a given field.

@Component({
  selector: 'app-kitchen-sink-page',
  templateUrl: './kitchen-sink-page.component.html',
  styleUrls: ['./kitchen-sink-page.component.scss']
})
export class KitchenSinkPageComponent  {
  displayResults = [
    {Name: 'Material Design 2', Points: 49, Col3: 'Data X'},
    {Name: 'Angular CLI', Points: 42, Col3: 'Data Y'},
    {Name: 'NGRX Store', Points: 35, Col3: 'Data Z'}
  ];
  columns: ITdDataTableColumn[] = [
    {name: 'Name', label: 'Name', sortable: true, tooltip: ''},
    {name: 'Points', label: 'Points', numeric: true, sortable: true, tooltip: ''},
    {name: 'Col3', label: '#3', sortable: true, tooltip: ''}
  ];
  sortBy = '';
  sortOrder = '';
  sort(sortEvent: ITdDataTableSortChangeEvent): void {
    console.log('sort', sortEvent);
    this.sortBy = sortEvent.name;
    this.sortOrder = sortEvent.order === TdDataTableSortingOrder.Ascending ? 'ASC' : 'DESC';
    this.displayResults.sort((x, y) => {
      let a = x;
      let b = y;
      if (this.sortOrder.toLowerCase() === 'asc') {
        a = y;
        b = x;
      }
      if (this.sortBy.toLowerCase() === 'name') {
        return (a.Name || '').toLowerCase().localeCompare((b.Name || '').toLowerCase());
      } else if (this.sortBy.toLowerCase() === 'points') {
        return (a.Points + '').localeCompare((b.Points + ''));
      } else if (this.sortBy.toLowerCase() === 'col3') {
        return (a.Col3 || '').toLowerCase().localeCompare((b.Col3 || '').toLowerCase());
      }
      return (a.Name || '').toLowerCase().localeCompare((b.Name || '').toLowerCase());
    });
  }
}

Notice those else statements checking the 'sortBy' field. the return statement following doing a localCompare can choose a different field to do the actual sort on.

Here's the matching view.

      <td-data-table
        #dataTable
        [data]="displayResults"
        [columns]="columns"
        [sortable]="true"
        [sortBy]="sortBy"
        [sortOrder]="sortOrder"
        (sortChange)="sort($event)">
      </td-data-table>

@emoralesb05
Copy link
Contributor

Closing this since a solution was provided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants