猿问

ng-repeat按日期范围过滤数据

我正在尝试通过键入日期范围来过滤包含时间戳的列表

例如:

的jsfiddle

HTML

<div ng-app="tst">
    <div ng-controller="MyController">
        <table>
            <tr>
                <td>From:                    <input ng-model="query.date1" type="text" placeholder="" />
                </td>
                <td>To:                    <input ng-model="query.date2" type="text" placeholder="" />
                </td>
            </tr>

            <tr ng-repeat="order in orders |filter:query">
                <td>{{order.date1 * 1000 | date:'dd-MM-yyyy'}}</td>
                <td>{{order.date2 * 1000 | date:'dd-MM-yyyy'}}</td>
            </tr>
        </table>
    </div></div>

JavaScript的

var nameSpace = angular.module('tst',[]);nameSpace.controller('MyController', function MyController($scope) {
  $scope.orders = [
  {
    "date1":"1306487800",
    "date2":"1406587800"
  },
  {
    "date1":"1196487800",
    "date2":"1406597800"
  }]});

我希望能够用值填充“发件人”字段:27-05-2010

并且“ To”字段的值为:29-07-2015

并仅获取该范围内的记录。

(示例中的第一条记录)。

谢谢分配阿维



慕无忌1623718
浏览 655回答 3
3回答

开心每一天1111

对于时间操纵,我强烈建议使用诸如momentJS之类的库;使用momentJS和现代浏览器,可以以非常声明的方式实现:.filter('dateRange',&nbsp;function()&nbsp;{ &nbsp;&nbsp;return&nbsp;function(items,&nbsp;startDate,&nbsp;endDate)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//an&nbsp;undefined&nbsp;startDate&nbsp;is&nbsp;converted&nbsp;to&nbsp;the&nbsp;beginning&nbsp;of&nbsp;time &nbsp;&nbsp;&nbsp;&nbsp;startDate&nbsp;=&nbsp;startDate&nbsp;||&nbsp;0; &nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;granularity&nbsp;=&nbsp;null&nbsp;//&nbsp;can&nbsp;be&nbsp;'days',&nbsp;...&nbsp;see&nbsp;momentJS&nbsp;doc &nbsp;&nbsp;&nbsp;&nbsp;//you&nbsp;need&nbsp;support&nbsp;for&nbsp;array.prototype.filter&nbsp;and&nbsp;arrow&nbsp;functions;&nbsp;i.e.&nbsp;IE&nbsp;sucks/needs&nbsp;a&nbsp;polyfill&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;items.filter(item&nbsp;=>&nbsp;moment(item).isBetween(startDate,&nbsp;endDate,&nbsp;granularity,&nbsp;'[]')); &nbsp;&nbsp;}}
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答