angular-bootstrapbootstrap-daterangepicker

Ui-tabset and bootstrap-daterangepicker not working together?


I have an application where I am using ui-bootstrap-tabs. documentation here. With the ng-bs-daterangepicker. The behavior I am observing is that whenever I put the daterangepicker inside the ui-tab. It is not able to catch the events attached to it. But when I move that input tag outside the ui-tabs, it's able to catch the events associated with it.

I have created a working plunker to highlight my issue.

<body ng-app="myApp">
    <div ng-controller="testController">
      <uib-tabset>
        <uib-tab index="0" heading="Drivers"></uib-tab>
        <uib-tab index="1" heading="Charts">
          <input class="btn btn-danger" type="daterange" id="daterange1" ng-model="dates" format="DD MMM" ranges="ranges" />
        </uib-tab>
      </uib-tabset>
      <!-- <input class="btn btn-danger" type="daterange" id="daterange1" ng-model="dates" format="DD MMM" ranges="ranges" /> -->
    </div>
  </body>

Here there are two input tags. One inside the uib-tab and other outside it.

 $scope.dates = {
    startDate: moment().startOf('day'),
    endDate: moment().endOf('day')
  };

  $scope.ranges = {
    'Today': [moment(), moment()],
    'Yesterday': [moment().subtract('days', 1), moment().subtract('days', 1)],
    'Last 7 days': [moment().subtract('days', 7), moment()],
    'Last 30 days': [moment().subtract('days', 30), moment()],
    'This month': [moment().startOf('month'), moment().endOf('month')]
  };
  $('#daterange1').on('apply.daterangepicker', function(ev, picker) {
    console.log(picker.startDate);
    console.log(picker.endDate);
  });

The event apply.daterangepicker is not called when I activate the inside input button but is called when I activate the outside one.

My approach
I am guessing it's not a scope issue as highlighted in some posts. Because if it was that, then how come even the date is being populated. Another thing could be the Known issue column which says

To use clickable elements within the tab, you have override the tab template to use div elements instead of anchor elements, and replicate the desired styles from Bootstrap's CSS. This is due to browsers interpreting anchor elements as the target of any click event, which triggers routing when certain elements such as buttons are nested inside the anchor element.

maybe somehow this is stopping the event propagation. I am stuck at this point and can't think of a solution on how to fix it. Hoping the community would help here...


Solution

  • In case of $('#daterange1').on the object to which the event if getting attached must exist at the moment when .on() is invoked.

    When daterangepicker is initialized inside Tabs component, you could attach event like this:

    $("body").on("apply.daterangepicker", "#daterange1", function(e,picker) {
        console.log(picker.startDate);
        console.log(picker.endDate);
    });
    

    Modified plunker