11

Though i'm very much new to angular, i'm facing some difficulties using ngx-DataTable. I am using simple ngx-DataTable for simple operations. The problem is on a column, sort is not working though i've declared attr as [sortable]=true. Here's code. Table Definition:

 <ngx-datatable
                [columns]="columns"
                [columnMode]="'force'"
                [headerHeight]="40"
                [footerHeight]="50"
                [rowHeight]="'auto'"
                [limit]="10"
                [rows]='contacts'>

DataTable Contains two columns and Definitions are as as follows.

    <ngx-datatable-column
                        [width]="50"
                        [resizeable]="true"
                        [sortable]="true"
                        [draggable]="true"
                        [canAutoResize]="true" name="Name">
       <ng-template let-row="row" ngx-datatable-cell-template>
          <span>{{row.first_name}}</span>
       </ng-template>
   </ngx-datatable-column>

    <ngx-datatable-column
                        [width]="50"
                        [resizeable]="true"
                        [sortable]="true"
                        [draggable]="true"
                        [canAutoResize]="true" name="Actions">
        <ng-template let-row="row" let-rowIndex="rowIndex" ngx-datatable-cell-template>
      <!--Template Here-->
        </ng-template>
   </ngx-datatable-column>

I just want to make my name column sortable. Please help me guys. Thanks in advance.

Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
Yash Majithiya
  • 861
  • 1
  • 8
  • 20

1 Answers1

28

Well it solved. Actually it can't find the values where it can make column sort. so i just written prop='first_name' in ngx-datatable-column declaration to let it know what is being to sort, like this.

<ngx-datatable-column
    [width]="50"
    [resizeable]="true"
    [sortable]="true"
    [draggable]="true"
    [canAutoResize]="true" name="Name" prop="first_name">
</ngx-datatable-column>
Yash Majithiya
  • 861
  • 1
  • 8
  • 20
  • This doesn't work for me. Also why is the value of prop in single quotes instead of double quotes like everything else?! – emirhosseini Sep 28 '18 at 02:08
  • well quote doesn't matters in this. I can check the issue if you share the code. @emirhosseini – Yash Majithiya Sep 28 '18 at 05:16
  • 1
    The explanation is that, by default, the `prop` value will be the camelcase version of the `name` (see the documentation https://swimlane.gitbook.io/ngx-datatable/api/column/inputs#prop-string) but you are using a snakecase name. – foobar443 Jan 03 '19 at 15:22
  • Can we create a custom sort for a particular column itself? – Vishnu S Babu Apr 23 '20 at 05:47
  • @VishnuSBabu Yes, we can create custom sorting on column itself. We will have to use comparator property with ngx-datatable-column. – Niraj Tathe Apr 16 '21 at 06:34