ng-repeat groupBy 每天的日期字段 [英] ng-repeat groupBy date field per day

查看:37
本文介绍了ng-repeat groupBy 每天的日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 groupBy 一个日期,它是一个 JS 日期对象.我想对每天的数据进行分组.
任何想法如何在视图中执行此操作?

$scope.data = {留言: [{日期:新日期(1432811030 * 1000),消息:'一些文本 01'},{日期:新日期(1432731600 * 1000),消息:'一些文本 02'},{日期:新日期(1432819703 * 1000),消息:'一些文本 03'}]};

在我看来,我使用:

<div>{{key}}</div><div ng-repeat="消息值"><div>{{ message.message }}</div>

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

问题是我现在按日期/时间而不是每天分组.知道怎么做吗?

解决方案

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

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

HTML:

<div ng-repeat="(key, value) in testData.messages"><div>{{key}}</div><div ng-repeat="消息值"><div>{{消息}}</div>

JS:

var app = angular.module('myApp', []);app.controller('myController', function($scope) {$scope.data = {留言: [{日期:新日期(1432811030 * 1000),消息:'一些文本 01'},{日期:新日期(1432731600 * 1000),消息:'一些文本 02'},{日期:新日期(1432819703 * 1000),消息:'一些文本 03'}]};$scope.convertTo = function (arr, key, dayWise) {var 组 = {};for (var i=0;l= arr.length, i

I am trying to groupBy a date which is a JS date object. I want to group the data per day.
Any ideas how I can do this in a view?

$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'
    }
  ]
};

And in my view I use:

<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>

I have this angular.filter module included in mu project: https://github.com/a8m/angular-filter

Problem is that I now group per date/time instead of per day. Any idea how?

解决方案

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

Screenshot: 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)

});

这篇关于ng-repeat groupBy 每天的日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆