I'm using Eonasdan's DateTimePicker in a directive, specifically example 9 on that page, to get a date range for a search. The date picker is easily set up, but I can't get a reference to DateTimePicker that is used in the example to call setMinDate and setMaxDate. The match $attr holds the id of the other date time picker so I can set up the onChange event. I can get the element by id using $( $attrs.match ) but the rest doesn't resolve and is undefined. Anyone know what the issue might be?
Using $element.data('DateTimePicker') gets a reference, but I need to get the paired date picker so I can change the min and max values that are allowed to be set on one with regards to the other. So for example if the fromDatePicker is Sept 1, 2014 then the toDatePicker can't choose anything before Sept 1, 2014 using the code from example 9, and vice versa.
Angular directive
.directive('datePicker', [function() {
var datePickers = [];
return {
restrict: 'A',
scope: {},
link: function( $scope, $element, $attrs ) {
$element.datetimepicker({
pickDate: true,
pickTime: false,
useCurrent: false,
showToday: true,
format: 'MMM DD, YYYY',
language: 'en'
});
// Check if date picker is used for determining a date range
if( $attrs.match ) {
// Check if date range minimum
if( $attrs.datePicker === 'min' ) {
// Setup date change event, and set the minimum date
$element.on( 'dp.change', function( e ) {
// NOTE: can get ref of element using match id, but
// data('DateTimePicker') doesn't resolve so setMaxDate is undefined
$( $attrs.match ).data('DateTimePicker').setMinDate( e.date );
});
}
// Check if date range maximum
else if( $attrs.datePicker === 'max' ) {
// Setup date change event, and set the maximum date
$element.on( 'dp.change', function( e ) {
// NOTE: can get ref of element using match id, but
// data('DateTimePicker') doesn't resolve so setMaxDate is undefined
$( $attrs.match ).data('DateTimePicker').setMaxDate( e.date );
});
}
}
}
};
Markup housing both directives:
<div class='col-md-4'>
<div class="form-group">
<label for="listDateRangeFrom">Date From</label>
<div class='input-group date'
id='listDateRangeFrom'
date-picker="min"
match="listDateRangeTo">
<input type='text'
class="form-control" >
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
</div>
</div>
<div class='col-md-4'>
<div class="form-group">
<label for="listDateRangeTo">Date To</label>
<div class='input-group date'
id='listDateRangeTo'
date-picker="max"
match="listDateRangeFrom">
<input type='text'
class="form-control">
<span class="input-group-addon">
<span class="fa fa-calendar"></span>
</span>
</div>
</div>
</div>