How to change view date dynamically (which is filter event by date time) - angularjs

I Want to filter event by date but date is pass by normal input type="text" not kendo default datepicker.And display passing date in kendo schduler header But cant not change view date.This is my code.........
$scope.searchEventByDate = function (item) {
var scheduler = $("#scheduler").data("kendoScheduler");
scheduler.view().startDate(item.StartDate);
scheduler.view().endDate(item.EndDate);
scheduler.view(("day"));
$scope.scheduler.dataSource.read();
};
This is my filter param
parameterMap: function (options, operation) {
var popupheight = $(window).height() - 180 + 'px';
$scope.popupWraperForTryout = popupheight;
var scheduler = $("#scheduler").data("kendoScheduler");
if (searchCount != 0) {
if (operation === "read") {
return {
filterByPersonalEvent: $scope._filterParamObj.filterBypersonal,
filterBySignUpRequired: $scope._filterParamObj.filterBySingupRequired,
filterByPaidOrFree: $scope._filterParamObj.filterByPaid,
filterByEventStatus: $scope._filterParamObj.eventStatusId,
filterByEventType: $scope._filterParamObj.eventTypeId,
selectedTeam: $scope._filterParamObj.seasonTeamId,
filterByStartDate: scheduler.view().startDate(),
filterByEndDate: scheduler.view().endDate(),
OrgId: _orgId,
UserTimezone: global.userTimezoneOffset
}
}
}
},
I am so tired.This code is not change in view date.Please help me

Several issues here - the day view shows just one day; you can't set startDate and endDate - just date.
$scope.searchEventByDate = function (item) {
var scheduler = $("#scheduler").data("kendoScheduler");
//scheduler.view().startDate(item.StartDate);
//scheduler.view().endDate(item.EndDate);
scheduler.view("day");
// item.StartDate should be Date object - like scheduler.date(new Date("2013/6/6"));
scheduler.date(item.StartDate);
$scope.scheduler.dataSource.read();
};
If you need to set some explicit date range to filter - you can do it, but still you can't show more than just one day in day view.
$scope.searchEventByDate = function (item) {
var scheduler = $("#scheduler").data("kendoScheduler");
scheduler._myFilterStartDate = item.StartDate;
scheduler._myFilterEndDate = item.EndDate;
scheduler.view("day");
scheduler.date(item.StartDate);
$scope.scheduler.dataSource.read();
};
And in parameter map:
...
return {
filterByStartDate: scheduler.view().startDate(),
filterByEndDate: scheduler.view().endDate(),
myFilterStartDate: scheduler._myFilterStartDate,
myFilterEndDate: scheduler._myFilterEndDate,
OrgId: _orgId,
UserTimezone: global.userTimezoneOffset
};
...

Related

Modifying a plugin, but changes aren't reflected. AngularJS in an Ionic (Angular2) app

I am trying to modify a plugin, it is a calendar. When you press on the dates that are not part of the month (at the beginning and the end of the month) it changes to the next or previous month. I am trying to disable this, and I found what part of the code to change, but when I make the changes (or add a console.log message), nothing gets reflected when I execute ionic serve. One thing is, the calendar is written in AngularJS - while I am using Ionic, which uses Angular 2. The part of the code I am trying to change looks like this:
$scope.select = function (viewDate) {
console.log("in in in select &&&&&&&");
var selectedDate = viewDate.date,
events = viewDate.events,
views = scope.views,
dates,
r;
if (views) {
dates = views[scope.currentViewIndex].dates;
var currentCalendarDate = ctrl.currentCalendarDate;
var currentMonth = currentCalendarDate.getMonth();
var currentYear = currentCalendarDate.getFullYear();
var selectedMonth = selectedDate.getMonth();
var selectedYear = selectedDate.getFullYear();
var direction = 0;
if (currentYear === selectedYear) {
if (currentMonth !== selectedMonth) {
direction = currentMonth < selectedMonth ? 1 : -1;
}
} else {
direction = currentYear < selectedYear ? 1 : -1;
}
ctrl.currentCalendarDate = selectedDate;
if (direction === 0) {
if (ngModelCtrl) {
ngModelCtrl.$setViewValue(selectedDate);
}
var currentViewStartDate = ctrl.range.startTime,
oneDay = 86400000,
selectedDayDifference = Math.floor((selectedDate.getTime() - currentViewStartDate.getTime()) / oneDay);
for (r = 0; r < 42; r += 1) {
dates[r].selected = false;
}
if (selectedDayDifference >= 0 && selectedDayDifference < 42) {
dates[selectedDayDifference].selected = true;
scope.selectedDate = dates[selectedDayDifference];
}
} else {
console.log("is getting here &&^^%%$$");
//ctrl.moveOnSelected = true;
//ctrl.slideView(direction); <----- I AM COMMENTING THIS OUT TO STOP THE SLIDE!!!
}
...
At the bottom of the above code block, I made an arrow pointing to the two lines I am commenting out. I also added console.log statements, one at the beginning of the function, and one in the middle, and neither of them are output into the console (the above is the select function which happens when a date on the calendar is selected).
The code structure of the plugin is like this:
angular.module("ui.rCalendar.tpls", ["templates/rcalendar/calendar.html","templates/rcalendar/day.html","templates/rcalendar/displayEvent.html","templates/rcalendar/month.html","templates/rcalendar/monthviewDisplayEvent.html","templates/rcalendar/monthviewEventDetail.html","templates/rcalendar/week.html"]);
angular.module('ui.rCalendar', ['ui.rCalendar.tpls'])
.constant('calendarConfig', {
formatDay: 'dd',
formatDayHeader: 'EEE',
formatDayTitle: 'MMMM dd, yyyy',
formatWeekTitle: 'MMMM yyyy, Week w',
formatMonthTitle: 'MMMM yyyy',
formatWeekViewDayHeader: 'EEE d',
formatHourColumn: 'ha',
calendarMode: 'month',
showEventDetail: true,
startingDayMonth: 0,
startingDayWeek: 0,
allDayLabel: 'all day',
noEventsLabel: 'No Events',
eventSource: null,
queryMode: 'local',
step: 60,
autoSelect: true,
monthviewDisplayEventTemplateUrl: 'templates/rcalendar/monthviewDisplayEvent.html',
monthviewEventDetailTemplateUrl: 'templates/rcalendar/monthviewEventDetail.html',
weekviewAllDayEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
weekviewNormalEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
dayviewAllDayEventTemplateUrl: 'templates/rcalendar/displayEvent.html',
dayviewNormalEventTemplateUrl: 'templates/rcalendar/displayEvent.html'
})
.controller('ui.rCalendar.CalendarController'
...
.directive('monthview', ['dateFilter', function (dateFilter) {
THIS IS WHERE THE SELECT FUNCTION IS
...
This is an angularjs plugin in an Ionic app, remember it is built on Angular 2 not angularjs. Im not sure where the plugin javascript file gets loaded.
I was looking in the wrong package folder, I didn't realized I had installed a typescript version.

Angular Scope watch change on filter change (v1.4.3)

I got a wired error
I have directive date picker
I have watch listener while the date change
$scope.$watch('model', function (newDate) {
if (newDate) {
if ($scope.hideDay) {
$scope.dateFields.day = 1;
} else {
$scope.dateFields.day = new Date(newDate).getUTCDate();
}
$scope.dateFields.month = new Date(newDate).getUTCMonth() +1;
$scope.dateFields.year = new Date(newDate).getUTCFullYear();
} else {
if ($scope.hideDay) {
$scope.dateFields.day = 1;
} else {
$scope.dateFields.day = null;
}
$scope.dateFields.month = null;
$scope.dateFields.year = null;
}
});
My weird problem is that I have a search box
<input class="free-txt" ng-model="search.name" placeholder="Free search" />
When I typed in the search box The watch model changed with no reason.
What can be the reason for this bug and how can I fixed it?
Here is the full demo
https://embed.plnkr.co/yGqE33kZNwGz7NaNDOD5/
Select day
Select Month
Open the Year modal than Write in the free text "78"
Select The year
The bug when Typing in the free text the day and month change to null
by default angular $watch watch object by reference
https://docs.angularjs.org/api/ng/type/$rootScope.Scope
objectEquality:
Compare for object equality using angular.equals instead of comparing for reference equality.
(default: false)
try to add a parametre to true for watching object or use watchcollection and give it the variables you need
$scope.$watch('model', function (newDate) {
...
},true );
the directive update the model only when the date is valid :
$scope.checkDate = function () {
$timeout(function () {
var date = rsmDateUtils.checkDate($scope.dateFields);
if (date) {
// the watch is called what date is valid with a year
if ($scope.hideDay) {
$scope.model = $filter('date')(date, 'yyyy-MM');
} else {
$scope.model = $filter('date')(date, 'yyyy-MM-dd');
}
}
});
};

angular push result to controller

(was not sure what to have as a title, so if you have a better suggestion, feel free to come up with one - I will correct)
I am working on an angular application where I have some menues and a search result list. I also have a document view area.
You can sort of say that the application behaves like an e-mail application.
I have a few controllers:
DateCtrl: creates a list of dates so the users can choose which dates they want to see posts from.
SourceCtrl: Creates a list of sources so the user can choose from which sources he/she wants to see posts from.
ListCtrl: The controller populating the list. The data comes from an elastic search index. The list is updated every 10-30 seconds (trying to find the best interval) by using the $interval service.
What I have tried
Sources: I have tried to make this a filter, but a user clicks two checkboxes the list is not sorted by date, but on which checkbox the user clicked first.
If it is possible to make this work as a filter, I'd rather continue doing that.
The current code is like this, it does not do what I want:
.filter("bureauFilter", function(filterService) {
return function(input) {
var selectedFilter = filterService.getFilters();
if (selectedFilter.length === 0) {
return input;
}
var out = [];
if (selectedFilter) {
for (var f = 0; f < selectedFilter.length; f++) {
for (var i = 0; i < input.length; i++) {
var myDate = input[i]._source.versioncreated;
var changedDate = dateFromString(myDate);
input[i]._source.sort = new Date(changedDate).getTime();
if (input[i]._source.copyrightholder === selectedFilter[f]) {
out.push(input[i]);
}
}
}
// return out;
// we need to sort the out array
var returnArray = out.sort(function(a,b) {
return new Date(b.versioncreated).getTime() - new Date(a.versioncreated).getTime();
});
return returnArray;
} else {
return input;
}
}
})
Date: I have found it in production that this cannot be used as a filter. The list of posts shows the latest 1000 posts, which is only a third of all posts arriving each day. So this has to be changed to a date-search.
I am trying something like this:
.service('elasticService', ['es', 'searchService', function (es, searchService) {
var esSearch = function (searchService) {
if (searchService.field === "versioncreated") {
// doing some code
} else {
// doing some other type of search
}
and a search service:
.service('searchService', function () {
var selectedField = "";
var selectedValue = "";
var setFieldAndValue = function (field, value) {
selectedField = field;
selectedValue = value;
};
var getFieldAndValue = function () {
return {
"field": selectedField,
"value": selectedValue
}
};
return {
setFieldAndValue: setFieldAndValue,
getFieldAndValue: getFieldAndValue
};
})
What I want to achieve is this:
When no dates or sources are clicked the whole list shall be shown.
When Source or Date are clicked it shall get the posts based on these selections.
I cannot use filter on Date as the application receives some 3000 posts a day and so I have to query elastic search to get the posts for the selected date.
Up until now I have put the elastic-search in the listController, but I am now refactoring so the es-search happens in a service. This so the listController will receive the correct post based on the selections the user has done.
Question is: What is the best pattern or method to use when trying to achieve this?
Where your data is coming from is pretty irrelevant, it's for you to do the hook up with your data source.
With regards to how to render a list:
The view would be:
<div ng-controller='MyController as myCtrl'>
<form>
<input name='searchText' ng-model='myCtrl.searchText'>
</form>
<ul>
<li ng-repeat='item in myCtrl.list | filter:myCtrl.searchText' ng-bind='item'></li>
</ul>
<button ng-click='myCtrl.doSomethingOnClick()'>
</div>
controller would be:
myApp.controller('MyController', ['ElasticSearchService',function(ElasticSearchService) {
var self = this;
self.searchText = '';
ElasticSearchService.getInitialList().then(function(list) {
self.list = list;
});
self.doSomethingOnClick = function() {
ElasticSearchService.updateList(self.searchText).then(function(list) {
self.list = list;
});
}
}]);
service would be:
myApp.service('ElasticSearchService', ['$q', function($q) {
var obj = {};
obj.getInitialList = function() {
var defer = $q.defer();
// do some elastic search stuff here
// on success
defer.resolve(esdata);
// on failure
defer.reject();
return defer.promise();
};
obj.updateList = function(param) {
var defer = $q.defer();
// do some elastic search stuff here
// on success
defer.resolve(esdata);
// on failure
defer.reject();
return defer.promise();
};
return obj;
}]);
This code has NOT been tested but gives you an outline of how you should approach this. $q is used because promises allow things to be dealt with asynchronously.

Paginate By Week - AngularJS

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
var currentDate,
weekStart,
weekEnd,
shortWeekFormat = 'MMMM Do';
function setCurrentDate(aMoment){
currentDate = aMoment,
weekStart = currentDate.clone().startOf('week'),
weekEnd = currentDate.clone().endOf('week')
}
// initialize with current date
setCurrentDate(moment());
// use these methods for displaying
$scope.currentWeek = function(){ return currentDate.format(shortWeekFormat); };
$scope.currentWeekStart = function(){ return weekStart.format(shortWeekFormat); };
$scope.currentWeekEnd = function(){ return weekEnd.format(shortWeekFormat); };
Then create two methods for going to next/previous week
$scope.nextWeek = function(){
setCurrentDate(currentDate.add(7,'days'));
};
$scope.prevWeek = function(){
setCurrentDate(currentDate.subtract(7,'days'));
};
(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)
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
if ((eventTime.isSame(weekStart) || eventTime.isAfter(weekStart))&&
(eventTime.isSame(weekEnd) || eventTime.isBefore(weekEnd))) return true;
return false;
};
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
return (eventTime >= weekStart && eventTime <= weekEnd);
};
(you also, most likely, want the ng-repeat on the li elements and not the ul)
Demo at http://codepen.io/gpetrioli/pen/QwLRQB
The weekStart and weekEnd variables don't exist outside of the scope of week(item). If you're using ngClick to call a function that tries to modify those variables, it'll just return undefined. I don't know how your layout is but I would pull those two variables outside of the week function and make them $scope variables.
Additionally, I would have ngClick call a function that would change the two $scope variables (either adds 7 or subtracts 7 depending on which direction you want to go in).
$scope.weekStart = moment().startOf('week');
$scope.weekEnd = moment().endOf('week');
$scope.week = function(item) {
var eventTime = moment(item.jsdatetime);
if (eventTime >= $scope.weekStart && eventTime <= $scope.weekEnd) return true;
return false;
};
$scope.update= function(direction) {
$scope.weekStart.add(direction, 'days');
$scope.weekEnd.add(direction, 'days');
}
And create two buttons in your view:
Previous week
Next week

Angular Filter by Date/Time Range

I'm trying to build an event date filter by passing in a time range. I was able to filter for events that are the same date as today but need help to filter events from last week, last month, etc...
$scope.eventDateFilter = function(column) {
if(column === 'today') {
$scope.dateRange = $scope.dateToday;
} else if (column === 'pastWeek') {
//need logic
} else if (column === 'pastMonth') {
//need logic
} else if (column === 'future') {
//need logic
} else {
$scope.dateRange = "";
}
}
Here's my fiddle:
http://jsfiddle.net/c6BfQ/3/
Your help is greatly appreciated.
I would use a custom filter. Here is one I used to filter things created in the last two days, it should give you an idea of how to do yours.
.filter('dateFilter', function() {
return function (objects) {
var filtered_list = [];
for (var i = 0; i < objects.length; i++) {
var two_days_ago = new Date().getTime() - 2*24*60*60*1000;
var last_modified = new Date(objects[i].date_created).getTime();
if (two_days_ago <= last_modified) {
filtered_list.push(objects[i]);
}
}
return filtered_list;
}
});

Resources