使用 jonthornton 的时间选择器 jquery 插件动态禁用特定时间



正如标题所示,我需要动态禁用我从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>

我想我正在创建对阵列错误,但我似乎无法弄清楚它的位置。

我不确定您需要做什么,但是我认为firebase API是异步的。如果API是异步的,则必须将DataPiker初始化移动到Firebase API回调中:

创建回调函数:

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

以这种方式,您将对数组的搭配与您从数据库架的数据进行重视。

最新更新