使用jonthornton的timepicker jquery插件动态禁用某些时间

时间:2017-11-21 12:35:41

标签: javascript jquery datetimepicker timepicker

正如标题所说,我需要动态禁用我从firebase数据库获取的某些时间。我无法解释清楚,所以这是代码:

<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>

我认为我正在创建对数组错误,但我似乎无法弄清楚错误的位置。

1 个答案:

答案 0 :(得分:0)

我不确定你需要做什么,但我认为firebase API是Async。如果API是异步的,则必须在firebase API回调中移动dataPiker初始化:

创建回调函数:

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

比调用API回调中的函数:

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
   });
});

通过这种方式,您可以使用从数据库中获取的数据对对数组进行估值。