How to filter dates through Angularjs?

Asked

Viewed 161 times

2

I have this function that takes how many accounts to pay there are and counts how many of them are within the range that I set. But you’re not getting the information right.

$scope.filterDate = function(option)
{
    var tam = $scope.billToPayData.length;

    if (tam > 0) {
        if ($scope.cp.inicio != null && $scope.cp.fim != null) {
            for (var i = 0; i < tam; i++) {
                $scope.cpFilter[i] = false;

                if ($scope.billToPayData[i].vencimento >= $filter('date')($scope.cp.inicio, 'yyyy-MM-dd') &&
                    $scope.billToPayData[i].vencimento <= $filter('date')($scope.cp.fim, 'yyyy-MM-dd')) {
                    $scope.cpFilter[i] = true;
                }
            }
        } else {
            swal('Selecione as duas datas', 'Digite o período das contas', 'warning');
        }
    }
};
  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

You can create a custom filter for your dates as follows:

(function() {
  angular
    .module('meuApp', []);

  angular
    .module('meuApp')
    .filter('entre', entre);

  function entre() {
    return function(itens, inicio, fim) {
      var filtrados = [];

      angular.forEach(itens, function(item) {
        if (item.vencimento >= inicio && item.vencimento <= fim) {
          filtrados.push(item);
        }
      });

      return filtrados;
    }
  }

  angular
    .module('meuApp')
    .controller('AppController', AppController);

  AppController.$inject = [];

  function AppController() {
    var vm = this;

    _inicializar();

    function _inicializar() {
      vm.itens = [];

      vm.de = new Date('2018-05-05T00:00:00');
      vm.ate = new Date('2018-05-07T00:00:00')

      vm.itens.push({vencimento: new Date('2018-05-01T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-02T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-03T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-04T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-05T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-06T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-07T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-08T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-09T00:00:00')});
      vm.itens.push({vencimento: new Date('2018-05-10T00:00:00')});
    }
  };
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="meuApp">
  <div ng-controller="AppController as vm">
    <div>
      TODAS
      <p ng-repeat="item in vm.itens">
        {{ item.vencimento | date : 'dd/MM/yyyy'}}
      </p>
    </div>
    <div>
      FILTRADAS
      <p ng-repeat="item in vm.itens | entre : vm.de : vm.ate">
        {{ item.vencimento | date : 'dd/MM/yyyy'}}
      </p>
    </div>
  </div>
</div>

Note that I created the filter entre who checks the property vencimento is between the two dates passed as parameter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.