简体   繁体   中英

Allow null for single date in Date range picker

I am working with angular-date rangepicker . For my requirement I need to have the option to set date as null for start date/end date or both. I looked into its option but didn't find any.

I also have the requirement to show checkbox in calendar popup to set startDate as null and checkbox for set endDate as null.

I found one plunker for setting null, but it sets both date as null/blank Plunker

The plunker uses

 eventHandlers: {
      'cancel.daterangepicker': function(ev, ev1) {
        $scope.datePicker.date = {};
        // $scope.setRange();
        var picker = $element.find('#daterange4').data('daterangepicker');
        picker.setStartDate(new Date);
        picker.setEndDate(new Date);
      }
    }

That clears both date. In my case I want to have option to clear any one date.

Prepare model in your controller. The model must have startDate and endDate attributes:

exampleApp.controller('TestCtrl', function ($scope) {
   //make initial values null
    $scope.datePicker.date = {startDate: null, endDate: null};

    //method to change start date back to null
    $scope.makeStartDateNull = function(){
        $scope.datePicker.date.startDate = null;
    }
}

In HTML you just bind this makeStartDateNull like

<input type="checkbox" ng-model="confirmed" ng-change="makeStartDateNull()" id="ng-change-example1" />

You can do same for the end date.

Have you tried just setting it to null? This is the standard plunker as given by datepicker with the option to set the date to null by:

  $scope.clear = function() {
    $scope.dt = null;
  };

It works on that plunker. I do know there was a bug in earlier versions of datepicker that did what you describe. Check your version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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