0
votes

How to develop sortable column by click on the header?

I am trying to do like in the documentation, but it does not work.

 <ngx-datatable
    #table
    ...
    [rows]='vendors'>

    <ngx-datatable-column name="VENDOR" [flexGrow]="1">
      <ng-template let-column="column" let-sort="sortFn" let-sortDir="sortDir">
        <span (click)="sort($event, sortDir, sortFn)">{{column.name}}</span>
      </ng-template>
      <ng-template let-row="row" let-value="value" ngx-datatable-cell-template>
        <div class="vendor-name">{{row.vendorName}}</div>
      </ng-template>
    </ngx-datatable-column>
2
try <ngx-datatable [sorts]="[{prop: 'name', dir: 'desc'}]">Sooriya Dasanayake
it works, but not by clicking on headerMax K
it works, but not by clicking on headerMax K

2 Answers

1
votes

Sorting does not work if you use ng-template for defining your column header. Default sorting only works if you created a basic column with only the ngx-datatable-cell-template attribute.

This works:

  <ngx-datatable-column name="Navn" prop="name" [flexGrow]="3">
    <ng-template let-value="value" ngx-datatable-cell-template>
      <span [attr.aria-label]="value">{{ value }}</span>
    </ng-template>
  </ngx-datatable-column>

this doesn't:

  <!-- Name -->
  <ngx-datatable-column name="Navn" prop="name" [flexGrow]="3">
    <ng-template let-column="column" ngx-datatable-header-template>
      <span aria-label="Name">{{ column.name }}</span>
    </ng-template>

    <ng-template let-value="value" ngx-datatable-cell-template>
      <span [attr.aria-label]="value">{{ value }}</span>
    </ng-template>
  </ngx-datatable-column>
0
votes

First of all, ngx-datatable by default supports Sorting. Dont pass any parameter to sort function. It is not required. Do this:

<ngx-datatable-column name="VENDOR" [flexGrow]="1" [sortable]="true">