Sandeep
Sandeep

Reputation: 69

How to use the linked pickers in angular-eonasdan-datetimepicker?

I am using angular-eonasdan-datetimepicker, an angular wrapper for eonasdan-datetimepicker. How to keep the end date always greater than start date. Also disable the previous dates till start date in end date calendar.

I tried with on-change event to update the UI but its not happening. The event is getting called but the value in not updating.

Inside the onchange event of end date:

$scope.FormName.enddate = $scope.startdate;

i.e.

(FormName.elementName) = userselected value of start date from model.

In jquery it uses :

<script type="text/javascript">
    $(function () {
        $('#datetimepicker6').datetimepicker();
        $('#datetimepicker7').datetimepicker({
            useCurrent: false //Important! See issue #1075
        });
        $("#datetimepicker6").on("dp.change", function (e) {
            $('#datetimepicker7').data("DateTimePicker").minDate(e.date);
        });
        $("#datetimepicker7").on("dp.change", function (e) {
            $('#datetimepicker6').data("DateTimePicker").maxDate(e.date);
        });
    });
</script>

the angular wrapper code look like this :

(function() {
  'use strict';

  var module = angular.module('ae-datetimepicker', []);

  module.directive('datetimepicker', [
      '$timeout',
      function($timeout) {
        return {
          require: '?ngModel',
          restrict: 'EA',
          scope: {
            options: '@',
            onChange: '&',
            onClick: '&'
          },
          link: function($scope, $element, $attrs, controller) {
            $element.on('dp.change', function() {
              $timeout(function() {
                var dtp = $element.data('DateTimePicker');
                controller.$setViewValue(dtp.date());
                $scope.onChange();
              });
            });

            $element.on('click', function() {
              $scope.onClick();
            });

            controller.$render = function () {
              if (!!controller) {
                if (controller.$viewValue === undefined) {
                  controller.$viewValue = null;
                }
                else if (!(controller.$viewValue instanceof moment)) {
                  controller.$viewValue = moment(controller.$viewValue);
                }
                $element.data('DateTimePicker').date(controller.$viewValue);
              }
            };


            $element.datetimepicker($scope.$eval($attrs.options));
          }
        };
      }
    ]);
})();

Any lead will be very helpful.

Upvotes: 0

Views: 1041

Answers (1)

Atais
Atais

Reputation: 11285

I guess there is no need to pass the source of the lib: https://github.com/atais/angular-eonasdan-datetimepicker

I have updated the lib so it is easier/possible to archive what you requested.

Basically, now the options is an object instead of JSON String, fe:

vm.options = {
    format: "DD.MM.YYYY",
    maxDate: dateTo
};

and the changes are being watched and applied. The plunker with demo is here: http://plnkr.co/ZSjN8f

Upvotes: 0

Related Questions