Filter date in en format in Angularjs?

Asked

Viewed 138 times

3

I have a little problem to make a filter in Angularjs in a date field.

Below follows the html:

<td class="text-center" title="'Data'" filter="{ dataProposta: 'text'}" id="'data'">
    {{p.dataProposta | date: "dd/MM/yyyy"}}
</td>

The view is as follows:

inserir a descrição da imagem aqui

In the input date when typing the date in Brazilian format 24/03/2016, the system does not filter, but if I type 2016-03-24, I can see the filters made for this date.

  • 1

    And where is the code that makes the filter?

  • Opa friend. I ended up having to leave work and I saw your comment today. But what the friend below answered helped me here. Anyway thank you too.

1 answer

1


You have to make a custom filter, and test the text in the format that is on your screen, because, what is coming is in another format and so does not work.

Example:

var app = angular.module('app', []);
app.controller('ctrl', function($scope) {
  $scope.search = '';
  $scope.list = [{
      id: 1,
      data: '2016-01-01'
    },
    {
      id: 2,
      data: '2016-01-02'
    },
    {
      id: 3,
      data: '2016-01-03'
    },
    {
      id: 4,
      data: '2016-01-03'
    },
  ];
  $scope.listFilter = function(model) {
    if ($scope.search === '') return true;    
    var _data = model.data.split('-').reverse();
    _data = _data.join('/');
    return _data.indexOf($scope.search) !== -1;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-i18n/1.7.2/angular-locale_pt-br.js"></script>


<div ng-app="app" ng-controller="ctrl">
  <input type="text" ng-model="search" />
  <ul>
    <li ng-repeat="d in list | filter:listFilter">{{d.id}} - {{d.data | date: 'dd/MM/yyyy'}}</li>
  </ul>
</div>

  • 1

    Good morning. Thank you, you helped me a lot here.

Browser other questions tagged

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