Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current viewMode property from "Bootstrap Datepicker"

How to get current viewMode property from "Bootstrap Datepicker"? I initialize the control with viewMode= 'years' and I want to close datepicker on changeDate event, only when viewMode='days'.

The user selects a year, then a month, and finally a day. In that moment the control must be closed.

This is the code:

$("#date").datepicker(
    {viewMode: 'years',
     format: 'dd/mm/yyyy'
});

$('#date').on('changeDate', function (ev) {
    //close when viewMode='0' (days)
})

Can anyone help?

like image 641
Gonzalo Avatar asked Feb 17 '23 18:02

Gonzalo


2 Answers

Check this : http://jsfiddle.net/nAXnM/

HTML

    <input type="text" class="span2" value="02/16/12" data-date-format="mm/dd/yy" id="dp2" >

JS

$("#dp2").datepicker({
 viewMode: 'years',
 format: 'dd/mm/yyyy'
});

$('#dp2').on('changeDate', function (ev) {
   //close when viewMode='0' (days)
   if(ev.viewMode === 'days'){
      $('#dp2').datepicker('hide');
   }
})
like image 136
Praveen Vijayan Avatar answered Feb 20 '23 08:02

Praveen Vijayan


If you're using the forked version of Bootstrap Datepicker, to close the UI widget when a date is selected, set the autoclose option to true:

$("#date").datepicker({
    autoclose: true
});
like image 28
Sam Avatar answered Feb 20 '23 09:02

Sam