How do I get bootstrap-datepicker to work with Bootstrap 3?

后端 未结 2 412
北海茫月
北海茫月 2020-12-12 15:43

I use the version of bootstrap-datepicker maintained by eternicode (Andrew Rowls).

On Bootstrap 2 it worked, but now it doesn\'t work with the Bootstrap 3 library.

2条回答
  •  不思量自难忘°
    2020-12-12 16:23

    I also use Stefan Petre’s http://www.eyecon.ro/bootstrap-datepicker and it does not work with Bootstrap 3 without modification. Note that http://eternicode.github.io/bootstrap-datepicker/ is a fork of Stefan Petre's code.

    You have to change your markup (the sample markup will not work) to use the new CSS and form grid layout in Bootstrap 3. Also, you have to modify some CSS and JavaScript in the actual bootstrap-datepicker implementation.

    Here is my solution:

    CSS changes in datepicker.css on lines 176-177:

    .input-group.date .input-group-addon i,
    .input-group.date .input-group-addon i {
    

    Javascript change in datepicker-bootstrap.js on line 34:

    this.component = this.element.is('.date') ? this.element.find('.input-group-addon') : false;
    

    UPDATE

    Using the newer code from http://eternicode.github.io/bootstrap-datepicker/ the changes are as follows:

    CSS changes in datepicker.css on lines 446-447:

    .input-group.date .input-group-addon i,
    .input-group.date .input-group-addon i {
    

    Javascript change in datepicker-bootstrap.js on line 46:

     this.component = this.element.is('.date') ? this.element.find('.input-group-addon, .btn') : false;
    

    Finally, the JavaScript to enable the datepicker (with some options):

     $(".input-group.date").datepicker({ autoclose: true, todayHighlight: true });
    

    Tested with Bootstrap 3.0 and JQuery 1.9.1. Note that this fork is better to use than the other as it is more feature rich, has localization support and auto-positions the datepicker based on the control position and window size, avoiding the picker going off the screen which was a problem with the older version.

提交回复
热议问题