Why does Bootstrap 4 datepicker API not work?

Asked

Viewed 3,852 times

1

I’m creating a calendar, somewhere in the view, I needed an input for the user to enter a date. Since I’m wearing bootstrap 4 and bootstrap has several Addons for datepickers, I used one of them. On the front end, nothing to say, it works great. When in the back-end, when I use API methods, options or events in jquery, they do not execute.

console.log($date = "data");
$('.datepicker').datepicker({
		format: 'dd/mm/yyyy'
}).on('changeDate', function(e) {
$date = $('.datepicker').datepicker('getDate');
		console.log($date);
});
<!-- Bootstrap CND -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
	<!-- Datapicker css -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/locales/bootstrap-datepicker.pt.min.js"></script>
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<div class="container-fluid">
  <div class="row">
    <div class="col-md-3">
	    <div class="input-group date" data-provide="datepicker" id="sandbox-container" style="flex: 0 0 auto;">
			<input type="text" class="form-control" class="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true" data-date-today-highlight="true">
			<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
				<i class="fa fa-calendar" aria-hidden="true" style="align-self: stretch"></i>
			</div>
		</div>
	  </div>
  </div>
</div>

[documentation] - I’ve been guided by the documentation, I may be using it incorrectly, you can identify the error? documentation

Can someone help me? Regards

[update] 2/03/2019 - Options already execute

According to the documentation: Data API As with bootstrap’s Own plugins, datepicker provides a data-api that can be used to instantiate datepickers without the need for custom javascript. For Most datepickers, Simply set data-provide="datepicker" on the element you want to initialize, and it will be intialized lazily, in true bootstrap fashion. For inline datepickers, use data-provide="datepicker-inline" ; These will be immediately initialized on page load, and cannot be lazily Loaded.

I changed the attribute data-provide="datepicker" for data-provide="datepicker-inline", with this part of jquery has already performed, however events are not yet dispachados.

$('.datepicker').datepicker({
            format: 'dd/mm/yyyy'
    })

2 answers

2

Hello I made some tests and the problem is in using INPUT you must use DIV in datetimer, I think there is a better library than the bootstrap you are using, try it’s here from jQuery.UI.

But anyway so it worked just try to adapt to the look you want.

$('#datepicker').datepicker();
$('#datepicker').on('changeDate', function(e){
    $date = $('#datepicker').datepicker('getDate');
		console.log($date);
});
<!-- Bootstrap CND -->
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
	<!-- Datapicker css -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/css/bootstrap-datepicker.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/js/bootstrap-datepicker.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.8.0/locales/bootstrap-datepicker.pt.min.js"></script>
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<div class="container-fluid">
  <div class="row">
    <div class="col-md-3">
	    <div class="input-group date" data-provide="datepicker" id="sandbox-container" style="flex: 0 0 auto;">
			<div id="datepicker" data-date-format="dd/mm/yyyy" data-date-autoclose="true" data-date-today-highlight="true"></div>
			<div class="input-group-addon" style="justify-content: center;align-items: center;flex: 0 0 25px;">
				<i class="fa fa-calendar" aria-hidden="true" style="align-self: stretch"></i>
			</div>
		</div>
	  </div>
  </div>
</div>

  • 1

    Thanks for the help. Actually the library suggested at first glance seems to perform better. However, it doesn’t solve the initial problem. I answered my question and I was able to solve part of the problem, you think you can improve my answer?

  • What do you want to do? no intendi. want to initialize it with a date that comes from an api?

  • Basically, when I change the date-provide attributo to "datepicker-inline", datepicker loads when loading the page, that is, it ceases to be "lazely", so when running the script, it can already be initialized.

  • I still could not intender, putting this attribute it loads faster?

  • Yeah, but it’s not just that. I changed the answer, the code was wrong. I know the term 'Azy' in other contexts of programming, in this I do not know how it works. For example, imagine you have a property "height=5cm", then you do setAltura(8cm), however if the property is 'Lazy' the height remains 5cm. Now, if you do getAltura(), the height changes to the value of the last set and returns 8cm. https://pt.wikipedia.org/wiki/Avalia%C3%A7%C3%A3o_pregui%C3%A7osa

  • In this case, the calendar being 'Lazy', only starts moments before it is used. Hence the script does not perform any of the functions. I still have to find a method/event that costs as soon as it is initialized.

Show 1 more comment

1

[Quasi-resolved]

DATA API Like the bootstrap plug-ins themselves, datepicker provides a data API that can be used to instantiate datepickers without the need for custom javascript. For most datepickers, simply set data-provide = "datepicker" on the element you want to initialize, and it will be idlely initialized in the form of true bootstrap.

[Unresolved] - In these cases I still don’t know which jquery code waits for the datepicker initialization. I tried to

$('.datepicker').ready(function() {
            $('.datepicker').datepicker();
            $('#sandbox-container input').datepicker({
            format: "dd/mm/yyyy"
            }).on('changeDate', function(e) {
            $date = $('.datepicker').datepicker('getDate');
            console.log($date);
            });
        });

[debbuging] - enters the function given in ready, but does not execute options or events.

[Busy] - For in-line, use data-provide = "datepicker-inline"; these will be initialized immediately on page loading and cannot be loaded with sloth.

Thus changing the HTML

<div class="input-group date" data-provide="datepicker-inline" id="sandbox-container" style="flex: 0 0 auto;">

$(document).ready(function() {
        $('#sandbox-container input').datepicker({
        format: "dd/mm/yyyy"
        }).on('changeDate', function(e) {
        console.log('Aqui');
        $date = $('.datepicker').datepicker('getDate');
        console.log($date);
        });
    });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.