Search code examples
javascriptjquerydatetimepickertimepicker

Dynamically disable certain time using jonthornton's timepicker jquery plugin


As the title says, I need to dynamically disable certain times that I'm getting from a firebase database. I can't explain it well, so here's the code:

<html>
<head>
    <title></title>
    <link rel="stylesheet" href="jquery/jquery-ui.css" />
    <script src="jquery/jquery-1.9.1.js"></script>
    <script src="jquery/jquery-ui.js"></script>
    <link rel="stylesheet" href="timepicker/jquery.timepicker.css">
    <script src="timepicker/jquery.timepicker.min.js"></script>
</head>
<body>
    <input type="text" id="timepicker" placeholder="Select time">
    <script>
        /*Initialize Firebase*/
        var db = firebase.database().ref();
        var s, f, pairs=Create2DArray(100);
        var pair = [ ['00:00', '10:00'], ['17:00', '23:00'] ];
        firebase.database().ref("Path/To/Parent/Of/Date/"+"20-11-2017"/*access the path to date in the databse(time's parent)*/).once('value').then(function(snapshot){
            snapshot.forEach(function(child) {
                s = JSON.stringify(child.key); //the start of disabled time
                f = JSON.stringify(child.val()); // the end of disabled time
                pairs.push([s, f]);
                console.log("Pairs:" + pairs);
            });
        });
        function Create2DArray(rows) {
           var arr = [];

            for (var i=0;i<rows;i++) {
               arr[i] = [];
            }

          return arr;
        }
        jQuery(function(){
            $('#timepicker').timepicker({ 'timeFormat': 'H:i', 'step':'10','forceRoundTime': true});
            $('#timepicker').timepicker( 'option', {
                'disableTimeRanges': pairs //if i use here the pair array instead it works, but I need to disable the dates dynamically, and probably multiple times
            });
        });
    </script>
</body>

I suppose that I'm creating the pairs array wrong, but I can't seem to figure out where it's wrong.


Solution

  • I'm not sure to understand what you need to do however I think that the firebase API is Async. If the API are async you have to move the dataPiker initialization inside the firebase API callback:

    Create a callback function:

    toClandar(){
        $(function(){
            $('#timepicker').timepicker({ 'timeFormat': 'H:i', 'step':'10','forceRoundTime': true});
            $('#timepicker').timepicker( 'option', {
                 'disableTimeRanges': pairs
            });
        });
    }
    

    Than call the function inside the API callback:

    firebase.database().ref("Path/To/Parent/Of/Date/"+"20-11-2017"/*access the path to date in the databse(time's parent)*/).once('value').then(function(snapshot){
       snapshot.forEach(function(child) {
           s = JSON.stringify(child.key); //the start of disabled time
           f = JSON.stringify(child.val()); // the end of disabled time
           pairs.push([s, f]);
           console.log("Pairs:" + pairs);
           //callback
           toCalendar();
           //end callback
       });
    });
    

    in this way you pair array is valued with the data you have piked from your database.