Update bootstrap 3 datepicker options dynamically?
Asked Answered
G

3

7

This datepicker it is already created with other default options, but I need to update it with the following new options and it does not seem to work:

// new options
var new_options = {
    format: 'dd/mm/yyyy',
    autoclose: true,
    language: 'es'
}

// update values
$("#fecha_periodo").datepicker("update", new_options );

I would also like to update other options like daysOfWeekDisabled, viewMode and so on.

Gonagle answered 30/5, 2017 at 14:43 Comment(8)
From the update area within the bootstrap datepicker, it seems very clear that the options specify updating values. bootstrap-datepicker.readthedocs.io/en/latest/…Stain
I saw that already, it changes values but not optionsGonagle
Try the format option. bootstrap-datepicker.readthedocs.io/en/latest/…Stain
looks great but I would like to update options at once, there is also some other properties that need to be updated like viewMode and languageGonagle
Language is one of the properties and I believe format as well.Stain
how can I change the viewMode?Gonagle
I am having a hard time trying to find how to change a simple option LOLGonagle
Let us continue this discussion in chat.Stain
V
10

Unfortunately, there is no API to change options dinamically, all availables methods are listed here (you can use setDaysOfWeekDisabled to set dinamically days of week disabled).

You can use destroy method to destroy datepicker instance and build it again with the new options.

Here a live example:

$('#datepicker').datepicker();

$('#btnChange').click(function(){
  var new_options = {
    format: 'dd/mm/yyyy',
    autoclose: true,
    language: 'es'
  }
  // Save value
  var value = $('#datepicker').datepicker('getDates');
  // Destroy previous datepicker
  $('#datepicker').datepicker('destroy');
  // Re-int with new options
  $('#datepicker').datepicker(new_options);
  // Set previous value
  $('#datepicker').datepicker('setDates', value);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.6.4/locales/bootstrap-datepicker.es.min.js"></script>

<input type="text" class="form-control" id="datepicker">

<button id="btnChange" class="btn btn-primary">Change option</button>
Vocable answered 30/5, 2017 at 15:46 Comment(2)
I was about to change to datepicker "v4" but it does not seem to be an official site, I do not know if you have observed that there is a lot of bootstrap "datepickers" and "datetimepickers" sites that got the people confused and do not know which one to use, I only use bootstraps built-int. I appreciate your answer and will use that, Regards.Gonagle
Unfortunately bootstrap itself does not include a datepicker, but there are a lot of independent datepickers with bootstrap theme. In my opinion, the best one is bootstrap-datetimepicker by eonasdan. It uses momentjs, it has a rich API and it's well documented.Vocable
S
0

You can find all about new version (Bootstrap 3 Datepicker v4 Docs) at https://eonasdan.github.io/bootstrap-datetimepicker/.

let new_options = {
    icons: {
        time: "fa fa-clock-o",
        date: "fa fa-calendar",
        up: "fa fa-chevron-up",
        down: "fa fa-chevron-down",
        previous: 'fa fa-chevron-left',
        next: 'fa fa-chevron-right',
        today: 'fa fa-screenshot',
        clear: 'fa fa-trash',
        close: 'fa fa-remove'
    },
    format: 'DD/MM/YYYY',
    date: new Date()
}

if(condition1){
        new_options.daysOfWeekDisabled = [0,2,3,4,5,6];
        new_options.viewMode = 'years';
        new_options.format = 'MM/YYYY';
    }else(condition2){
        new_options.viewMode = 'years';
        new_options.format = 'YYYY';
    }
    // Destroy previous datepicker
    $('.datetimepicker').data("DateTimePicker").destroy();
    // Re-int with new options
    $('.datetimepicker').datetimepicker(new_options);

Sorry for my poor english please :)

Sonority answered 5/6, 2020 at 10:17 Comment(0)
G
0

Here I have a hacking solution that works for me to change the datepicker options dynamically after I initiate a bootstrap datepicker object.

var id = "MyDatepicker"
var beginDate = new Date();
var endDate =  new Date(2022, 11, 17, 3, 24, 0);

function myCallBack(date) {
    if ((beginDate <= date) && (date <= endDate)){
        return {classes: 'activeClass'};
    }
    return;
}

$('#' + id ).datepicker({
    startDate: new Date(),
    language: 'en'
});

$('#' + id ).datepicker('_process_options', {
    beforeShowDay: function(date) {
        return myCallBack(date);
    }
}).datepicker('update','')

In this case. I apply a class to a range of dates on the datepicker.

I use the "non public" datepicker method '_process_options' to change intern datepicker methods.

With this method I can change all the options of the datepicker instance because the '_process_options' has no method validator.

Also I use the method 'update' bacause the dobbleclick bug: bootstrap datepicker, beforeShowDay works after second click

Garment answered 26/2, 2021 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.