2011-11-16 104 views
0

我是JavaScript和jQuery的新手,所以如果這是一個簡單的問題,請和我一起袒護。我嘗試了這裏以及谷歌在多個JavaScript網站的搜索功能,但我似乎無法得到解決我的問題的答案(並且我會理解;-))如何將變量的內容傳遞給jquery函數?

我想構建一個顯示已存儲在數據庫中的事件的日曆。要做到這一點,我環通在我的劇本的開頭的一組記錄,並建立一個字符串,最終包含此變量vEvents

{ 
    title: 'new appointment', 
    start: '12-OCT-2011 14:00' 
}, 
{ 
    title: 'next appointment', 
    start: '12-OCT-2011 15:00' 
} 

等。因此,每個事件都是通過駐留在數據庫中的信息構建的,最終的事件條目數(兩個大括號之間)未知,並且可以爲每個日曆調用動態更改。

我使用一個jQuery日曆插件(http://arshaw.com/fullcalendar)它建立使用jQuery的調用是這樣的日曆:

$('#calendar').fullCalendar({ 
      header: { 
       left: 'prev,next today', 
       center: 'title', 
       right: 'month,agendaWeek,agendaDay', 
      }, 
      editable: false, 
      height: 370, 
      firstDay: 1, 
      weekMode: 'liquid', 
      minTime: 8, 
      maxTime: 22, 
      weekends: false, 
      //and now on to the event creation 
      events: [ 
       { 
       title: 'this is a standard event', 
       start: '11-OCT-2011 16:00' 
       }, 
       { 
       title: ' yet another demonstration event', 
       start: '11-OCT-2011 17:00' 
       }] 
}); 

我想做的是由先前建立的變量的內容在通話前衛括號[]之間更換任何東西,所以像

//and now on to the event creation 
events: [ 
    $(vEvents) // or some other unknown syntax, or an evaluation function etc. that 
       // replaces the varibale with it's content at that place 
] 

我一直沒能得到那個置換完成後,我嘗試了eval()功能一些其他的方法,但隨後jQuery總是co關於不恰當的語法的草稿。那麼這是可能做到整體,如果積極,我該如何做到這一點?如果有人能給我適當的語法,我會非常感激!

非常感謝

斯特凡

回答

3

看起來好像fullCalendar插件需要對象的數組。所以,如果vEvents看起來是這樣的:

var vEvents = [//this bracket starts an array 
    {//this bracket starts an object 
     title: 'new appointment',//this line defines the `title` property of the object it's in 
     start: '12-OCT-2011 14:00'//this line defines the `start` property of the object it's in 
    },//this ends the current object and the comma separates the two objects as items in an array 
    { 
     title: 'next appointment', 
     start: '12-OCT-2011 15:00' 
    } 
    //note that there is not a comma here, it's important that commas are only between objects and there is no trailing comma at the end of the list of objects 
];//this bracket ends the array 

那麼你應該能夠只是events選項設置爲vEvents變量是這樣的:

$('#calendar').fullCalendar({ 
    ... 
    //and now on to the event creation 
    events: vEvents 
}); 

--UPDATE--

//first, create an empty array 
var myEvents = []; 

//then iterate through your data 
for (var rowIx = 0; rowIx < this.Data.Rows.length; rowIx++) { 

    //and add an object, notice the `{` and `}` (brackets) that note the beginning and ending of an object definition 
    myEvents.push({title : this.Data.Rows[rowIx][2].text, start : this.Data.Rows[rowIx][5].text;); 
} 

.push()將一個值添加到數組的末尾,所以我們在這裏我們.push()將一個對象放到數組上。 title : this.Data.Rows[rowIx][2].texttitle屬性添加到新對象並將您想要的值映射到該屬性。

+0

他正在構建** string **變量。它需要首先被json解析。 –

+0

我在關於構建對象數組的回答中添加了一些註釋。如果他自己構建數據,那麼他可能會馬上正確地完成數據,而不是單向構建數據,然後使用其他語言來修復數據。 – Jasper

+0

謝謝Jasper,但是如何讓數組動態構建?這就是爲什麼我用一個字符串來「接近」,直到對記錄的循環結束爲止......再說一遍,我是JavaScript新手,所以我的問題看起來很愚蠢,我知道:-( 因此,例如一個var myEvents: 標題:this.Data.Rows [rowIx] [2]的.text,\t 開始:this.Data.Rows [rowIx] [5]的.text ]; 似乎不工作 – Stefan

0

這是您以前建立的變量。這是一個數組,用方括號表示。

var myEvents = [ 
      { 
      title: 'this is a standard event', 
      start: '11-OCT-2011 16:00' 
      }, 
      { 
      title: ' yet another demonstration event', 
      start: '11-OCT-2011 17:00' 
      }]; 

這是您的日曆設置。

$('#calendar').fullCalendar({ 
     header: { 
      left: 'prev,next today', 
      center: 'title', 
      right: 'month,agendaWeek,agendaDay', 
     }, 
     editable: false, 
     height: 370, 
     firstDay: 1, 
     weekMode: 'liquid', 
     minTime: 8, 
     maxTime: 22, 
     weekends: false, 
     events: myEvents 
}); 
0

要在我的劇本的開頭做到這一點我環路直通的一組記錄,並在可變vEvents

而不是建立一個字符串建立一個字符串,你可以建立一個JavaScript目的?

如果不是,則生成一個有效的JSON格式化字符串,然後使用jQuery.parseJSON()從中獲取JavaScript對象。請注意,vEvents應該是一組對象。

就用

$('#calendar').fullCalendar({ 
    events: vEvents 
}); 
0

一個簡單的變量是所有你需要在那裏。因此,爲了測試它第一次做這個

var vEvents = [ 
       { 
       title: 'this is a standard event', 
       start: '11-OCT-2011 16:00' 
       }, 
       { 
       title: ' yet another demonstration event', 
       start: '11-OCT-2011 17:00' 
       } 
       ]; 

// Calendar code 
    ... 
    maxTime: 22, 
    weekends: false, 
    //and now on to the event creation 
    events: vEvents }); 

一旦你知道的作品,那麼你可以與你通過簡單地將其添加到陣列從數據庫中讀取的數據動態填充vEvents陣列。

+0

他正在構建** string **變量。它需要首先被json解析。 –