totothegreat
totothegreat

Reputation: 1643

Angular order by numerous fields

I want a simple order by three fields:

name, status and owner in that order

Up until now i had this:

<tr ng-repeat='data in model.tableData|filter:model.search|orderBy:"name":model.sorted.name'>

(the model.sorted.name is toggling true false when clicked on), Now i saw i need to do something like this:

<tr ng-repeat="data in model.tableData|filter:model.search|orderBy:['name','status','owner']">

To deal with multiple fields, but how can i change it from, for example, the status from true to false, like i did with the single one?

Upvotes: 0

Views: 50

Answers (1)

sylwester
sylwester

Reputation: 16498

You can use '!' ie :

 <tr ng-repeat="data in model|filter:model.search|orderBy:['name','!status','owner']">

please demo below

var app = angular.module('app', []);

app.controller('homeCtrl', function($scope) {


  $scope.model = [{
      'name': 'a1',
      'status': true,
      'owner': 'as'
    }, {
      'name': 'a1',
      'status': false,
      'owner': 'as'
    }, {
      'name': 'a1',
      'status': false,
      'owner': 'as'
    }


  ]

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="homeCtrl">
    <h3> By status reverse </h3>
    <table>
      <tr ng-repeat="data in model|filter:model.search|orderBy:['!status','name','owner']">
        <td>{{data.status}}|{{data.owner}}| {{data.name}}</td>
      </tr>
    </table>
    <hr/>

    <h3> By status</h3>
    <table>
      <tr ng-repeat="data in model|filter:model.search|orderBy:['status','name','owner']">
        <td>{{data.status}}|{{data.owner}}| {{data.name}}</td>
      </tr>
    </table>
  </div>
</div>

Upvotes: 1

Related Questions