I am trying to paginate a list of events (using ng-repeat) by week in AngularJS. I have a custom filter working that only displays the events within the current week, but I am trying to add functionality to look at future and past weeks.
Here is the filter I am using for the event lists -
$scope.week = function(item) {
var weekStart = moment().startOf('week');
var weekEnd = moment().endOf('week');
var eventTime = moment(item.jsdatetime);
if (eventTime >= weekStart && eventTime <= weekEnd) return true;
return false;
};
I have tried using ng-click to call a function that uses moment.js to .add(7, 'days'); to the weekStart and weekEnd variables but can't seem to get it to work.
Any help would be appreciated.
Here's a CodePen with the basic functionality going on - http://codepen.io/drewbietron/pen/xbKNdK
The
moment()
always return the current date/time.You need to store a reference to it to a variable, and then use that for manipulations.
(and since you have other variables depending on it, i would create a function that sets all those variables at once)
So in the controller i changed the top part to
Then create two methods for going to next/previous week
(moment.js implements
valueOf
so you do direct comparisons)And finally change your week filter to actually compare the dates (using.isSame()
,.isBefore()
and.isAfter()
) instead of the moment objects (which was wrong as you cannot do direct comparisons on custom objects)(you also, most likely, want the
ng-repeat
on theli
elements and not theul
)Demo at http://codepen.io/gpetrioli/pen/QwLRQB