Reputation: 469
I'm trying to create a filter that will filter items in an ng-repeat for their dates. For example, in a div title "today" only showing items where todays date is set in each items date property. I'm hoping to end up with something like this in the markup:
<li class="{{todo.category}}" ng-repeat="todo in todos | taskDate : today">
{{todo.title}}
</li>
Example todo structure: (dates are generated through the AngularUI bootstrap directives so not 100% on how they are formatted.)
$scope.todos = [
{name : 'Complete 5 tasks',
category: 'Life',
date: ''}
]
The method and filter I have for this at the moment are here:
Date.prototype.sameDay = function(d) {
return this.getFullYear() === d.getFullYear() && this.getDate() === d.getDate() && this.getMonth() === d.getMonth();
};
app.filter("taskDate", function() {
return function(input, date) {
var todaysDate = new Date();
var filtered = [];
angular.forEach(input, function(todo, index) {
if (todo.date instanceof Date) {
if (todo.date.sameDay(todaysDate)) {
filtered.push(todo);
}
} else if (todo.date == date) {
// filtered.push(todo);
}
});
return filtered;
};
});
This works for displaying items with todays date when I use the following filter
|taskDate, but I can't figure out how to extend it to show tomorrows etc at all.
Edit: managed to get a filter to work for today, but not other dates
Upvotes: 1
Views: 97
Reputation: 1336
I think you can use Angular's native filters to help:
<li class="{{todo.category}}" ng-repeat="todo in todos | filter:todoFilter('today')">
$scope.todoFilter= function(date) {
return function(todo) {
if(date==='today' && todo.date=== $scope.todaysDate)){
return true;
}
}
}
Upvotes: 1