Selecting 'This Month' range by default in bootstrap daterangepicker

ぃ、小莉子 提交于 2019-12-22 10:17:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!