Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude item on orderBy

Say I have 4 item in ng-repeat. How can I exclude one item in orderBy?

<li ng-repeat="item in items | orderBy:'id':true">

$scope.items = [
    {"name":"item1","id":1},
    {"name":"item2","id":2},
    {"name":"item3","id":3},
    {"name":"item4","id":4}
];

How can I make, say, id:3 always appear as the first item?

plunker demo

like image 508
user3522462 Avatar asked May 09 '14 06:05

user3522462


1 Answers

You can create a function to alter the value you sort by (plunker):

$scope.itemSortValue = function(item) {
  if (item.id == 3)
    return -1;
  return item.id;
}

Html:

<li ng-repeat="item in items | orderBy:itemSortValue">
  {{item.name}}
</li>
like image 188
Jason Goemaat Avatar answered Oct 11 '22 09:10

Jason Goemaat