简体   繁体   English

在angular-daterangepicker中选择startDate时更新日期

[英]Updating Date on startDate selection in angular-daterangepicker

I am using https://github.com/fragaria/angular-daterangepicker and http://www.daterangepicker.com/ to display a date-range picker inline. 我正在使用https://github.com/fragaria/angular-daterangepickerhttp://www.daterangepicker.com/来显示内联日期范围选择器。 Based on the type of flight selected in my app, I either require a date-range for travel dates, or a single date for a oneway flight. 根据我在应用中选择的航班类型,我要么要求一个日期范围作为旅行日期,要么要求一个日期作为单程航班。 The date object only seems to update however on the endDate selection - the second selection. date对象似乎只是在endDate选择(第二个选择)上更新。 This is an issue, as I really don't know how to set the startDate on the first selection. 这是一个问题,因为我真的不知道如何在第一个选择上设置startDate。

my controller function 我的控制器功能

//oneTripDate Calendar controller
 app.controller('OnewayPickerCtrl',['$scope','$rootScope', 'searchFactory',
function($scope, $rootScope, searchFactory) {
    $rootScope.flyDates = { departureDate : '' };
    $scope.onewaydate = {
        startDate: searchFactory.search.searchparams.journeys[0].departuredate, 
        endDate: searchFactory.search.searchparams.journeys[0].returndate
    };
    $scope.opts = {
        autoApply: true,
        minDate: moment(),
        startDate: moment(),
        alwaysShowCalendars: true,
        parentEl: '.cal-block .form-group',
        autoUpdateInput: true,
        singleDatePicker: true
    };

    //Watch for date changes
    $scope.$watch('onewaydate', function(newDate) {
        console.log('One Way model Changed');
        searchFactory.search.searchparams.journeys[0].departuredate = moment(newDate.startDate).format("YYYY-MM-DD");
    }, false);
}
]);

And this is where I include the input to be converted to the datepicker. 这是我包含要转换为日期选择器的输入的地方。

<div data-ng-controller="OnewayPickerCtrl">
      <input date-range-picker id="onewaydate" name="onewaydate" class="form-control date-picker" options="opts" type="text" value="" ng-model="onewaydate" required/>
</div>

As you can see I am watching the model to detect changes and then apply the value to my factory. 如您所见,我正在监视模型以检测更改,然后将该值应用于我的工厂。 This works a treat for date ranges, due to the 2nd selection being made, however it does not work on only one selection. 由于选择了第二个选项,因此可以对日期范围进行处理,但是它不能仅对一个选项有效。 In short, watching onewaydate only fires on the second selection. 简而言之,观看单向约会只会在第二个选择上触发。

Does anyone have experience with this scenario? 有人在这种情况下有经验吗?

The issue is that he Single Date picker does not "close" when it is forced to be inline. 问题在于,单日期选择器在被强制内联时不会“关闭”。 This means that the model does not update, or only once on initial selection. 这意味着该模型不会更新,或者在初始选择时不会更新一次。 The second selection is never picked up, hence the $watch function does not fire. 第二个选择永远不会被选择,因此$ watch函数不会触发。

my solution to this is as follows and now works 我对此的解决方案如下,现在可以工作

    $scope.optsOneway = {
        minDate: moment(),
        maxDate: moment(new Date().setFullYear(new Date().getFullYear() + 1)),
        autoUpdateInput: true,
        autoapply: true,
        singleDatePicker: true,
        parentEl: '.cal-block .form-group .oneway',
        eventHandlers: {
            'apply.daterangepicker': function(ev, picker) { 
                angular.element('#onewaydate').triggerHandler('click'); //one way calendar
            }
        }
    };


   //Watch for date changes
    $scope.$watch('onewayDate', function(newDate) {
        searchFactory.search.searchparams.journeys[0].departuredate = moment(newDate).format("YYYY-MM-DD");
        angular.element('#onewaydate').triggerHandler('click'); //one way calendar
    }, false);

I guess this is very hacky, but essentially I re-trigger the datepicker when ever a value is assigned. 我猜这很hacky,但是从本质上讲,一旦分配了值,我就会重新触发日期选择器。 It would seem this particular Datepicker is not built for the use we are throwing at it. 似乎该特定的Datepicker不是为我们要使用的功能而构建的。

I hope this might help someone. 我希望这可以帮助某人。

Cheers, 干杯,

I also had encountered this kind of problem somewhere, but in your case if you can share a fiddle for it then it will be easy to debug. 我在某处也遇到过此类问题,但是如果您可以为此共享一个小提琴,那么调试起来就很容易。

I went through the library and I found something very helpful for you: 我浏览了图书馆,发现有一些对您有帮助的东西:

https://github.com/fragaria/angular-daterangepicker/blob/master/js/angular-daterangepicker.js https://github.com/fragaria/angular-daterangepicker/blob/master/js/angular-daterangepicker.js

line 155: 155行:

$scope.$watch('model.startDate', function(n) {
         //some event
          return _setStartDate(n);
        });

You can emit an event from here, and get your desired result. 您可以从此处发出事件,并获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM