How can I sort items by date in smart-table
Asked Answered
L

3

7

How can I sort data by date in smart-table? With st-sort it isn't so good.

<table st-table="displayedCollection" st-safe-src="playerCollection" class="table table-hover">
      <thead>
        <tr>
            <th st-sort="id" class="hover">Id</th>
            <th st-sort="firstname" class="hover">Jméno</th>
            <th st-sort="lastname" class="hover">Příjmení</th>
            <th st-sort="registrationDate" class="hover">Datum registrace</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="player in displayedCollection">
            <td class="hover">{{player.id}}</td>
            <td class="hover">{{player.firstname}}</td>
            <td class="hover">{{player.lastname}}</td>
            <td class="hover">{{player.registrationDate}}</td>
        </tr>
      </tbody>
  </table>

Thanks for answers.

Limitative answered 5/4, 2015 at 18:38 Comment(0)
O
8

It should normally work (cf documentation website). However if your registration date is a date string you should use a getter to return the date object version of it, otherwise you'll have the alphaNumeric order

in your controller

$scope.getters = {
   registrationDate:function(row) {
      return new Date(row.registrationDate);
   }
}

so you can bind your header to this getter

<th st-sort="getters.registrationDate">Datum registrace</th>
Orsay answered 7/4, 2015 at 2:11 Comment(2)
It is the correct way to do it, should be the accepted answerPhillip
i know this is an old thread, but i am facing issue with sorting too. I have date in milliseconds and when i convert it to human readable format using (new Date(1496987379155 )).toLocaleString(); , sorting doesnt work , only half of my rows are sorted correctly and other half is unsorted.Tuantuareg
V
1

Add an order by to your ng-repeat like this:

<tr ng-repeat="player in displayedCollection | orderBy:'registrationDate'">
   <td class="hover">{{player.id}}</td>
   <td class="hover">{{player.firstname}}</td>
   <td class="hover">{{player.lastname}}</td>
   <td class="hover">{{player.registrationDate}}</td>
</tr>

For sorting onclick, you could add a variable to the scope that determines the sorting and used that on the orderBy on the ng-repeat.

Something like this:

<table st-table="displayedCollection" st-safe-src="playerCollection" class="table table-hover">
  <thead>
    <tr>
        <th st-sort="id" class="hover"><a href="" ng-click="predicate = 'id'; reverse=false">Id</a></th>
        <th st-sort="firstname" class="hover"><a href="" ng-click="predicate = 'firstname'; reverse=false">Jméno</a></th>
        <th st-sort="lastname" class="hover"><a href="" ng-click="predicate = 'lastname'; reverse=false">Příjmení</a></th>
        <th st-sort="registrationDate" class="hover"><a href="" ng-click="predicate = 'registrationDate'; reverse=false">Datum registrace</a></th>
    </tr>
  </thead>
  <tbody>
    <tr ng-repeat="player in displayedCollection | orderBy:predicate:reverse">
        <td class="hover">{{player.id}}</td>
        <td class="hover">{{player.firstname}}</td>
        <td class="hover">{{player.lastname}}</td>
        <td class="hover">{{player.registrationDate}}</td>
    </tr>
  </tbody>
</table>

I created a plunker for this. You'll see if you click on a column header it'll sort the table by that column. I hope it helps.

http://plnkr.co/edit/pAJ3PpRwVk7PuTmoMjsr?p=preview

Vola answered 6/4, 2015 at 0:48 Comment(3)
Thanks, but it order by date only when is page loaded. But i want to sort on click.Nigercongo
Ok, let me look into that.Vola
@VáclavPavlíček Checkout the edited response and plunker at plnkr.co/edit/pAJ3PpRwVk7PuTmoMjsr?p=previewVola
M
0

I will add another possible solution related to @laurent answer. His solution wasn't working in my case so I changed a bit the getter:

$scope.getters = {
   registrationDate:function(row) {
      return (new Date(row.registrationDate)).getTime();
   }
}

getTime() returns the number of milliseconds since 1970/01/01, so the column will be ordered by number instead of date (it wasn't working in my case and I didn't figured out why) and the result is the same.

Microscopium answered 5/9, 2016 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.