AngularJs ng-repeat orderBy 不适用于嵌套对象属性
AngularJs ng-repeat orderBy not working for nested object properties
我正在尝试 ng-repeat 嵌套对象属性并对它们进行排序,但排序对我不起作用。
我看过这个:
How to orderby in AngularJS using Nested property
但是 json 结构不同,我无法让它工作。
我的代码:
<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
}
}
};
任何人都可以告诉我我做错了什么吗?
非常感谢
阿维
您可以创建自定义过滤器以按嵌套属性排序。
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>
您应该始终认为 angular 不是限制性语言。您通常使用的过滤器是 built in filters。但您可以在需要时尽快使用自己的滤镜!
您的 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>
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'">
我很确定这应该是:
<div ng-repeat="item in dataArray | orderBy:'item.order.allListPosition'">
我正在尝试 ng-repeat 嵌套对象属性并对它们进行排序,但排序对我不起作用。
我看过这个: How to orderby in AngularJS using Nested property
但是 json 结构不同,我无法让它工作。
我的代码:
<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
}
}
};
任何人都可以告诉我我做错了什么吗?
非常感谢
阿维
您可以创建自定义过滤器以按嵌套属性排序。
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>
您应该始终认为 angular 不是限制性语言。您通常使用的过滤器是 built in filters。但您可以在需要时尽快使用自己的滤镜!
您的 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>
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'">
我很确定这应该是:
<div ng-repeat="item in dataArray | orderBy:'item.order.allListPosition'">