angularJS 范围 orderBy 不排序 children,只是 parent
angularJS scope orderBy does not sort children, just the parent
当我使用
$scope.data = $filter('orderBy')(data,'title');
parent 已排序,children 未排序。
如果我的数组有 children 怎么办?我如何订购 parent 和 children?
我的阵列:
data = [
{
"id": 1,
"title": "abc",
"items": [
{
"id": 3,
"title": "abc2",
"items": []
},
{
"id": 4,
"title": "abc1",
"items": []
}
]
},
{
"id": 2,
"title": "cde",
"items": [
{
"id": 5,
"title": "cde2",
"items": []
},
{
"id": 6,
"title": "cde1",
"items": []
}
]
}
]
提前致谢。
如果您只是想在视图中使用 orderBy,我会建议您使用
数据-ng-重复="item in data | orderBy: 'colum you want'"
它不应该对子对象中的任何内容进行排序。
对于子对象内的排序,递归地对您的 'items' 进行排序,对于每个子对象,使用 $filter('orderBy') 或类似的代码,使用您自己的自定义比较函数。
var orderByFieldName = 'title',
childPropertyForSorting = 'items',
s = function (a, b) {
//Revise comparing function depends on direction of sorting and your wishes
if (a[orderByFieldName] > b[orderByFieldName])
return 1;
if (a[orderByFieldName] < b[orderByFieldName])
return -1;
return 0;
},
f = function (o) {
if (o.hasOwnProperty(childPropertyForSorting) && o[childPropertyForSorting] instanceof Array) {
o[childPropertyForSorting].sort(s);
o[childPropertyForSorting].forEach(f);
}
};
if (originalData instanceof Array) {
originalData.sort(s);
originalData.forEach(f);
}
原因是子项处于另一个级别并且被视为该对象内的另一个键值对,您可以使用与 for 循环中使用的过滤器相同的过滤器,它将循环每个数据数组项并对子项进行排序这样:
//filter the parent level by title
$scope.data = $filter('orderBy')(data,'title');
//copy the sorted array in temp variable
var temp = angular.copy($scope.data);
//sort the children by title while aggregating each array item and then storing the output in $scope.
for(var i=0;i<data.length;i++){
$scope.data[i].items = $filter('orderBy')(temp[i].items,'title');
}
当我使用
$scope.data = $filter('orderBy')(data,'title');
parent 已排序,children 未排序。
如果我的数组有 children 怎么办?我如何订购 parent 和 children?
我的阵列:
data = [
{
"id": 1,
"title": "abc",
"items": [
{
"id": 3,
"title": "abc2",
"items": []
},
{
"id": 4,
"title": "abc1",
"items": []
}
]
},
{
"id": 2,
"title": "cde",
"items": [
{
"id": 5,
"title": "cde2",
"items": []
},
{
"id": 6,
"title": "cde1",
"items": []
}
]
}
]
提前致谢。
如果您只是想在视图中使用 orderBy,我会建议您使用
数据-ng-重复="item in data | orderBy: 'colum you want'"
它不应该对子对象中的任何内容进行排序。 对于子对象内的排序,递归地对您的 'items' 进行排序,对于每个子对象,使用 $filter('orderBy') 或类似的代码,使用您自己的自定义比较函数。
var orderByFieldName = 'title',
childPropertyForSorting = 'items',
s = function (a, b) {
//Revise comparing function depends on direction of sorting and your wishes
if (a[orderByFieldName] > b[orderByFieldName])
return 1;
if (a[orderByFieldName] < b[orderByFieldName])
return -1;
return 0;
},
f = function (o) {
if (o.hasOwnProperty(childPropertyForSorting) && o[childPropertyForSorting] instanceof Array) {
o[childPropertyForSorting].sort(s);
o[childPropertyForSorting].forEach(f);
}
};
if (originalData instanceof Array) {
originalData.sort(s);
originalData.forEach(f);
}
原因是子项处于另一个级别并且被视为该对象内的另一个键值对,您可以使用与 for 循环中使用的过滤器相同的过滤器,它将循环每个数据数组项并对子项进行排序这样:
//filter the parent level by title
$scope.data = $filter('orderBy')(data,'title');
//copy the sorted array in temp variable
var temp = angular.copy($scope.data);
//sort the children by title while aggregating each array item and then storing the output in $scope.
for(var i=0;i<data.length;i++){
$scope.data[i].items = $filter('orderBy')(temp[i].items,'title');
}