Search code examples
angularjsangular-filters

AngularJS ordering collection


I have a simple inbox table which consists of mails that have mailtext, sender and date attributes. I want this table to be sortable in terms of every one of these attributes when clicked to correspondent table header. Right now I am only trying to order this table in terms of sender attribute. I am using AngularJS 'orderBy' filter but nothing changes in the table. What is wrong with my code?

HTML Code Snippet:

<div class="container">
  <h2>Your Inbox</h2><br><br>
  <table ng-show="showInboxTable" class="table table-hover">
    <thead>
      <tr>
        <th>Mail</th>
        <th ng-click="sortMailsSender()">Sender</th>
        <th>Date</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="mail in mails">
        <td>{{mail.mailtext}}</td>
        <td>{{mail.sender}}</td>
        <td>{{mail.date}}</td>
      </tr>      
    </tbody>
  </table>
</div>

AngularJS Controller:

$scope.sortMailsSender = function()
{
    if ( !$window.sessionStorage.inboxCompare || $window.sessionStorage.inboxCompare == 'gt')
    {               
        console.log('filter straight'); 

        $filter('orderBy')($scope.mails, 'sender', false); 

        $window.sessionStorage.setItem('inboxCompare', 'lt');
    }
    else
    {                
        console.log('filter reverse'); 

        $filter('orderBy')($scope.mails, 'sender', true);

        $window.sessionStorage.setItem('inboxCompare', 'gt');
    }
}

Solution

  • You should assign to your $scope.mails after applying filter and syntax should be

       $scope.mails = $filter('orderBy')($scope.mails , 'sender', false);