简体   繁体   English

如何将firebase angularfire阵列分为几周和几天?

[英]How can I sort a firebase angularfire array into weeks and days?

I'm working on a todo list that groups tasks by week (based on added and completion dates) and groups tasks by the days of the week. 我正在制作一个待办事项列表,按周分组任务(根据已添加和完成日期),并按星期几分组任务。 The database structure looks like this: 数据库结构如下所示:

users
  userA
    tasks
      taskobject1
      taskobject2
      ..
  userB
    tasks
      taskobject1
      task object2

I'm using an ng-repeat to display all the tasks to the view for each user. 我正在使用ng-repeat来为每个用户显示视图的所有任务。 I would like to be able to sort them first by which week they fall into and then like this: 我希望能够先将它们排序到哪个星期,然后像这样:

#week1
--monday--
task a
task b
--tuesday--
task c
task d
..
#week2
--monday--
task a
..

Even a conceptual answer would be helpful. 即使是概念性答案也会有所帮助。 I don't know where to start exactly. 我不知道从哪里开始。

Thanks. 谢谢。

See also this post, which provides a directive to do this grouping: Is it possible to .sort(compare) and .reverse an array in angularfire? 另请参阅这篇文章,它提供了执行此分组的指令: 是否可以在angularfire中对.sort(compare)和.reverse数组进行排序?

There are a few approaches you could take here. 你可以在这里采取一些方法。

Data Structured by Date 按日期结构化的数据

If the records will always be fetched by this structure, you can just store them that way; 如果记录将始终由此结构提取,您可以按这种方式存储它们;

/users/usera/tasks/week1/monday/taska/...

Then you can simply fetch them at the tasks level as an object, and you will obtain a pre-sorted JSON object with values nested at the appropriate levels. 然后,您可以简单地在tasks级别将它们作为对象获取,并且您将获得具有嵌套在适当级别的值的预先排序的JSON对象。

This approach is not highly compatible with AngularFire, which is intended for binding objects or collections, not nested trees of data, but should work with some care. 这种方法与AngularFire不兼容,后者用于绑定对象或集合,而不是嵌套的数据树,但应该谨慎使用。

Using Priorities 使用优先权

A second approach would be to add priorities on to the tasks, which would be an epoch timestamp. 第二种方法是在任务上添加优先级 ,这将是一个纪元时间戳。 Now when you want to fetch them, you can startAt/endAt specific points in the tree, and get the records. 现在,当您想要获取它们时,您可以启动它/结束树中的特定点,并获取记录。 Each will have a timestamp on it you can use to identify the week and day. 每个都有一个时间戳,您可以用它来识别周和日。

var ref = new Firebase(ref).startAt(twoWeeksAgo).endAt(nextThursday);
$scope.list = $fireabse(ref).$asArray();

However, this does not segment the entries. 但是,这不会对条目进行细分。 You would need to either examine each entry in the ng-repeat and add headers on the fly: 您需要检查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>

This approach is readily compatible with AngularFire. 这种方法很容易与AngularFire兼容。

Roll Your Own List 滚动你自己的名单

A last approach would be to cut out AngularFire and roll your own. 最后一种方法是削减AngularFire并自己动手。 For example, if we have the week and weekday stored on each task, we could do the following: 例如,如果我们在每个任务上存储了工作日和工作日,我们可以执行以下操作:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM