0

I was wondering if anyone could help me with filter on dates within ng-repeat I have a text field that I enter the search text into for filter the results in my table

take this cut down example`

 <tr id="credentialsData" ng-repeat="credential in credentials.data | filter:credentialsSearchText">
      <td>{{credential.createdDate | date:'medium'}}</td>
 </tr>`

credential.createdDate comes back in the rest call in the format 2015-03-24T21:19:49Z

When I attach the medium date filter - it displays as Mar 24, 2015 9:19:49 PM

However when i search on the String Mar or 9:, I get no results. Angularjs searches on the base object and ignores the filter. I have read other options online where the person recommends adding different date formats into the json object but unfortunately that is not an option for me

Any help on this would be appreciated

Cheers Damien

1 Answer 1

1

You can use a custom function for the filter.

https://docs.angularjs.org/api/ng/filter/filter

 <tr id="credentialsData" ng-repeat="credential in credentials.data | filter:credentialsSearchText:compareCredentialDate">
      <td>{{credential.createdDate | date:'medium'}}</td>
 </tr>

In your controller, put a

$scope.compareCredentialDate = function(credential, expected) {
  // you have to inject '$filter' to use this:
  var dateFilter = $filter('date');

  // this is the value that "credential.createdDate | date:'medium'"
  // evaluates to:
  var formattedDateString = dateFilter(credential.createdDate, 'medium');

  // hypothetical matching method - you can implement whatever you
  // want here:    
  var isMatch = formattedDateString.indexOf(expected) >= 0;

  return isMatch;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Smithecus - ill try that now and let you know how i get on
Could this function be written as a filter so it can be used accross the board?
You can add that function to your $rootScope in a module.run() callback so that compareCredentialDate is available everywhere. A filter for this would be more trouble than its worth - trust me. :)
thanks for this i tried it but it didnt work. When i have more than 1 field in the table, they come in as individual values. Not as 1 object for each row. Running credential.createdDate does not work. Any other suggestions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.