ng-repeat group每天的日期字段

时间:2015-05-28 13:32:19

标签: angularjs angularjs-ng-repeat angular-filters

我正在尝试groupBy一个JS日期对象的日期。我想每天对数据进行分组。
我有什么想法可以在视图中做到这一点吗?

$scope.data = {
  messages: [
    {
      date: new Date(1432811030 * 1000),
      message: 'Some text 01'
    },
    {
      date: new Date(1432731600 * 1000),
      message: 'Some text 02'
    },
    {
      date: new Date(1432819703 * 1000),
      message: 'Some text 03'
    }
  ]
};

在我看来,我使用:

<div ng-repeat="(key, value) in data.messages | groupBy: 'date'">
  <div>{{ key }}</div>
  <div ng-repeat="message in value">
    <div>{{ message.message }}</div>
  </div>
</div>

我在mu项目中包含了这个angular.filter模块:https://github.com/a8m/angular-filter

问题是我现在按日期/时间而不是每天进行分组。知道怎么样?

2 个答案:

答案 0 :(得分:6)

DEMO:http://plnkr.co/edit/djPMu4UH9t9BeGwBcUMI

屏幕截图https://www.dropbox.com/s/9z5t6nsp3x1266s/Screenshot%202015-05-28%2019.50.44.png?dl=0

<强> HTML

<body ng-app="myApp" ng-controller="myController">
  <div ng-repeat="(key, value) in testData.messages">
    <div>{{ key }}</div>
    <div ng-repeat="message in value">
      <div>{{ message }}</div>
    </div>
  </div>
</body>

<强> JS

var app = angular.module('myApp', []);
app.controller('myController', function($scope) {

  $scope.data = {
    messages: [
      {
        date: new Date(1432811030 * 1000),
        message: 'Some text 01'
      },
      {
        date: new Date(1432731600 * 1000),
        message: 'Some text 02'
      },
      {
        date: new Date(1432819703 * 1000),
        message: 'Some text 03'
      }
    ]
  };


  $scope.convertTo = function (arr, key, dayWise) {
    var groups = {};
    for (var i=0;l= arr.length, i<l;i++) {
      if (dayWise) {
        arr[i][key] = arr[i][key].toLocaleDateString();
      }
      else {
        arr[i][key] = arr[i][key].toTimeString();
      }
      groups[arr[i][key]] = groups[arr[i][key]] || [];
            groups[arr[i][key]].push(arr[i]);
    }
    return groups;
    };

     $scope.testData = {};
     angular.copy($scope.data, $scope.testData);

    $scope.testData.messages = $scope.convertTo($scope.testData.messages, 'date', true);
    console.log($scope.testData)

});

答案 1 :(得分:3)

这里有两种方法。一种是使用group by。另一个我想要的是按天预先分组我的数据,并且有一个“哑”视图,它不进行分组,而是分组在服务中预先完成或者至少在控制器。 我会添加一个plunkr来演示。

http://plnkr.co/edit/z1Y4147sCw2cCAfunHwg?p=preview

如果你有这样的数据:

vm.data = [
    {day: new Date(1432811030000), msg: 'someText'}, 
    {day: new Date(1432811030000), msg: 'someText2'},
    {day: new Date(1432811030000), msg: 'someText3'},
    {day: new Date(1432731600000), msg: 'someTextDay2'},
    {day: new Date(1432731600000), msg: 'someTextDay2-1'}, 
    {day: new Date(1432731600000), msg: 'someTextDay2-2'},
    {day: new Date(1432819703000), msg: 'someTextDay3'},
    {day: new Date(1432819703000), msg: 'someTextDay3'}
  ];

以下是相关代码:

 <ul data-ng-repeat="(key, val) in vm.data | groupBy: 'day'">
      Day: {{key}}
      <li data-ng-repeat="msg in val">
         {{msg.msg}}
      </li>
  </ul>

而且,另一方面,如果您要对数据进行预分组,则视图中的相关代码将如下所示:

<ul data-ng-repeat="(key, val) in vm.groupedData">
   Day: {{key}}
   <li data-ng-repeat="msg in val">
      {{msg.msg}}
   </li>
</ul>

以下是每天对它们进行分组的js代码:

 var groupByDate = function groupByDate(messages){
      var grouped = {};
      _.forEach(messages, function(msg){
          var actualDay = new Date(msg.day - (msg.day % 86400000));
          if ( !grouped[actualDay] ){
            grouped[actualDay] = [];
          }
          grouped[actualDay].push(msg);
      });

      return grouped;
  };