why does datepicker popup not display? - angularjs

I am trying to create a bootstrap datapicker directive:
app.directive('datePicker', function ($compile, $timeout) {
return {
replace: true,
restrict:'E',
templateUrl: 'datepicker.html',
scope: {},
controller: function ($scope) {
console.log('datepciker');
// debugger;
$scope.today = function () {
$scope.dt = new Date();
};
$scope.today();
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
//$scope.disabled = function(date, mode) {
// return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
//};
$scope.toggleMin = function () {
$scope.minDate = $scope.minDate ? null : new Date();
};
$scope.toggleMin();
$scope.open = function ($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
//$scope.open = function ($event) {
// $scope.status.opened = true;
//};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
$scope.status = {
opened: false
};
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var afterTomorrow = new Date();
afterTomorrow.setDate(tomorrow.getDate() + 2);
$scope.events =
[
{
date: tomorrow,
status: 'full'
},
{
date: afterTomorrow,
status: 'partially'
}
];
$scope.getDayClass = function (date, 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 '';
};
}
};
In my page I have:
<date-picker ng-model="dateTo"></date-picker>
For some reason the popup does not come up, any suggestions on how to fix this?
plunkr:http://plnkr.co/edit/UiMiEk5GQSVId4YdAnCH?p=preview

Line 2 of datepicker.html ends in
is-open="status.opened"
If you change that to just is-open-"opened" you get a date picker.

Related

Datepicker Angular bootstrap

I'm using angular-ui-bootstrap datepicker, and I want to use the following input format 'ddMMyyyy' on my input. Actually the ng-change is called when type this format in the input, but I want the input changes to the format to 'dd/MM/yyyy' after ng-change, just like when you select a date inside the datepicker calendar. How can I do that?
Here a plnkr example code
https://plnkr.co/edit/Njetw7fVfZATxruuUI3m?p=preview
angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSanitize', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerPopupDemoCtrl', function ($scope) {
$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 = {
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.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', 'ddMMyyyy'];
$scope.popup1 = {
opened: false
};
$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 '';
}
});

why is there only 1 datepicker visible?

I have created a bootstrap datepicker:
app.directive('datePicker', function() {
return {
templateUrl: 'datepicker.html',
controller: function($scope){
console.log('it works');
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.showWeeks = true;
$scope.toggleWeeks = function () {
$scope.showWeeks = ! $scope.showWeeks;
};
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
};
$scope.toggleMin = function() {
$scope.minDate = ( $scope.minDate ) ? null : new Date();
};
$scope.toggleMin();
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
'year-format': "'yy'",
'starting-day': 1
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'shortDate'];
$scope.format = $scope.formats[0];
console.log('end of controller');
}
};
});
When I place 2 of these directives on my page I only see 1 of them? Why is that?
How can I get 2 independent ones so I select 1 date in the first picker and another in the second one?
plunkr ref:http://plnkr.co/edit/QoiLt339m9x6pAswY0Kc?p=preview
Becouse you miss restrict:'E' property inside directive body.
To make them independent add scope:{} property it makes scope isolated.
http://plnkr.co/edit/aXW0zFR4t9kj75HxZ3kd?p=preview

How to display datepicker one at a time?

I am using datepicker from "http://angular-ui.github.io/bootstrap/"
In my page i am using more than one datepicker, So now i am able to open both at a time but what i exactly need is one datepicker at a time can any one suggest me an answer.
Code
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl',
function ($scope) {
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 )
);
};
$scope.toggleMin = function() {
$scope.minDate = $scope.minDate ? null : new Date();
};
$scope.toggleMin();
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
});

Datepicker For Bootstrap : update bootstrap-datepicker

I want to custom the footer of my bootstrap-datepicker, i want to remove the 3 buttons and do this:
JSFIDDLE
angular.module('ui.bootstrap.demo', ['ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($scope) {
$scope.today = function() {
$scope.dt = new Date();
};
$scope.today();
$scope.clear = function () {
$scope.dt = null;
};
// Disable weekend selection
$scope.disabled = function(date, mode) {
return ( mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ) );
};
$scope.toggleMin = function() {
$scope.minDate = $scope.minDate ? null : new Date();
};
$scope.toggleMin();
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yyyy',
startingDay: 1,
numberOfMonths: 2,
autoclose: true
};
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
$scope.format = $scope.formats[0];
});
You can remove the current footer
show-button-bar='false'
And add a new div with ng-show if the calendar is opened
<div class="footer" ng-show="opened">Horaries:<button>1</button><button>2</button><button>3</button></div>
Here is an example:
http://plnkr.co/edit/8WvmM9AmS8x6iArJzZv8?p=preview
off course, You´ll need some CSS to make it look like you want

AngularJS Directive failing apply because $apply is already running

Error: [$rootScope:inprog] http://errors.angularjs.org/1.2.7/$rootScope/inprog?p0=%apply
Hi! I'm making a mini calendar standalone directive and I'm having trouble applying the changes of the months and the day. It tells me $apply is already in progress, I have google around and notice it's a problem when using it inside a compile definition object.
But preventing the $apply from running when it's already doing it didn't solve things, since it seems like its always running. Any suggestion or fixes for my issue would be appreciated :D
Controller + Directive
function calendarController($scope) {
$scope.config = new Date(2013, 4, 25);
}
angular.module("calendar", []).directive('miniCalendar', function($parse) {
return {
restrict: "E",
replace: true,
templateUrl: "../views/template.html",
transclude: true,
controller: function($scope) {
$scope.currentDate = new Date();
$scope.prev = function() {
$scope.mCalDate = new Date($scope.mCalDate.getFullYear(), $scope.mCalDate.getMonth() - 1, $scope.mCalDate.getDay());
$scope.updateDate($scope.mCalDate);
};
$scope.next = function() {
$scope.mCalDate = new Date($scope.mCalDate.getFullYear(), $scope.mCalDate.getMonth() + 1, $scope.mCalDate.getDay());
$scope.updateDate($scope.mCalDate);
};
$scope.selecting = false;
$scope.selectDate = function() {
$scope.selecting = !$scope.selecting;
};
$scope.selectDay = function(day) {
$scope.mCalDate = day.date;
$scope.updateDate($scope.mCalDate);
};
$scope.currentMonth = '';
$scope.currentYear = '';
$scope.days = [{
day: "Sun"
}, {
day: "Mon"
}, {
day: "Tue"
}, {
day: "Wed"
}, {
day: "Thu"
}, {
day: "Fri"
}, {
day: "Sat"
}];
$scope.weeks = [];
},
scope: {
mCalDate: '='
},
compile: function(element, attrs) {
var modelAccessor = $parse(attrs.mCalDate);
return function(scope, element, attrs, controller) {
scope.currentDate = scope.mCalDate.getDate() + '/' + Math.abs(scope.mCalDate.getMonth() + 1) + '/' + scope.mCalDate.getFullYear();
var firstDayOfMonth = new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), 1).toDateString().split(' ');
var totalDays = new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth() + 1, 0).getDate();
var firstDayIndex;
for (var i in scope.days) {
if (scope.days[i].day == firstDayOfMonth[0]) {
firstDayIndex = i;
}
}
var allDays = [];
var h = 0;
for (i = 0; i < totalDays + 6; i++) {
if (i >= firstDayIndex && (h + 1) <= totalDays) {
allDays.push({
dayName: scope.days[new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), h + 1).getDay()].day,
day: ++h,
date: new Date(scope.mCalDate.getFullYear(), scope.mCalDate.getMonth(), h + 1)
});
} else {
allDays.push({});
}
}
var calendar = []
var chunk = 7;
for (i = 0, allDays.length; i < allDays.length; i += chunk) {
calendar.push({
week: calendar.length,
days: allDays.slice(i, i + chunk)
});
}
scope.weeks = calendar;
scope.currentYear = scope.mCalDate.getFullYear();
scope.currentMonth = scope.mCalDate.toDateString().split(' ')[1];
scope.$watch('mCalDate', function(val) {
//debugger;
});
scope.updateDate = function(date) {
scope.mCalDate = date;
var phase = scope.$root.$$phase; //Safe applying
if (phase != '$apply' || phase != '$digest') {
scope.$apply(function() {
scope.mCalDate = date;
$parse(attrs.mCalDate).assign(scope.$parent, 'config');
});
}
};
};
}
};
});
HTML
<mini-calendar type="text" m-cal-date="config" />
Template
<div class="miniCalendar"><label ng-click="selectDate()"><input type="text" ng-model="currentDate" disabled></label><div ng-show="selecting"><div><a><span ng-click="prev()">Prev</span></a><a><span ng-click="next()">Next</span></a><div ><span ng-bind="currentMonth">January</span> <span ng-bind="currentYear"></span></div></div><table><thead><tr><th ng-repeat="day in days"><span ng-bind="day.day"></span></th></tr></thead><tbody><tr ng-repeat="week in weeks"><td ng-repeat="d in week.days"><a ng-bind="d.day" ng-click="selectDay(d)">1</a></td></tr></tbody></table></div></div>
Using $timeout instead of $apply can solve $digest issue.
scope.updateDate = function(date) {
$timeout(function() {
scope.mCalDate = date;
$parse(attrs.mCalDate).assign(scope.$parent, 'config');
});
};

Resources