Search code examples
htmlcsstwitter-bootstrapbootstrap-datetimepicker

How do I set default settings for Datetimepicker?


I have multiple calendar inputs set up with Bootstrap Datetimepicker: http://eonasdan.github.io/bootstrap-datetimepicker/.

I have created an object containing some overwrites:

DatetimepickerDefaults = {
    icons: {
        time: 'fa fa-clock-o',
        date: 'fa fa-calendar',
        up: ' fa fa-angle-up',
        down: 'fa fa-angle-down',
        previous: 'fa fa-angle-left',
        next: 'fa fa-angle-right',
        today: 'fa fa-crosshairs',
        clear: 'fa fa-trash',
        close: 'fa fa-times'
    }
}

that I want to apply to all of the calendar inputs. So now when I want to init a plugin I write:

$('#datetimepicker1').datetimepicker(DatetimepickerDefaults);

The problem is that some of the inputs need to have some special settings, like format: 'LT' so when I init them, I write:

$('#datetimepicker3').datetimepicker({
    icons: {
        time: 'fa fa-clock-o',
        date: 'fa fa-calendar',
        up: ' fa fa-angle-up',
        down: 'fa fa-angle-down',
        previous: 'fa fa-angle-left',
        next: 'fa fa-angle-right',
        today: 'fa fa-crosshairs',
        clear: 'fa fa-trash',
        close: 'fa fa-times'
    },
    format: 'LT'
});

so I have to repeat this default settings all over again. Is there any way to make it default for all of the plugin instances?


Solution

  • You can set it as a function like:

    function.DatetimepickerDefaults = function(field, value) {
        var retVal = {
        icons: {
            time: 'fa fa-clock-o',
            date: 'fa fa-calendar',
            up: ' fa fa-angle-up',
            down: 'fa fa-angle-down',
            previous: 'fa fa-angle-left',
            next: 'fa fa-angle-right',
            today: 'fa fa-crosshairs',
            clear: 'fa fa-trash',
            close: 'fa fa-times'
        }};
        if (field) {
            retVal[field] = value;
        }
        return retVal;
    }
    

    And use it like:

    $('#datetimepicker1').datetimepicker(DatetimepickerDefaults());
    $('#datetimepicker2').datetimepicker(DatetimepickerDefaults("format", "LT"));
    $('#datetimepicker2').datetimepicker(DatetimepickerDefaults("format", "LT2"));
    //And etc...
    

    You can use lists for more values, I hope this helps