AngularJS - 从一系列日期映射日期

时间:2014-10-22 09:35:26

标签: javascript jquery angularjs angularjs-scope angularjs-ng-repeat

我有一个表格,在标题中,生成12个单元格从当前日期开始,然后减去一个月,所以:

22 Oct 2014   |   22 Sep 2014   |   22 Aug 2014 etc etc

然后我有一个带有日期列表的JSON文件。我有一个ANgular服务,它检索日期然后,我正在尝试做什么,比较标题中的日期范围,然后计算我的JSON中的项目数,落在日期之间。

这是一个Plunker:http://plnkr.co/edit/8nCKlxRAdtosc0EN2swG?p=preview

HTML:

<table>
    <thead>
        <tr>
            <th></th>
            <th ng-repeat="months in getMonths track by $index">{{ months }}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>
            <td ng-repeat="months in getMonths track by $index">
                <span ng-repeat="item in filtered = (DateMapping(months))"></span>
                <p>{{filtered.length}}</p>
            </td>
        </tr>
    </tbody>
</table>

JS:

因此,输出看起来像:

22 Oct 2014   |   22 Sep 2014   |   22 Aug 2014 etc etc
    2         |       5         |       0       etc etc

更新:

 $scope.DateMapping = function(months) {

        var curDate = new Date(months);
        var prevDate = new Date(months);
        prevDate = new Date(new Date(prevDate.setMonth(prevDate.getMonth() - 1)).setDate(prevDate.getDate() - 1));

        //console.log("curDate:  " + curDate);
        //console.log("prevDate: " + prevDate);

        var jsonEntries = $scope.Dates;

        for (var i = 0; i < jsonEntries.length; i++) {
            var jsonDate = jsonEntries[i].Date.substring(6, jsonEntries[i].Date.length - 2);

                if (jsonDate >= curDate && jsonDate <= prevDate ) {
                    //PERFORM .push
                }
                else {

                }
        }
    };

0 个答案:

没有答案