Adhum
Adhum

Reputation: 117

Custom sorting in ngTable using AngularJs

I am looking forward for custom sorting in ngtable, but according to http://ng-table.com/#/
replacing default sorting algorithm with a custom sort is still pending. In my case, I want to sort dataset with respect to days of week i.e, from starting from Monday and ending at Sunday.

Is there any way to do this?

Any kind of help and suggestions will be appreciated.

Upvotes: 3

Views: 1943

Answers (1)

Chris Story
Chris Story

Reputation: 1197

On your day, you could have a column display one property and actually sort on another. I would suggest having an object with dayName and daySort, where dayName would be the day of the week ("Monday") and the daySort would be the sort order (1). See the working CodePen below.

http://codepen.io/storytimesolutions/pen/NNdvzO

Table:

<table ng-table="demo.tableParams" show-filter="true" class="table table-bordered table-striped">
      <tr ng-repeat="row in $data track by row.id">
        <td title="'Day of the Week'" filter="{dayName: 'text'}" sortable="'daySort'">{{row.dayName}}</td>
        <td title="'Workout'" filter="{workoutPlan: 'text'}" sortable="'workoutPlan'">{{row.workoutPlan}}</td>
      </tr>
</table>

Stubbed Data:

data: [
        {id: 1, dayName: "Monday", daySort: 1, workoutPlan: "Run"},
        {id: 2, dayName: "Thursday", daySort: 4, workoutPlan: "Swim"},
        {id: 3, dayName: "Friday", daySort: 6, workoutPlan: "Jog"},
        {id: 4, dayName: "Friday", daySort: 6, workoutPlan: "Tennis"},
        {id: 5, dayName: "Saturday", daySort: 7, workoutPlan: "Volleyball"}
      ]

Upvotes: 1

Related Questions