Two filter fields in the second - Datepicker

Asked

Viewed 218 times

0

Ola Stackoverflow Family

I’m finalizing a project and I’m having trouble with the datepicker. I have two datepicker fields (calendar and calendario1), working perfectly, Finding precise that the field "calendario1" present dates from the choice of the date of the field "calendario". I was able to make myself understood?

In short: the calendar field is chosen 14-01-2018 and the date options in the calendario1 should be from 15-01-2018

Follow the datepicker codes

<script>
$(function() {
    $( "#calendario" ).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: false,
        numberOfMonths: 2,
        maxDate: '-1d +12m',
        minDate: '+1d',
        dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
        dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
        dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']
    });
    $( "#calendario1" ).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: false,
        numberOfMonths: 2,
        maxDate: '-2d +12m',
        minDate: '+3d',
        dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
        dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
        dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']
    });
});
</script>
  • Could you mount an example on http://jsfiddle.net? This way it’s easier to help you

  • And then Rodrigo! Solved the question?

1 answer

0

To set an initial date on jQuery Datepicker the option is used minDate in format:

minDate: new Date(ano, mês, dia)

And to change the ownership of a input with Datepicker, you use:

$(seletor).datepicker("option", {propriedade: novo_valor} );

To detect a change in a date, you use the method onSelect:

onSelect: function(){
   executar algum código
}

So to change the initial date of the second input #calendario1, we use the onSelect in the first input passing the date selected in the function and taking the values (day, month and year):

onSelect: function(dia){
   var dia = dia.split("-").map(Number),
       d = dia.pop()+1,
       m = dia[1]-1,
       a = dia.shift();
   $("#calendario1").datepicker("option", {minDate: new Date(a, m, d)} );
}

Edit: I added a .val('') to empty and reset the second input.

Example (see comments in code):

$(function() {
    $( "#calendario" ).datepicker({
      onSelect: function(dia){ // o parâmetro "dia" é a data selecionada
         var dia = dia.split("-").map(Number), // quebro a data em array com números inteiros
             d = dia.pop()+1, // pego o dia seguinte ao selecionado
             m = dia[1]-1, // pego o mês selecionado -1 (é preciso subtrair por 1 porque o retorno é um mês a frente)
             a = dia.shift(); // pego o ano selecionado
         $("#calendario1").val('').datepicker("option", {minDate: new Date(a, m, d)} );
      },
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: false,
        numberOfMonths: 2,
        maxDate: '-1d +12m',
        minDate: '+1d',
        dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
        dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
        dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']
    });
    $( "#calendario1" ).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: false,
        numberOfMonths: 2,
        maxDate: '-2d +12m',
        minDate: '+3d',
        dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado','Domingo'],
        dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
        dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
        monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
        monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez']
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<input type="text" id="calendario" >
<input type="text" id="calendario1" >

  • I select the date in the calendar and calendar. So far OK. But if I select another date in the calendar again I see that the calendar 1 does not start from the new date chosen. You can update the date in the calendario1 if you select a new date in the calendar?

  • quick that my finger is itching for a down kkkkk

  • Now yes, effective and efficient!! + 1

  • I’m a little tired

  • I get it... sometimes it gets really sick.

Browser other questions tagged

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