2011-11-16 26 views
1

這裏是情況:如何在jQuery-UI Datepicker中重新加載數據?

我有JQuery-UI日期選擇器從jquery ajax請求傳遞數據。

$(document).ready(function() { 

$.ajax({ 
    url: "load-calendar", 
    dataType: "json", 
    success: function(calendarEvents){ 

     $("#calendar_1").datepicker({ 
      beforeShowDay: function(date) { 
       var m = date.getMonth(), d = date.getDate(), y = date.getFullYear(); 
       for (i = 0; i < calendarEvents.length; i++) { 
        if($.inArray(y + '-' + (m+1) + '-' + d,calendarEvents) != -1) { 
         return [true, 'ui-state-busy', false]; 
        } 
       } 
       return [true]; 

      } 

      }); 
    } 
}); 

}); 

它工作正常,我可以看到突出顯示繁忙日子的日曆。

現在我想autorefresh這個日曆(每分鐘),以便用戶可以自動查看新的突出顯示的日子。這可能嗎?

我試過用setInterval函數,但沒有成功。

所以任何經驗/想法都是值得歡迎的。提前致謝。

回答

0

我已經解決了這個問題:

<div id="show_calendars"></div> 

$(document).ready(function() { 

    var update = function(){ 
     $('#show_calendars') 
      .load('/reload-calendars') 
      .fadeIn("slow"); 
    }; 
    var auto_refresh = setInterval(function(){update();}, 10000); 
    update(); 

}); 

功能重載,日曆

<div id="calendar_1"></div> 

$(document).ready(function() { 

var busyDays = [<? echo $data;?>]; 
var date = new Date(); 

$("#calendar_1").datepicker({ 
    dateFormat: 'yy-mm-dd', 
    beforeShowDay: function(date) { 
     var m = date.getMonth(), d = date.getDate(), y = date.getFullYear(); 
     for (i = 0; i < busyDays.length; i++) { 
      if($.inArray(y + '-' + (m+1) + '-' + d,busyDays) != -1) { 
       return [true, 'ui-state-busy', false]; 
      } 
     } 
     return [true]; 
    } 

}); 

}); 
0

如果你的Ajax響應速度不夠快,你可以做在日期選擇器的beforeShow事件電話:

$('#calendar_1').datepicker({ 
    beforeShow: function(input, inst) { //code to update calandar events here// } 
}); 

如果使用日期選擇器內嵌:

function refreshDatePicker(){ 
    //your ajax call here 
} 

$(function(){ 
    // Document is ready 
    setInterval ("refreshDatePicker()", 1000); 
}); 
+0

感謝@Lucas的回答,但我發現事件處理程序「beforeShow」沒有調用內聯datepicker。 –

+0

啊,不知道它被內聯使用。你可以用setInterval()發佈你正在嘗試的例子嗎?我很確定這就是你想要使用的。 –