Doubt with DATAPICKER

Asked

Viewed 50 times

1

I am using Datapicker and would like to ask a question about just enable a specific day.

<script>
    $(function () {
        var datepicker = document.getElementById('<%=txtDataRetirada.ClientID%>');
        $("#MainContent_txtDataRetirada").datepicker({
            minDate: 0,
            dateFormat: 'dd/mm/yy',
            dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
            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'],
            nextText: 'Próximo',
            prevText: 'Anterior'
        });
    });
</script>
  • 2

    Can you explain the doubt better? You want him to accept only one day a year, a month, a week?

  • I want you to enable just a few days that I will spend

1 answer

1


To API of .datepicker() has the method for this, beforeShowDay that accepts a function that calls for each date. This function has to return an array with the following index (only the first is required):

[0]: true / false indicating whether the day can be selected or not
[1]: a CSS class to date
[2]: an optional text for the tooltip of that date

The code would be:

var datasLivres = ['30-4-2017', '2-5-2017', '9-5-2017', '18-5-2017'];
function verificadorDeDatas(d) {
    var data = [d.getDate(), d.getMonth() + 1, d.getFullYear()].join('-');
    return [datasLivres.indexOf(data) != -1];
}
$("#MainContent_txtDataRetirada").datepicker({
    beforeShowDay: verificadorDeDatas,
    minDate: 0,

Example:

$(function() {
  // para testar muda estas datasLivres para outras datas
  var datasLivres = ['30-4-2017', '2-5-2017', '9-5-2017', '18-5-2017'];
  function verificadorDeDatas(d) {
    var data = [d.getDate(), d.getMonth() + 1, d.getFullYear()].join('-');
    return [datasLivres.indexOf(data) != -1];
  }
  $("#MainContent_txtDataRetirada").datepicker({
    beforeShowDay: verificadorDeDatas,
    minDate: 0,
    dateFormat: 'dd/mm/yy',
    dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
    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'],
    nextText: 'Próximo',
    prevText: 'Anterior'
  });
});
<link href="https://code.jquery.com/ui/jquery-ui-git.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/jquery-ui-git.js"></script>

<input type="text" id="MainContent_txtDataRetirada" />

Browser other questions tagged

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