我有一个非常简单的场景,其中有一组记录可用,我需要在一个简单的 ng-repeat 中显示它们。但是,我需要按属性分组的记录,而我的目标不是不必更改集合即可完成此分组。我的想法是可以应用某种类型的过滤器,但实际上过滤器、过滤器、数据和不分组。有没有办法收集并简单地分组和重复?
真正的 hack-ish jsFiddle 就在这里,我正在尝试做。
http://jsfiddle.net/bryangrimes/RufQh/5/
简而言之,这个想法是这样的:
<ul>
<li ng-repeat="log in logs grouped by log.dept">
<h4>{{log.dept}}</h4>
{{log.name}} worked {{log.hours}} this week
</li>
</ul>
更新:所以最后我们已经使用 taffyDB 来容纳原始数据集,所以它只是被扩展了。
$.each($scope.logs, function() {
var log = $(this)[0];
// build angular friendly data structure
log.workLogsDB = TAFFY(log.worklogs);
log.deptLogs = [];
$.each(log.workLogsDB().distinct('department').sort(), function() {
var dept = $(this)[0].toString();
var cost = log.workLogsDB({department:dept}).sum('cost');
var hours = log.workLogsDB({department:dept}).sum('hours');
var items = log.workLogsDB({department:dept}).get();
log.deptLogs.push({
department: dept,
total_cost: cost,
total_hours: hours,
line_items: items
});
});
});
和要呈现的 HTML:
<div ng-repeat="log in logs">
<h3 onclick="$('#{{log.project}}').slideDown()">
{{log.project}} hours:{{log.hours}} cost: ${{log.cost}}
</h3>
<ul id="{{log.project}}" style="display: none;">
<div ng-repeat="log in log.deptLogs">
<span style="font-weight: bold; text-transform: capitalize;">{{ log.department }} - Team Cost: ${{ log.total_cost }}, Team Hours: {{ log.total_hours }} </span>
<li ng-repeat="line in log.line_items">
{{line.employee}} {{line.hours}} hours
</li>
<br>
</div>
</ul>
</div>