最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng-repeat orderBy keeping fixed top value - Stack Overflow

programmeradmin4浏览0评论

see the fiddle here /

i want to get one i item on the top of the list and the rest in alphabetical order:

<div ng-controller="MyCtrl">
 <ul>
  <li ng-repeat="value in name | orderBy:'name'">{{value.name}} </li>   
   </ul>    

  </div>

In my controller i have the following

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

function MyCtrl($scope) {
    $scope.name=[
{name:'zani',country:'Norway'},
{name:'aege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]


}

what i want is the name "kai" to e first and then the rest in alphabetical order.

================Edit===============

now i have played and got the following

in my view:

<div ng-controller="MyCtrl">
 <ul>
  <li ng-repeat="value in name | orderBy:myValueFunction ">{{value.name}} </li>   
   </ul>    

  </div>

in my controller :

 $scope.myValueFunction = function(value) {
    if(value.name == "kai"){

        return value.name;
    }else{
        //what todo here so the rest of the list is sorted alphabetically
    }
}        

see the fiddle here http://jsfiddle/prantikv/gcz4gwgw/1/

i want to get one i item on the top of the list and the rest in alphabetical order:

<div ng-controller="MyCtrl">
 <ul>
  <li ng-repeat="value in name | orderBy:'name'">{{value.name}} </li>   
   </ul>    

  </div>

In my controller i have the following

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

function MyCtrl($scope) {
    $scope.name=[
{name:'zani',country:'Norway'},
{name:'aege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]


}

what i want is the name "kai" to e first and then the rest in alphabetical order.

================Edit===============

now i have played and got the following

in my view:

<div ng-controller="MyCtrl">
 <ul>
  <li ng-repeat="value in name | orderBy:myValueFunction ">{{value.name}} </li>   
   </ul>    

  </div>

in my controller :

 $scope.myValueFunction = function(value) {
    if(value.name == "kai"){

        return value.name;
    }else{
        //what todo here so the rest of the list is sorted alphabetically
    }
}        
Share Improve this question edited Sep 3, 2015 at 15:37 krv asked Sep 3, 2015 at 15:08 krvkrv 2,9208 gold badges45 silver badges83 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

You can add a "pinned" variable to your array item, and make it like this:

$scope.name=[
{name:'zani',country:'Norway', pinned: false},
{name:'aege',country:'Sweden', pinned: false},
{name:'Kai',country:'Denmark', pinned: true}]

And then change you ng-repeat accordingly:

<li ng-repeat="value in name | orderBy:['pinned','name']">{{value.name}} </li>

Now as "pinned" has the first order priority, 'kai' will always be the first in the loop.

Above answer is good to implement but as you don't have an option to edit a response then you follow easy workaround like I mentioned below. Else you could go for creating custom filter.

Markup

<div ng-controller="MyCtrl">
  <ul>
    <li ng-repeat="single in name | filter:{'name':'Kai'}">{{single.name}} </li>  
    <li ng-repeat="value in name | filter:{'name':'!Kai'}" | orderBy:'name'">{{value.name}} </li>   
  </ul>    
</div>

for those ing to this question i got it to work: Working fiddle here: http://jsfiddle/prantikv/gcz4gwgw/7/

so in my view i have the following:

<div ng-controller="MyCtrl">
 <ul>
  <li ng-repeat="value in name | orderBy:myValueFunction ">{{value.name}} </li>   
   </ul>    

  </div>

in my controller:

function MyCtrl($scope) {
$scope.name=[
    {name:'zani',country:'Norway'},
    {name:'aege',country:'Sweden'},
    {name:'Kai',country:'Denmark'}];

   $scope.myValueFunction = function(value) {

    if(value.name == "Kai"){
            return -1; //skip this sort and place at top
    }else{
        return value.name;//return the default sort
    }
   }  
 }

This works with alphabetical sort as it is the default sort order

发布评论

评论列表(0)

  1. 暂无评论