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'
})
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?
– Pedro Correia
What do you want to do? no intendi. want to initialize it with a date that comes from an api?
– Lodi
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.
– Pedro Correia
I still could not intender, putting this attribute it loads faster?
– Lodi
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
– Pedro Correia
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.
– Pedro Correia