How to push date from calendar into startAt() function in angularJS - angularjs

angular.module('...', ['...'])
.controller('main', main);
function main($scope, $firebaseArray) {
$scope.assign = function() {
$scope.assigned = $scope.deviceName;
};
$scope.today = function () {
// must put to set hours before isostring
$scope.dth = new Date();
$scope.test = moment($scope.dth).format('YYYY-MM-DD');
console.log($scope.test);
// this console.log is to check if the date format is correct
};
$scope.today();
$scope.inlineOptions = {
customClass: getDayClass,
minDate: new Date(),
showWeeks: true
};
$scope.open1 = function () {
$scope.popup1.opened = true;
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
$scope.altInputFormats = ['M!/d!/yyyy'];
$scope.popup1 = {
opened: false
};
$scope.dateOptionsM = {
formatYear: 'MM',
startingDay: 1,
minDate: new Date(2018, 0, 1, 0, 0, 0, 0),
minMode: 'month'
};
$scope.dateOptionsY = {
formatYear: 'yyyy',
startingDay: 1,
// minDate: start from which date
minDate: new Date(2018, 0, 1, 0, 0, 0, 0),
minMode: 'year'
};
function getDayClass(data) {
var date = data.date,
mode = data.mode;
if (mode === 'day') {
var dayToCheck = new Date(date).setHours(0, 0, 0, 0);
for (var i = 0; i < $scope.events.length; i++) {
var currentDay = new Date($scope.events[i].date).setHours(0, 0, 0, 0);
if (dayToCheck === currentDay) {
return $scope.events[i].status;
}
}
}
return '';
}
$scope.$watch('[assigned, test]', function(values) {
//UI for calendar function
var newtest = values[1];
var value = values[0];
var newRef = firebase.database().ref('editedData').child(value);
var hourRef = newRef.child('H');
$scope.halfhourlyData = $firebaseArray(hourRef.orderByChild('timestamp').limitToLast(48).startAt(newtest));
console.log(newtest);
// this console log only displays once
$scope.halfhourlyData.$loaded();
So what I want to do is to push the date that is set on the calendar into the startAt() function. However, upon testing, I realized that both logs only have been displayed once. Meaning that the value is not changing. However, on the HTML page the value does change and it uses $scope.dth. Is there a way for me to make the values change according to the calendar display? I'm using angular-bootstrap UI. Thanks!

Related

Show Color on Specific dates in datetime-picker AngularJs

I'm using bootstrap datetime-picker in angularjs
I need to show color on some specific dates i.e. on weekend and holiday dates
Here is my date picker option :-
$scope.dateOptions = {
dateFormat: 'yyyy-MM-dd',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1,
onChangeDate: countDays
};
I'm getting list of holidays from database and weekends I'm able to identify.
How to show color to specific date?
I tried using some custom css but its applying on all the dates not to specific.
Thanks!
Answering your question
How to show color to specific date?
Bootstrap datetime-picker for Angularjs Do provide an option to apply custom class.
Try this
$scope.dateOptions = {
dateFormat: 'yyyy-MM-dd',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1,
onChangeDate: countDays,
customClass: getDayClass // fucntion having logic to select particular dates
};
The function getDayClass can be implement like this
function getDayClass(data) {
var date = data.date,
mode = data.mode;
if (mode === 'day') {
var dayToCheck = new Date(date).setHours(0,0,0,0);
for (var i = 0; i < $scope.events.length; i++) {
var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);
if (dayToCheck === currentDay) {
return $scope.events[i].status;
}
}
// check for weekend 0 for sun and 6 for sat
if(date.getDay() == 0 || date.getDay() == 6)
{
return 'full'; //return class to be applied
}
}
return '';
}
Set the class for dates
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var afterTomorrow = new Date(tomorrow);
afterTomorrow.setDate(tomorrow.getDate() + 1);
$scope.events = [
{
date: tomorrow,
status: 'full'
},
{
date: afterTomorrow,
status: 'partially'
}
];
The date picker calls date picker options for every date at the time of initialization.
you can check the Plunker example.

Disable Dates Using Bootstrap datepicker

I am trying to disable some dates (eventually via an array I provide) using the datepicker control. However, it seems that nothing useful is passed in to the dateDisabled function. When I set a breakpoint in the developer tools, the first variable (which is set to data in the documentation examples) comes across as simply the number zero.
I have verified that the option itself works, as blankly returning a true disables all dates. What must I do to get valid inputs?
Note: I just discovered we are using angular-ui-bootstrap version 0.12.1.
JS
//options object for the datepicker control
$scope.dateOptions = {
dateDisabled: disableDates,
formatYear: "yyyy"
};
// Disable weekend selection
function disableDates(date, mode) {
//return mode === 'day' && (date.getDay() === 5 || date.getDay() === 6);
//return true;
return date === new Date(2016, 6, 18);
}
//Set the calendar open
$scope.openCalendar = function (e) {
e.preventDefault();
e.stopPropagation();
$scope.vm.is_cal_open = !$scope.vm.is_cal_open;
};
$scope.hasInvalidErrorDate = function (date) {
if (!date || date <= Date.parse("1/1/1900")) {
return true;
}
return false;
};
$scope.earliestErrorDate = Date.parse("1/1/1900");
HTML
<div class="col-sm-4">
<!-- Error Date -->
<div class="form-group" ng-class="{'has-error': hasInvalidErrorDate(vm.data.adjustment.error_date) && form.$dirty}">
<label for="error_date">Error Date</label>
<p class="input-group calendar-wrapper">
<!--input disabled the input so that it forces users to use the date picker and therfore ensure good input-->
<input type="text" datepicker-popup="MM/dd/yyyy"
title="Click the calendar button"
name="error_date"
datepicker-options="dateOptions"
class="form-control"
is-open="vm.is_cal_open"
width="5"
ng-disabled="true"
ng-model="vm.data.adjustment.error_date"
min-date="dateOptions.minDate" />
<span class="input-group-btn">
<button type="button" class="btn btn-default delegate-cal-btn" ng-click="openCalendar($event)"><i class="fa fa-calendar"></i></button>
</span>
</p>
<span class="text-danger small" ng-show="hasInvalidErrorDate(vm.data.adjustment.error_date) && form.$dirty">
Please select an error date
</span>
</div>
</div>
Working absolutely fine.
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.clear = function() {
$scope.dt = null;
};
$scope.inlineOptions = {
customClass : getDayClass,
minDate : new Date(),
showWeeks : true
};
$scope.dateOptions = {
formatYear : 'yy',
maxDate : new Date(2199, 12, 31),
minDate : new Date(),
startingDay : 1
};
$scope.toggleMin = function() {
$scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null
: new Date();
$scope.dateOptions.minDate = $scope.inlineOptions.minDate;
};
$scope.toggleMin();
$scope.open1 = function() {
$scope.popup1.opened = true;
};
/*
* $scope.open2 = function() { $scope.popup2.opened =
* true; };
*/
$scope.setDate = function(year, month, day) {
$scope.dt = new Date(year, month, day);
};
$scope.formats = [ 'dd-MMMM-yyyy', 'yyyy/MM/dd',
'dd.MM.yyyy', 'shortDate' ];
$scope.format = $scope.formats[0];
$scope.altInputFormats = [ 'M!/d!/yyyy' ];
$scope.popup1 = {
opened : false
};
$scope.dateOptions = {
dateDisabled: disabled,
formatYear: 'yy',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1
};
// Disable weekend selection
function disabled(data) {
var date = data.date,
mode = data.mode;
return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
}
/*
* $scope.popup2 = { opened : false };
*/
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var afterTomorrow = new Date();
afterTomorrow.setDate(tomorrow.getDate() + 1);
$scope.events = [ {
date : tomorrow,
status : 'full'
}, {
date : afterTomorrow,
status : 'partially'
} ];
function getDayClass(data) {
var date = data.date, mode = data.mode;
if (mode === 'day') {
var dayToCheck = new Date(date).setHours(0,
0, 0, 0);
for (var i = 0; i < $scope.events.length; i++) {
var currentDay = new Date(
$scope.events[i].date)
.setHours(0, 0, 0, 0);
if (dayToCheck === currentDay) {
return $scope.events[i].status;
}
}
}
return '';
}

Calendar with only month and year in pickadate js api

Am trying with pickadate datepicker calendar in my application, is that possible to display the calendar with only month and year?
$('#datePicker').pickadate({
selectMonths: true, // Creates a dropdown to control month
selectYears: 150, // Creates a dropdown of 15 years to control year
format: 'mmm-yyyy',
max: true,
onSet: function (arg) {
if ('select' in arg) { //prevent closing on selecting month/year
this.close();
}
}
});
var from_$input = $('.from_date').pickadate({
today: 'Ok',
format: 'mmmm-yyyy',
min: new Date(),
formatSubmit: 'yyyy-mm-dd',
hiddenPrefix: 'prefix__',
hiddenSuffix: '__suffix',
selectYears: true,
selectMonths: true,
}),
var nowTemp = new Date();
var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), 0, 0, 0, 0, 0);
var checkin = $('#dpd1').datepicker({
onRender: function(date) {
return date.valueOf() < now.valueOf() ? 'disabled' : '';
}
}).on('changeDate', function(ev) {
if (ev.date.valueOf() > checkout.date.valueOf()) {
var newDate = new Date(ev.date)
newDate.setDate(newDate.getDate() + 1);
checkout.setValue(newDate);
}
checkin.hide();
$('#dpd2')[0].focus();
}).data('datepicker');
var checkout = $('#dpd2').datepicker({
onRender: function(date) {
return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
}
}).on('changeDate', function(ev) {
checkout.hide();
}).data('datepicker');

colouring of ui bootstrap calendar(datepicker) giving problems after ajax call

html code:
<style>
.full button span {
background-color: limegreen;
border-radius: 32px;
color: black;
}
.partially button span {
background-color: orange;
border-radius: 32px;
color: black;
}
.appointment>button {
color: black;
background-color: red;
}
.holidays>button {
color: black;
background-color: green;
}
</style>
<div>
<pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
<div style="display:inline-block; min-height:290px;">
<uib-datepicker ng-model="dt" class="well well-sm" datepicker-options="inlineOptions" ng-change="dateSelected()" date-disabled="enableRequired(date, mode)" custom-class="dayClass(date, mode);"></uib-datepicker>
angularjs colour working code:
$scope.today = function() {
console.log($rootScope.dt);
var newdate = new Date($rootScope.dt);
newdate.getDate();
newdate.getMonth()+1;
newdate.getFullYear();
d=newdate.getFullYear()+"-"+(newdate.getMonth()+1)+"-"+newdate.getDate();
console.log("after cj\hnage"+d);
holidays = new Array();
var promise = servicePOST.send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
"date":d
}).then(function(result) {
$scope.dcrlocked = result.dcrlocked;
$scope.leaves = result.leaves;
console.log(result.holidays);
//$scope.holidays = result.holidays;
for(i=0;i<result.holidays.length;i++)
{
holidays.push(new Date(result.holidays[i].date.replace(/-/g,',')));
}
alert(holidays.length);
return result.holidays;
});
/*promise.then(function(result) {
alert("holiday length after service post"+holidays.length);
$scope.dayClass= function(date, mode) {
if (mode === 'day') {
var dateToCheck = new Date(date);
for(var i = 0; i < holidays.length ; i++) {
if(areDatesEqual(holidays[i], dateToCheck)){
return 'appointment';
}
}
}
return '';
};
}); */
$rootScope.dt = new Date();
};
$scope.today();
$scope.clear = function() {
$rootScope.dt = null;
};
$scope.inlineOptions = {
minDate: new Date(),
showWeeks: false
};
$scope.dateOptions = {
formatYear: 'yy',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1
};
// Disable weekend selection
function disabledasda(data) {
var date = data.date,
mode = data.mode;
return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
}
$scope.toggleMin = function() {
$scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
$scope.dateOptions.minDate = $scope.inlineOptions.minDate;
};
$scope.toggleMin();
$scope.open1 = function() {
$scope.popup1.opened = true;
};
$scope.setDate = function(year, month, day) {
$rootScope.dt = new Date(year, month, day);
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
$scope.altInputFormats = ['M!/d!/yyyy'];
$scope.popup1 = {
opened: false
};
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var afterTomorrow = new Date();
afterTomorrow.setDate(tomorrow.getDate() + 1);
$scope.events = [
{
date: tomorrow,
status: 'full'
},
{
date: afterTomorrow,
status: 'partially'
}
];
$scope.enableRequired = function(date, mode){
$scope.holidaysdateshardcoded = [
new Date(2016,04,03),
new Date(2016,04,08),
//new Date(2016,2,16),
new Date()
]
var isHoliday = true;
for(var i = 0; i < $scope.holidaysdateshardcoded.length ; i++) {
if(areDatesEqual($scope.holidaysdateshardcoded[i], date)){
isHoliday = false;
}
}
return ( mode === 'day' && isHoliday );
};
function areDatesEqual(date1, date2) {
return date1.setHours(0,0,0,0) === date2.setHours(0,0,0,0)
}
$scope.dayClass = function(date, mode) {
var appointments = [
new Date(2016,04,03),
new Date(2016,04,08),
new Date(),
]
if (mode === 'day') {
var dateToCheck = new Date(date);
for(var i = 0; i < appointments.length ; i++) {
if(areDatesEqual(appointments[i], dateToCheck)){
return 'appointment';
}
}
}
return '';
};
$scope.dateSelected = function(){
var appointments = [
new Date(2016,04,03),
new Date(2016,04,08),
new Date(),
];
var dateSelected = new Date($rootScope.dt);
for(var i = 0; i < appointments.length ; i++) {
if(areDatesEqual(appointments[i], dateSelected)){
performAction();
}
}
};
function performAction(){
location.href='#/data';
}
with the above angularJS code, the dates get coloured but next when i fetch an array from the database and use promise to wait till data come and than apply the colour using dayclass as in the following angularJS code, it does not work (no colour gets applied to the particular dates coming in holidays array). the data comes properly and i manipulate it to the format required and passs the dates from the holidays arry. but it does'nt work.
angularJS code that does not work:
$scope.today = function() {
console.log($rootScope.dt);
var newdate = new Date($rootScope.dt);
newdate.getDate();
newdate.getMonth()+1;
newdate.getFullYear();
d=newdate.getFullYear()+"-"+(newdate.getMonth()+1)+"-"+newdate.getDate();
console.log("after cj\hnage"+d);
holidays = new Array();
var promise = servicePOST.send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
"date":d
}).then(function(result) {
$scope.dcrlocked = result.dcrlocked;
$scope.leaves = result.leaves;
console.log(result.holidays);
//$scope.holidays = result.holidays;
for(i=0;i<result.holidays.length;i++)
{
holidays.push(new Date(result.holidays[i].date.replace(/-/g,',')));
}
alert(holidays.length);
return result.holidays;
});
promise.then(function(result) {
alert("holiday length after service post"+holidays.length);
$scope.dayClass = function(date, mode) {
if (mode === 'day') {
var dateToCheck = new Date(date);
for(var i = 0; i < holidays.length ; i++) {
if(areDatesEqual(holidays[i], dateToCheck)){
return 'appointment';
}
}
}
return '';
};
});
$rootScope.dt = new Date();
};
$scope.today();
$scope.clear = function() {
$rootScope.dt = null;
};
$scope.inlineOptions = {
minDate: new Date(),
showWeeks: false
};
$scope.dateOptions = {
formatYear: 'yy',
maxDate: new Date(2020, 5, 22),
minDate: new Date(),
startingDay: 1
};
// Disable weekend selection
function disabledasda(data) {
var date = data.date,
mode = data.mode;
return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
}
$scope.toggleMin = function() {
$scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
$scope.dateOptions.minDate = $scope.inlineOptions.minDate;
};
$scope.toggleMin();
$scope.open1 = function() {
$scope.popup1.opened = true;
};
$scope.setDate = function(year, month, day) {
$rootScope.dt = new Date(year, month, day);
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
$scope.altInputFormats = ['M!/d!/yyyy'];
$scope.popup1 = {
opened: false
};
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var afterTomorrow = new Date();
afterTomorrow.setDate(tomorrow.getDate() + 1);
$scope.events = [
{
date: tomorrow,
status: 'full'
},
{
date: afterTomorrow,
status: 'partially'
}
];
$scope.enableRequired = function(date, mode){
$scope.holidaysdateshardcoded = [
new Date(2016,04,03),
new Date(2016,04,08),
//new Date(2016,2,16),
new Date()
]
var isHoliday = true;
for(var i = 0; i < $scope.holidaysdateshardcoded.length ; i++) {
if(areDatesEqual($scope.holidaysdateshardcoded[i], date)){
isHoliday = false;
}
}
return ( mode === 'day' && isHoliday );
};
function areDatesEqual(date1, date2) {
return date1.setHours(0,0,0,0) === date2.setHours(0,0,0,0)
}
$scope.dateSelected = function(){
var appointments = [
new Date(2016,04,03),
new Date(2016,04,08),
new Date(),
];
var dateSelected = new Date($rootScope.dt);
for(var i = 0; i < appointments.length ; i++) {
if(areDatesEqual(appointments[i], dateSelected)){
performAction();
}
}
};
function performAction(){
location.href='#/data';
}
any help would be appreciated! please ask if confused!

Cannot read property 'length' of undefined

I have a problem with my angular JS function
when I first call this function, everything is good. But my next attempts cause an error and stops it from working.
My js code:
$scope.updateUptimeCalendar = function() {
$scope.loadHeatMap();
$scope.getMonthHeatMap(new Date().getFullYear(), new Date().getMonth());
};
$scope.loadHeatMapData = function (date) {
var time = date.getTime();
var isMonthLoaded = false;
var eps = 1000 * 60 * 60 * 6; // epsilont +-6 hours
angular.forEach($scope.heatMatData, function (record) {
if (!isMonthLoaded) {
if (Math.abs(record.date - time) < eps) {
isMonthLoaded = true;
}
}
});
if (!isMonthLoaded) {
$scope.getMonthHeatMap(date.getFullYear(), date.getMonth());
};
};
$scope.getMonthHeatMap = function(fullYear, month) {
$scope.setLoadingState(true);
var monthStart = new Date(Date.UTC(fullYear, month, 1));
var monthEnd;
if (month == 12) {
monthEnd = new Date(Date.UTC(fullYear + 1, 1, 1));
} else {
monthEnd = new Date(Date.UTC(fullYear, month + 1, 1));
};
monthStart = monthStart.getTime();
monthEnd = monthEnd.getTime();
var turnOnDuration;
$http.post('Statistic/GetClientStateStatisticByDays', {
clientId: $scope.$parent.clientForStatistic.ClientId,
stateId: 1,
beginPeriod: monthStart,
endPeriod: monthEnd
}).success(function(data) {
$scope.$parent.processResponse(data);
angular.forEach(data, function(record) {
turnOnDuration = record.Duration / (1000 * 60 * 60);
$scope.heatMatData.push({
date: record.AvarageTime,
value: Number((turnOnDuration).toFixed(0)),
});
});
$scope.heatMatData.sort(function(a, b) {
return a.date - b.date;
});
//todo: fixed this when try to get null data values DK
$scope.setLoadingState(false);
$scope.cal.update($scope.heatMatData);
});
};
$scope.heatMapDateParser = function (data) {
var stats = {};
for (var d in data) {
stats[data[d].date / 1000] = data[d].value;
}
return stats;
};
$scope.loadHeatMap = function () {
$('#statistic-modal-chart-container').empty();
$('#statistic-modal-chart-container-calendar').empty();
$scope.cal = new CalHeatMap();
$scope.cal.init({
data: $scope.heatMatData,
afterLoadData: $scope.heatMapDateParser,
considerMissingDataAsZero: true,
//todo: use to set visible dates interval. MR
//minDate: new Date(2014, 9),
//maxDate: new Date(),
itemSelector: "#statistic-modal-chart-container-calendar",
domain: "month",
domainLabelFormat: "%b %Y",
subDomain: "x_day",
subDomainTextFormat: "%d",
itemName: "Hour",
cellSize: 35,
range: 1,
displayLegend: true,
legend: [0, 4, 8, 12, 16, 20, 25],
legendColors: {
base: '#ededed',
empty: '#ededed',
min: '#FFFFFF',
max: '#3366FF'
},
legendHorizontalPosition: "center",
previousSelector: "#uptime-calendar-prev",
nextSelector: "#uptime-calendar-next",
afterLoadNextDomain: function (date) {
$scope.loadHeatMapData(date);
},
afterLoadPreviousDomain: function (date) {
$scope.loadHeatMapData(date);
},
itemNamespace: "animationDuration-a",
tooltip: true
});
};
If I use debug, the error occurs in this row $scope.cal.update($scope.heatMatData);
I get this error
my error:
TypeError: Cannot read property 'length' of undefined
at e (http:/Scripts/d3.min.js:3:10447)
at Array.pa.data (http://Scripts/d3.min.js:3:11196)
at Object.CalHeatMap.fill (http://Scripts/cal-heatmap.min.js:8:20667)
at http://Scripts/cal-heatmap.min.js:9:4615
at h (http:/Scripts/cal-heatmap.min.js:9:1605)
at Object.CalHeatMap.getDatas (http:/Scripts/cal-heatmap.min.js:9:1940)
at Object.CalHeatMap.update (http://Scripts/cal-heatmap.min.js:9:4537)
at http://Scripts/Controllers/statistic-ctrl.js:355:24
at http://Scripts/angular.min.js:72:45
at L (http:/Scripts/angular.min.js:99:469) angular.min.js:92
(anonymous function) angular.min.js:92
(anonymous function) angular.min.js:68
L angular.min.js:99
L angular.min.js:99
(anonymous function) angular.min.js:101
k.$eval angular.min.js:111
k.$digest angular.min.js:108
k.$apply angular.min.js:112
h angular.min.js:72
w angular.min.js:77
A.onreadystatechange
please anybody help me!
Put
$scope.heatMatData = [];
as first instruction

Resources