Exclude item on orderBy

Say I have 4 elements in ng-repeat. How can I exclude one element 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 do, say id: 3 always display as the first element?

demonstration plunker

+4
source share
1 answer

You can create a function to change the value you sort (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>
+8
source

Source: https://habr.com/ru/post/1539867/


All Articles