使用AngularJS的日期范围选择器指令

时间:2013-10-17 19:16:53

标签: javascript angularjs

我遇到日期范围选择器指令以及自定义和范围问题,我已将原始https://github.com/luisfarzati/ng-bs-daterangepicker指令修改为:

(function (angular) {
'use strict';

angular.module('ngBootstrap', []).directive('input', function ($compile, $parse) {
    return {
        restrict: 'E',
        require: 'ngModel',
        link: function ($scope, $element, $attributes, ngModel) {
            if ($attributes.type !== 'daterange') return;

            var options = {};
            options.format = $attributes.format || 'YYYY-MM-DD';
            options.separator = $attributes.separator || ' - ';
            options.minDate = $attributes.minDate && moment($attributes.minDate);
            options.maxDate = $attributes.maxDate && moment($attributes.maxDate);
            options.dateLimit = $attributes.limit && moment.duration.apply(this, $attributes.limit.split(' ').map(function (elem, index) { return index === 0 && parseInt(elem, 10) || elem; }) );
            options.ranges = $attributes.ranges && $parse($attributes.ranges)($scope);

            function format(date) {
                return date.format(options.format);
            }

            function formatted(dates) {
                return [format(dates.startDate), format(dates.endDate)].join(options.separator);
            }

            ngModel.$formatters.unshift(function (modelValue) {
                if (!modelValue) return '';
                return modelValue;
            });

            ngModel.$parsers.unshift(function (viewValue) {
                return viewValue;
            });

            ngModel.$render = function () {
                if (!ngModel.$viewValue || !ngModel.$viewValue.startDate) return;
                $element.val(formatted(ngModel.$viewValue));
            };

            $scope.$watch($attributes.ngModel, function (modelValue) {
                if (!modelValue || (!modelValue.startDate)) {
                    ngModel.$setViewValue({ startDate: moment().startOf('day'), endDate: moment().startOf('day') });
                    return;
                }
                $element.data('daterangepicker').startDate = modelValue.startDate;
                $element.data('daterangepicker').endDate = modelValue.endDate;
                $element.data('daterangepicker').updateView();
                $element.data('daterangepicker').updateCalendars();
                $element.data('daterangepicker').updateInputText();
            });

            $element.daterangepicker(options, function(start, end) {
                $scope.$apply(function () {
                    ngModel.$setViewValue({ startDate: start, endDate: end });
                    ngModel.$render();
                });
            });         
        }
    };
});

})(angular);

允许声明如下:

<date-range-picker ng-model="dateRange"
                   class="pull-right"
                   style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc"
                   min-date="{{minDate}}"
                   max-date="{{maxDate}}"
                   ranges="{'Last 7 Days': [moment().subtract('days', 6), moment()],
                            'Last Month': [moment().subtract('month', 1).startOf('month'), moment().subtract('month', 1).endOf('month')],
                            'Last 3 Months': [moment().subtract('month', 3).startOf('month'), moment().subtract('month', 1).endOf('month')]}">
    <i class="fontello-icon-calendar-alt-1"></i>
    <span>....</span>


    <b class="caret" style="margin-top: 8px"></b>
</date-range-picker>

选择2个日历的自定义范围作为原始指令正常工作,但选择其中一个预定义范围时会出现问题。 有人可以帮我这个吗?。

更新:

我已经解决了问题,它就是这样的:

<date-range-picker ng-model="dateRange"
                               class="pull-right"
                               style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc"
                               ng-model="dateRange"
                               ranges="dateRange.ranges"
                               >
                <i class="fontello-icon-calendar-alt-1"></i>
                <span></span>
                <b class="caret" style="margin-top: 8px"></b>
</date-range-picker>

对指令进行最小的更改:

((angular) ->
  "use strict"
  angular.module("directives.daterangepicker", []).directive "dateRangePicker", ($compile, $parse) ->
    restrict: "E"
    require: "ngModel"
    link: ($scope, $element, $attributes, ngModel) ->
      format = (date) ->
        date.format options.format
      formatted = (dates) ->
        [format(dates.startDate), format(dates.endDate)].join options.separator
      options = {}
      options.format = $attributes.format or "MMM D, YYYY"
      options.separator = $attributes.separator or " - "
      options.opens = $attributes.opens or "left"
      options.minDate = $attributes.minDate and moment($attributes.minDate)
      options.maxDate = $attributes.maxDate and moment($attributes.maxDate)
      options.dateLimit = $attributes.limit and moment.duration.apply(this, $attributes.limit.split(" ").map((elem, index) ->
        index is 0 and parseInt(elem, 10) or elem
      ))
      options.ranges = $attributes.ranges and $parse($attributes.ranges)($scope)
      ngModel.$formatters.unshift (modelValue) ->
        return ""  unless modelValue
        modelValue

      ngModel.$parsers.unshift (viewValue) ->
        viewValue

      ngModel.$render = ->
        return  if not ngModel.$viewValue or not ngModel.$viewValue.startDate
        $element.find("span").html(formatted(ngModel.$viewValue))

      $scope.$watch $attributes.ngModel, (modelValue) ->
        if not modelValue or (not modelValue.startDate)
          ngModel.$setViewValue
            startDate: moment().startOf("day")
            endDate: moment().startOf("day")

          return
        $element.data("daterangepicker").startDate = modelValue.startDate
        $element.data("daterangepicker").endDate = modelValue.endDate
        $element.data("daterangepicker").minDate = modelValue.minDate
        $element.data("daterangepicker").maxDate = modelValue.maxDate
        $element.data("daterangepicker").updateView()
        $element.data("daterangepicker").updateCalendars()
        #$element.data("daterangepicker").updateInputText()

      $element.daterangepicker options, (start, end) ->
        alert "$apply#{moment(start).toDate()}"
        alert options.maxDate
        $scope.$apply ->
          ngModel.$setViewValue
            startDate: start
            endDate: end

          ngModel.$render()



) angular

0 个答案:

没有答案