问题
I'm using Bootstrap Daterange Picker, which uses the following range options :
ranges: {
'Today': [moment(), moment()],
'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
},
But when the page loads the selected value is empty.
I want to select the range 'This Month' by default when the page loads, in bootstrap daterangepicker.
Jsfiddle Url: https://jsfiddle.net/renishar/wnuy6ypc/
回答1:
Just set the startDate and endDate options to the respective value of This month.
Also call cb function after initialise your datepicker to set the first value
See here : https://jsfiddle.net/wnuy6ypc/6
var dat_opt = {
...,
startDate: moment().startOf('month'),
endDate: moment().endOf('month'),
ranges: {
...
},
}
EDIT : After initialisation add jQuery('.daterange').trigger("apply.daterangepicker");
来源:https://stackoverflow.com/questions/40993211/selecting-this-month-range-by-default-in-bootstrap-daterangepicker