AngularJs ng-repeat orderBy不适用于嵌套对象属性

时间:2015-01-05 11:59:35

标签: javascript angularjs

我试图重复嵌套对象属性并对它们进行排序,但排序并不适合我。

我已经看到了这个: How to orderby in AngularJS using Nested property

但json结构不同,我无法使其工作。

Plunker

我的代码:

   <div ng-repeat="item in data | orderBy:order.allListPosition">
       <div>{{item.name}} - {{item.order}}</div>
   </div>

范围:

   $scope.data = {
           "78962": {
                 "id": "78962",
                 "name": "item 2",
                 "type": "blind",
                 "order": {
                       "allListPosition": "008",
                       "catListPosition": "059"
                       },
                 "data": {
                       "status": "stop",
                       "percent": 20
                       },
                 "longPress": {
                       "item": "78966",
                       "active": true
                      }
           },
            "78963": {
                   "id": "78963",
                   "name": "item 3",
                   "type": "coolmaster",
                   "order": {
                         "allListPosition": "053",
                         "catListPosition": "001"
                    },
                    "data": {
                           "status": 1,
                           "temp": 16,
                           "point": 25,
                           "mode": "cool",
                           "fan": 3
                          },
                 "longPress": {
                           "item": "78966",
                           "active": false
                            }
               }
            };

任何人都可以告诉我我做错了什么吗?

非常感谢

阿维

4 个答案:

答案 0 :(得分:14)

orderBy在这里工作的原因有两个:

  • orderBy仅适用于数组,但您将它应用于普通对象。
  • 要按表达式排序,您需要为表达式赋予orderBy字符串值。你给它order.allListPosition,它等于$scope.order.allListPosition(不存在)。

要解决第一个问题,请添加数据对象数组:

$scope.dataArray = Object.keys($scope.data)
  .map(function(key) {
    return $scope.data[key];
  });

要解决第二个问题(并合并上面的dataArray):

<div ng-repeat="item in dataArray | orderBy:'order.allListPosition'">

http://plnkr.co/edit/BXgYPTElSM3sjvLg30CL?p=preview

答案 1 :(得分:2)

您可以创建一个自定义过滤器,以按所需的属性进行排序。

myApp.filter('customorder', function() {
   return function(items) {  
    items.sort(function(a,b){
        // Make sure we are comparing integers
        var aPos = parseInt(a.order.allListPosition);
        var bPos = parseInt(b.order.allListPosition);

        // Do our custom test
        if (aPos  > bPos ) return 1;
        if (aPos < bPos) return -1;         
        return 0; 
    })
   }
 });

你的HTML看起来像

<div ng-repeat="item in data | customorder">
   <div>{{item.name}} - {{item.order}}</div>
</div>

你应该始终认为角度不是一种重新语言。您通常使用的过滤器是built in filters。但是,您可以在需要时尽快使用自己的过滤器!

答案 2 :(得分:2)

您的data对象是对象的对象,而不是对象数组。

因此,orderBy无法正常工作,因为它只与数组兼容。

我已更新您的data对象以使其正常工作:

$scope.data = [
             {              
              "id": "78961",
              "name": "item 1",
              "type": "blind",
              "order":{allListPosition:"093",catListPosition: "009"}, 
              "data": {
                  "status": "up",
                  "percent": 80
              },
              "longPress": {
                  "item": "78966",
                  "active": true
              }

            },
            {  
              "id": "78962", 
              "name": "item 2",
              "type": "blind",
                "order":{allListPosition:"008",catListPosition: "059"},
              "data": {
                  "status": "stop",
                  "percent": 20
              },
              "longPress": {
                  "item": "78966",
                  "active": true
              }
            },
            {

              "id": "78963",
              "name": "item 3",
              "type": "coolmaster",
                "order":{allListPosition:"053",catListPosition: "001"},
              "data": {
                  "status": 1,
                  "temp": 16,
                  "point": 25,
                  "mode": "cool",
                  "fan": 3
              },
              "longPress": {
                  "item": "78966",
                  "active": false
              }

            }];

在HTML中:

<div ng-repeat="item in data | orderBy:'order.allListPosition'">
   <div>{{item.name}} - {{item.order}}</div>
</div>

Plunker

答案 3 :(得分:-3)

我很确定这应该是:

<div ng-repeat="item in dataArray | orderBy:'item.order.allListPosition'">