另请参阅这篇文章,它提供了执行此分组的指令:Is it possible to .sort(compare) and .reverse an array in angularfire?
您可以在这里采取一些方法。
按日期结构化的数据
如果记录总是被这个结构获取,你可以这样存储它们;
/users/usera/tasks/week1/monday/taska/...
然后您可以简单地将它们作为对象在tasks 级别获取,您将获得一个预先排序的 JSON 对象,其值嵌套在适当的级别。
这种方法与 AngularFire 不高度兼容,AngularFire 旨在绑定对象或集合,而不是嵌套的数据树,但应该小心使用。
使用优先级
第二种方法是在任务上使用add priorities,这将是一个纪元时间戳。现在,当您想要获取它们时,您可以 startAt/endAt 树中的特定点,并获取记录。每个都会有一个时间戳,您可以使用它来识别星期和日期。
var ref = new Firebase(ref).startAt(twoWeeksAgo).endAt(nextThursday);
$scope.list = $fireabse(ref).$asArray();
但是,这不会对条目进行分段。您需要检查 ng-repeat 中的每个条目并动态添加标题:
// in the controller
var last = null;
$scope.priorityChanged(priority) {
/**
* here we would use a library like moment.js to parse the priority as a date
* and then do a comparison to see if two elements are for the same day, such as
**/
var current = moment(priority).startOf('day');
var changed = last === null || !last.isSame(current);
last = current;
return changed;
};
$scope.getDayName = function($priority) {
return moment($priority).format('dddd');
};
<!-- in the view -->
<li ng-repeat="item in list" ng-init="changed = priorityChanged(item.$priority)">
<h3 ng-show="changed">{{getDayName(item.$priority)}}</h3>
{{item|json}}
</li>
这种方法很容易与 AngularFire 兼容。
滚动你自己的列表
最后一种方法是删除 AngularFire 并推出自己的方法。例如,如果我们在每个任务中存储星期和工作日,我们可以执行以下操作:
app.service('DatedList', function($timeout) {
return function(pathToList) {
var list = {};
pathToList.on('child_added', function(snap) {
$timeout(function() { // force Angular to run $digest when changes occur
var data = snap.val();
var week_number = data.week;
var week_day = data.day;
list[week_number][week_day] = data;
});
});
//todo: write similar processing for child_changed and child_removed
return list;
}
});
app.controller('ctrl', function($scope, DatedList) {
var listRef = new Firebase(URL).limit(500);
$scope.weeks = DatedList(listRef);
});
<div controller="ctrl">
<div ng-repeat="(week, days) in weeks">
<h1>{{week}}</h1>
<div ng-repeat="(day, items) in days">
<h2>{{day}}</h2>
<div ng-repeat="item in items">
{{item|json}}
</div>
</div>
</div>
</div>