首页 > 解决方案 > 单击按钮后,应在 2 个日期之间过滤数据-Angularjs

问题描述

 scope.routeToTxn = function(){
            route.reload();
            location.path('/tellers/' + routeParams.tellerId + "/cashiers/" + routeParams.cashierId  +"/txns/" +  scope.formData.currencyCode);

            return function( item, startdate,enddate ) {
                var filtered = [];
                var txnstartDate = Date.parse(txnstartDate);
                var txnendDate = Date.parse(txnendDate);
                angular.forEach(item, function(item) {
                    if(item.completed_date > txnstartDate && item.completed_date < txnendDate) {
                        filtered.push(item);
                    }
                });
                return filtered;
            };

        };
<td class="col-md-2">
    from date:
    <input id="startDate" sort type="text" datepicker-pop="dd MMMM yyyy" ng-model="txnstartDate" class="form-control" is-open="opened" min="minDate" max="restrictDate"/>
</td>

<td class="col-md-2">
    To date:
    <input id="endDate" sort type="text" datepicker-pop="dd MMMM yyyy" ng-model="txnendDate" class="form-control" is-open="opened" min="minDate" max="restrictDate"/>
</td>

<td>
    <a ng-click="routeToTxn()" class="btn btn-primary">{{'label.button.cashier.showtxn' | translate}} </a>
</td>

单击搜索按钮后,应通过 2 个日期过滤数据:

有两个插入框,需要 2 个日期开始日期和结束日期,点击搜索框后,应看到数据过滤数据

标签: htmlangularjs

解决方案


检查这个片段你可能想要格式化这些日期,所以当你比较它们时,它们实际上是比较 https://jsfiddle.net/Lt7aP/14764/

此答案中使用的格式化程序 https://stackoverflow.com/a/29774197/8101253

html

<div ng-app ng-controller="Ctrl">
    <input ng-model="date1" type="date"></input>
    <input ng-model="date2" type="date"></input>
    <input type="submit" ng-click="compare(date1, date2)"></input>
    {{array}}
</div>

javascript

function Ctrl($scope) {

      $scope.array = [new Date("2018-11-06").toISOString().split('T')[0],
        new Date("2018-11-07").toISOString().split('T')[0],
        new Date("2018-11-09").toISOString().split('T')[0]
      ];
      $scope.compare = function(date1, date2) {
        $scope.array
          .filter(x => {
            console.log(x, date1, date2)
            return x > date1 && x < date2
          })
          .forEach(x => console.log(x));

      };
    }

推荐阅读