Problem defining 'allowTimes' in xdsoft Datetimepicker dynamically

Asked

Viewed 78 times

0

Well guys, I have a problem here with the Datetimepicker. I’m trying to set the times dynamically accepted with the allowTimes attribute. To make it easier the code is asism:

$('#datetimepicker2').datetimepicker({
    datepicker: false,
    format: 'H:i',
    onShow: function () {
        var id = $('#id').find('option:selected').val();
        var horarios = PegaHorarios(id);
        console.log("allowTimes:"+horarios);
        this.setOptions({
           allowTimes:horarios
        });
    }
});

Quickly explaining the code: I pick up the 'id' to do a search with the function Pegahorarios(), using AJAX, put 'dataType' as 'script' (already tried json, html none worked), the function returns me the values correctly, which I tested via console.log:

allowTimes:['14:00','14:20','14:40','15:00','15:20','15:40','16:00','16:20','16:40','17:00','17:20','17:40']

So I don’t know what to do... I tried some options and nothing, someone has some idea how to fix?

Thank you.

A, just for the record I copied and pasted this log in function and the code worked...

Edit 1: That’s how it works.

function PegaHorarios(id) {
var retorno = "";
carregando(true);
$.ajax({
    type: 'POST',
    dataType: 'script',
    url: 'includes/monta-horas.php',
    async: false, // não achei método alternativo
    // Valor default é true então essa opção é desnecessária agora - async: true,
    data: 'id='+id,
    success: function (response) {
        //console.log("Sucesso no envio da solicitação Ajax");
        //console.log(response);
        carregando(true);
        retorno = response;
    },
    error: function (response) {
        //console.log("Erro na Solicitação Ajax");
        //console.log(response);
        retorno = false;
    },
    complete: function (response) {
        //console.log("Solicitação Ajax Completada");
        //console.log(response);
        carregando(false);
    }

});
return retorno;
}

In the case the return comes from a PHP code that returns the String that is shown in the console.log.

//echo json_encode($retorno);
echo $retorno;

I used json_encode when dataType was json and the result was the same.

I don’t remember if you used JSON.parse. @Marconi, but I used it and it didn’t work.

Edit 2: To complete a summary, when showing on time in the datetimepicker I call the function

   PegaHorarios() 

with the value of a select field through

  $('#id').find('option:selected').val();

Already in the function Catch() I make an AJAX request to a PHP page that returns me a String with var_dump:

   string(97) "['14:00','14:20','14:40','15:00','15:20','15:40','16:00','16:20','16:40','17:00','17:20','17:40']"

I get this string and return in the function to the

var horarios;
  • What is the return of the function PegaHorarios()? Post her code.

  • you already tried gives a JSON.Parse(times)?

  • I edited with more information, @Marconi as I said up there I tested JSON.parse but it didn’t work. Thanks for the help.

  • @Rafaelfontenele you made the inclusion of Jquery before the datetimepicker? I made a test here everything works normal.

  • @Marconi, yes, is so much so that when I manually set allowTimes to allowTimes:['08:00'] (for example) it works. I thought it could be the scope of the variable times and I tried to make it global, but it didn’t help :/. Thanks again.

  • @Rafaelfontenele tries to use JSON again.Parse(times), I misspelled the "P" was capitalized, see if there is any console error.

  • When I use JSON.Parse says, Uncaught Typeerror: JSON.Parse is not a Function and when I use JSON.parse says, Uncaught Syntaxerror: Unexpected token '

Show 2 more comments

1 answer

0

I finally solved this. The changes in the code I made were those:

First of all in my PHP code where it returned a String with all times changed to return one:

array()
$retorno = array();

where each time is a position in the array.

After that I removed the:

dataType: 'script',

that even changing to 'JSON' did not work, which is strange... Then I used the

JSON.parse(horarios)

when using dataType as JSON gave error.

So I made a:

var teste = JSON.parse(horarios);

And I used it at the end:

this.setOptions({
       allowTimes:teste
    });

And now it finally works. Vlw!

Summarizing the problem of all this is due to the fact that allowTimes receive an array and not a string.

Browser other questions tagged

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