2017-08-13 49 views
0

我有一個listeJNO變量,我需要在beforeShowDay功能

$('#calendrier').datepicker({ 

       format: "dd/mm/yyyy", 
       weekStart: 1, 
       maxViewMode: 3, 
       language: "fr", 
       calendarWeeks: true, 
       beforeShowDay: function (date) { 

        for (var i = 0; i < listeJNO.length; i++) { 
         console.log(date >= listeJNO[i].Begin && date <= listeJNO[i].End); 
         if (date >= listeJNO[i].Begin && date <= listeJNO[i].End) { 

          return { 

           classes: 'morning-ferie' 
          }; 
         } 
        } 
       } 

但在此之前,我需要定義listeJNO。我有一個這樣做的功能,實際上它是getListeJNO()。因此,這裏是我的問題,只要我把getListeJNO()listeJNO初始化數據,該$('calendrier').datepicker({...})beforeShowDay選項總是先行其中listeJNO有內部

回答

0

一個選項是當listeJNO的負載完成時初始化datepicker。 您可以通過返回承諾並在「然後」回調中設置日期選擇器來完成此操作。

首先創建該數據加載到listeJNO

var listeJNO; 
var loadListeJNOpromise = new Promise(
    function (resolve, reject) { 
      //load the data into listeJNO, then send it in the resolve callback 
      listeJNO = ....; 

      resolve('Load completed'); // fulfilled 

      //if soomething went wrong then reject it 
      //reject('Didnt work..'); 
     } 
    ); 

承諾然後消耗的承諾,因爲這

//create a function thats sets the datepicker 
var initializeDatepicker = function(){ 
    $('#calendrier').datepicker({ 
     format: "dd/mm/yyyy", 
     weekStart: 1, 
     maxViewMode: 3, 
     language: "fr", 
     calendarWeeks: true, 
     beforeShowDay: function (date) { 

      for (var i = 0; i < listeJNO.length; i++) { 
       console.log(date >= listeJNO[i].Begin && date <= listeJNO[i].End); 
       if (date >= listeJNO[i].Begin && date <= listeJNO[i].End) { 

        return { 

         classes: 'morning-ferie' 
        }; 
       } 
      } 
     } 
}; 

//declare a function which invokes the datepicker function in the promise 
var initialize = function() { 
    loadListeJNOpromise 
     .then(function (fulfilled) { 
      console.log(fulfilled); 
      //call the datepicker initialize here 
      initializeDatepicker(); 
     }) 
     .catch(function (error) { 
      console.log(error); 
     }); 
}; 
//invoke the initilize 
initialize(); 
+1

非常感謝,它的工作......並且,謝謝你讓我知道「承諾」 – kevricher

0

沒有數據你嘗試並設置beforeShowDay裏面的變量?我會認爲這會起作用。將listeJNO var聲明在beforeShowDay函數之外,然後將其設置在該函數內。像這樣:

beforeShowDay: function (date) { 
       //Set your variable in this function 
       listeJNO = getListeJNO(); 
       for (var i = 0; i < listeJNO.length; i++) { 
        console.log(date >= listeJNO[i].Begin && date <= listeJNO[i].End); 
        if (date >= listeJNO[i].Begin && date <= listeJNO[i].End) { 

         return { 

          classes: 'morning-ferie' 
         }; 
        } 
       } 
      } 
+0

問題是listeJNO將每次加載的一天即將展現在日期選擇器 – kevricher

相關問題