2
我有以下对象,并希望创建一个过滤器按ParentCategoryID分组。AngularJs集团通过数据和显示使用过滤器
[
{id : 116, Desciption : 'Item1', ParentID : 1, parent : 'Parent1'},
{id : 117, Desciption : 'Item2', ParentID : 2, parent : 'Parent2'},
{id : 118, Desciption : 'Item3', ParentID : 2, parent : 'Parent2'},
{id : 119, Desciption : 'Item4', ParentID : 1, parent : 'Parent1'}
]
所以结局会是
Parent1
Item1
Item4
Parent2
Item2
Item3
我的过滤器目前看起来是这样的:
productDetailsApp.filter('groupBy', function() {
return function(list, group_by) {
var filtered = [];
var prev_item = null;
angular.forEach(list, function(item) {
if (prev_item !== null) {
if (prev_item[group_by] === item[group_by]) {
filtered.push(item);
}
}
prev_item = item;
});
console.log(filtered);
return filtered;
};
});
和HTML:
<div data-ng-repeat="d in details | groupBy:'ParentID'">
<h2 >{{d.parent}}</h2>
<li>{{d.Desciption}}</li>
</div>
但这仅输出输出最后元素作为fi的prev_item第一项始终为空:
Parent1
Item4
Parent2
Item3
有人可以帮助我。
完美..谢谢! – Zaki