AngularJS: orderBy

删除回忆录丶 提交于 2019-12-11 13:44:19

问题


So i have got a ng-repeat with some names in it. What i want to do is following. The names that are outputted to the screen are ordered alphabetically.

<div ng-repeat="n in names | orderBy:'name'"></div>

  • Anne
  • Jane
  • John
  • Mike
  • Ziggy

But is it possible to always show the name 'Mike' as the top name and order the rest alphabetically?

  • Mike
  • Anne
  • Jane
  • John
  • Ziggy

回答1:


You can write additional sorting function for this. For example:

HTML:

<div ng-repeat="n in names | orderBy:[egoSort, 'name']">{{n}}</div>

Controller:

$scope.egoSort = function(name) {
    return name === 'Mike' ? '' : name;
};

This is possible because orderBy allows you to specify several predicates using array notation.

Demo: http://plnkr.co/edit/FfpcjJBBMm52A71Pa01m?p=preview




回答2:


There is one more way...

add an attribute say order to the user Object which you want to appear at top.

var specificUserName = {name: Mike,order:0} //This name would appear first in the list

HTML would be

<div ng-repeat="n in names | orderBy:['order', 'name']">{{n}}</div>



来源:https://stackoverflow.com/questions/25909609/angularjs-orderby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!