Multiple instances of daterange pickers on the same page

javascript jquery twitter-bootstrap bootstrap-daterangepicker

698 观看

1回复

70 作者的声誉

I have two Bootstrap date range pickers. When I change the date of one picker, the same date appears in the second date range picker.

How can I separate the two date range pickers, so they each operate independently?

Code:

First date picker:

<div id="reportrange" class=" form-control">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
<b class="caret "></b>&nbsp;<span></span> 
</div>

Second date picker:

<div id="usersdaterange" class=" form-control"> <i class="glyphicon 
glyphicon-calendar fa fa-calendar"></i>&nbsp;<b class="caret "></b>&nbsp;
<span></span>
</div>

JS:

$('#usersdaterange 
span ').on('
  apply.daterangepicker ',function(ev1,picker1) {
  var username1 = $("#userfilter").val();
  var start1 = picker1.startDate.format('YYYY-MM-DD');
  var end1 = picker1.endDate.format('YYYY-MM-DD');
});

$('#reportrange span').on('apply.daterangepicker', function(ev, picker) {
  $("#loader").show();
  var username = $("#userfilter").val();
  var start = picker.startDate.format('YYYY-MM-DD');
  var end = picker.endDate.format('YYYY-MM-DD');
});
作者: moazzam 的来源 发布者: 2017 年 12 月 27 日

回应 1


0

1072 作者的声誉

Bootstrap daterangepicker works with multiple instances out of the box.

$(function() {
  $('#dateRange1').daterangepicker({}, onSelect);
  $('#dateRange2').daterangepicker({opens:"left"}, onSelect);
});


function onSelect(start, end, label) {
    console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
  }
body{
  padding:20px;
}

#dateRange1.form-control,
#dateRange2.form-control{
  width:45vw;
}

#dateRange1{
  position:absolute;
  left:10px;
}

#dateRange2{
  position:absolute;
  right:10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" rel="stylesheet"/>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>

<input id="dateRange1" class="form-control" type="text" name="daterange" value="01/01/2018 - 01/15/2018" />

<input id="dateRange2" class="form-control" type="text" name="daterange" value="01/01/2018 - 01/15/2018" />

(JSFiddle)

If the two are linked then it must be because there is some code that is updating them together. Perhaps the most obvious possibility is that there is some function being called on apply.daterangepicker that is then updating the other datepicker with setStartDate or setEndDate etc.

(Unfortunately it's not clear what the original askers problem is from the code they have posted, but I thought I would answer this question anyway to clear up any potential confusion for anyone searching for this)

作者: ChrisM 发布者: 2019 年 4 月 1 日
32x32