【问题标题】:Grouping by date but not time in angular按日期分组,但不按角度分组
【发布时间】:2016-09-26 03:22:00
【问题描述】:

我正在构建我的第一个 Angular 应用程序。

我正在使用工厂服务从 API 返回一些 JSON 数据,其中包括足球比赛装置。

我想按日期分组,但不考虑时间。 JSON日期字符串的格式为“2016-06-10T19:00:00Z”

我已经成功地在我的 ng-repeat 上使用了 groupBy,这很好用 - 唯一的问题是 - 它还考虑了时间,因此不会对同一天在不同 KO 时间的 2 场比赛进行分组。

我的 JSON 对象是这样的,我将它分配给我的控制器中名为 fixtures.list 的数组:

"fixtures":[
{
    "_links":{
        "self":{
            "href":"http://api.football-data.org/v1/fixtures/149855"
        },
        "soccerseason":{
            "href":"http://api.football-data.org/v1/soccerseasons/424"
        },
        "homeTeam":{
            "href":"http://api.football-data.org/v1/teams/773"
        },
        "awayTeam":{
            "href":"http://api.football-data.org/v1/teams/811"
        }
    },
    "date":"2016-06-10T19:00:00Z",
    "status":"TIMED",
    "matchday":1,
    "homeTeamName":"France",
    "awayTeamName":"Romania",
    "result":{
        "goalsHomeTeam":null,
        "goalsAwayTeam":null
    }
},
...

我的角度模板是这样的:

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

我的控制器:

function FixturesController (EurosService) {
    var ctrl = this;
    ctrl.list = [];
    function getFixtures () {
        EurosService
            .retrieveFixtures()
            .then( function (response) {
                ctrl.list = response;
            });
    }
    getFixtures();
}

angular
    .module('app')
    .controller('FixturesController', FixturesController)

对此表示赞赏。最好的方法是什么?谢谢。

【问题讨论】:

  • 你能分享一下你如何在 ng-repeat 上使用groupBy 的代码吗?
  • 现在添加了 plnkr 的链接。在 stackoverflow 上有点菜鸟

标签: javascript angularjs json


【解决方案1】:

与此答案有关https://stackoverflow.com/a/30509960/1211299

您可以将映射函数添加到您的 groupby 过滤器:| map: toLocaleDate | groupBy: 'date' 而您的映射功能是这样的:

$scope.toLocaleDate = function(e) {
    e.date = e.date.toLocaleDateString();
    return e;
};

此函数将您的日期转换为仅日期字符串。时间部分将从您的原始对象中删除。

【讨论】:

  • 谢谢,这正是我所需要的。我尝试创建一个自定义过滤器,但这使它更容易!甚至不知道地图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2019-06-06
  • 2014-01-31
  • 2021-06-05
相关资源
最近更新 更多