Problem setting 'allowTimes' in xdsoft DateTimePicker dynamically

0

Well folks, I have a problem here with DateTimePicker . I'm trying to set the dynamically accepted times with the allowTimes attribute. To get easier the code tá 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 take the 'id' to do a search with the function PicksTime (), using AJAX , I put the 'dataType' as a '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 do not know what to do ... I tried some options and nothing, does anyone have any idea how to fix it?

Thank you.

A, just to note I copied and pasted this log into the function and the code worked ...

Edit 1: This is the PegaTime function.

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 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 the dataType was json and the result was the same.

I do not remember if I used JSON.parse @Marconi , but I used it and it did not work.

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

   PegaHorarios() 

with the value of a select field through

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

In the PegaHararios () function I make an AJAX request to a PHP page that returns 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 the function to the

var horarios;
    
asked by anonymous 13.11.2015 / 03:09

1 answer

0

I finally figured this out. The changes to the code I made were these:

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

array()
$retorno = array();

where each time is a position in the array.

After that I removed:

dataType: 'script',

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

JSON.parse(horarios)

That when I used the dataType as JSON it was an error.

So I made one:

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 receives an array and not a string.

    
19.11.2015 / 17:20