1

如何按子属性订购项目列表"active" : true?第一个节点是为每个朋友动态插入的 id。

<li data-ng-repeat="friend in friends|orderBy:'friend.active':true">
        <span class="name">{{friend.name}}</span>
        <span class="phone">{{friend.lastName}}</span>
</li>

$scope.friends = {
    "12345": {
        "name": "Dhiraj",
        "lastName": "Agarwal",
        "active" : false
    },
    "23456": {
        "name": "Poonam",
        "lastName": "Mittal",
        "active" : false
    },
    "34567": {
        "name": "Suresh",
        "lastName": "Bansal",
        "active" : true
    },
    "45678": {
        "name": "Usha",
        "lastName": "Goel",
        "active" : false
    }
};
4

2 回答 2

0

得到了解决方案

http://justinklemm.com/angularjs-filter-ordering-objects-ngrepeat/

github.com/fmquaglia/ngOrderObjectBy

创建了一个新过滤器,然后用于订购它

plnkr:plnkr.co/edit/4eCq7yaVyXGZB8etLZhN?p=preview

<li data-ng-repeat="friend in friends|orderObjectBy:'active':true">

app.filter('orderObjectBy', function() {
  return function(items, field, reverse) {
    var filtered = [];
    angular.forEach(items, function(item) {
      filtered.push(item);
    });
    filtered.sort(function (a, b) {
      return (a[field] > b[field] ? 1 : -1);
    });
    if(reverse) filtered.reverse();
    return filtered;
  };
});
于 2015-06-05T18:30:54.490 回答
-1

常规 ng-filter orderBy 只接受数组。

使用这种数据可以正常工作:

 $scope.friends = [{
            "name": "Dhiraj",
                "lastName": "Agarwal",
                "active": false

    },  {
            "name": "Poonam",
                "lastName": "Mittal",
                "active": false

    },{
            "name": "Suresh",
                "lastName": "Bansal",
                "active": true

    }, {
            "name": "Usha",
                "lastName": "Goel",
                "active": false

    }];

jsfiddle 演示

编辑:如果您不想使用数组,可以使用此角度问题中描述的解决方法

过滤器(未调整并使用下划线):

app.filter('toArray', function() { return function(obj) {
    if (!(obj instanceof Object)) return obj;
    return _.map(obj, function(val, key) {
        return Object.defineProperty(val, '$key', {__proto__: null, value: key});
    });
}});

示例标记:

<div ng-repeat="val in object | toArray | orderBy:'priority' | filter:fieldSearch">
  {{val.$key}} : {{val}} 
</div>
于 2015-06-05T18:37:08.040 回答