angularJS:过滤JSON数据w.r.t日期范围

时间:2014-03-25 10:33:40

标签: angularjs angular-ui angularjs-filter

我是AngularJS的新手。这是我的HTML代码

<div ng-controller="DateRangeCtrl">

      <div class="container">
        <div class="form-horizontal">
          <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt1" is-open="opened1" max="maxFromDate" ng-change="setMinToDate()"/>
          <button class="btn btn-sm" ng-click="open1($event)"><span class="glyphicon glyphicon-calendar"></span></button>
        </div>
        <div class="form-horizontal">
            <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt2" is-open="opened2" min="minToDate" max="maxToDate" ng-change="filterDateAdded()"/>
            <button class="btn btn-sm" ng-click="open2($event)"><span class="glyphicon glyphicon-calendar"></span></button>
        </div>

        <p><strong>Selected From Date: </strong> {{dt1 | date:'mediumDate'}}</p>
        <p><strong>Selected To Date: </strong> {{dt2 | date:'mediumDate'}}</p>
      </div>
      <hr />
      <table class="table table-bordered">
        <thead>
          <tr>
            <th>ID</th>
            <th>Date Added</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="item in items | orderBy:mySortFunction ">
            <td>{{item.ID}}</td>
            <td>{{ parseDate(item.dateAdded) | date:'longDate'}}</td>
          </tr>
        </tbody>
      </table>

    </div>

以下是我的角度代码:

testApp.config(function (datepickerConfig, datepickerPopupConfig) {
          datepickerConfig.showWeeks = false;
          datepickerPopupConfig.showButtonBar = false;
    });  

    testApp.controller('DateRangeCtrl', function($scope) {

        $scope.items = [
          {ID: "1", dateAdded: "01-04-2013"},
          {ID: "2", dateAdded: "12-01-2013"},
          {ID: "3", dateAdded: "12-31-2013"},
          {ID: "4", dateAdded: "01-12-2014"},
          {ID: "5", dateAdded: "03-04-2014"}

        ];

        $scope.parseDate = function(input){
            var parts = input.split('-');
            var newParts = new Date(parts[2], parts[0]-1, parts[1]); // Note: months are 0-based
            return newParts;
        }


        $scope.open1 = function($event) {
        $event.preventDefault();
        $event.stopPropagation();

        $scope.opened1 = true;
       };

      $scope.maxFromDate = new Date();

      $scope.maxToDate = new Date();

      $scope.setMinToDate = function (){
        $scope.dt2 = null;
        $scope.minToDate = $scope.dt1;
      };

      $scope.open2 = function($event) {
      $event.preventDefault();
      $event.stopPropagation();

      $scope.opened2 = true;
      };
    });

如何根据所选日期过滤行?对于例如如果我选择&#34; 01/01/2014&#34;在&#34; From&#34; datePicker中,我应该能够看到&#34;添加日期&#34;列的值超过&#34; 2014年1月1日&#34;。输出将是ID为4和5的行。

当选择&#34; To&#34; datePicker时,它应该采用字面上的等效方式

请帮帮我。我被困了!

更新: HTML

<tr ng-repeat="item in items | filter:dateFilter ">

JS

$scope.dateFilter = function (item) {
          return (item.dateAdded < $scope.dt2);
      };

我做错了吗?它仍然无法工作......

2 个答案:

答案 0 :(得分:2)

通过一些尝试方法,我最终做对了。问题是&#34; item.dateAdded&#34;字符串必须转换为Date对象。解决方案是在js文件中添加以下代码:

$scope.filterDateAdded = function (){
if($scope.dt1 != null)
{
  $scope.dateFilter = function (item) {
    return ($scope.parseDate(item.dateAdded) >= $scope.dt1 && $scope.parseDate(item.dateAdded) <= $scope.dt2);
  };
}

};

干杯!

答案 1 :(得分:0)

像这样更改你的代码

    <input type="text" datepicker-popup="MM-dd-yyyy" ng-model="dt2" is-open="opened2" min="minToDate" max="maxToDate" ng-model="search" ng-change="filterDateAdded()"/>


     <tr ng-repeat="item in items | filter:dateFilter">



 $scope.filterDateAdded=function()
    {

                 $scope.dateFilter = function () {
                  var result=[];
                   for(var i in $scope.item)
                    {  if($scope.item[i].dateAdded >$scope.search)
                           {
                                   result.push($scope.item[i]);
                                 }

                           }
              return result;
          };
    }