简体   繁体   中英

getting the event from variable

im studing the jquery calendar from github: https://github.com/themouette/jquery-week-calendar

I pull the data from database by using code behind C#, and stored it at hiddenfield, then javascript read the hiddenfield field value as string.

i will skip how i get the value from database, in this question, i hardcode the events into sampleEvents var.

Working Javascript:

 var eventData = {
      events: [
{'id':1, 'start': new Date(2015, 3, 27, 12), 'end': new Date(2015, 3, 27, 13, 35),'title':'Lunch with Mike'},
{'id':2, 'start': new Date(2015, 3, 28, 10), 'end': new Date(2015, 3, 28, 14, 45),'title':'Dev Meeting'}
]};

Not Working Javascript:

var sampleEvents = "{'id':1, 'start': new Date(2015, 3, 27, 12), 'end': new Date(2015, 3, 27, 13, 35),'title':'Lunch with Mike'},{'id':2, 'start': new Date(2015, 3, 28, 10), 'end': new Date(2015, 3, 28, 14, 45),'title':'Dev Meeting'}";

 var eventData = {
      events: [
         sampleEvents
]};

Error message:

Uncaught TypeError: Cannot read property 'getTime' of undefined

Not Working Javascript 2:

var sampleEvents = "[{'id': 1,'start': new Date(2015, 3, 27, 12),'end': new Date(2015, 3, 27, 13, 35),'title': 'Lunch with Mike'},{'id': 2,'start': new Date(2015, 3, 28, 10),'end': new Date(2015, 3, 28, 14, 45),'title': 'Dev Meeting'}]";

var array = JSON.parse(sampleEvents);

var eventData = {
       events: 
           sampleEvents
};

Error Messsage:

Uncaught SyntaxError: Unexpected token '

Can anyone tell me, what did i missed?

Because your created sampleEvents is string not array.

event is accepting array not a string .

To use array try this:

var sampleEvents = [
    {
        'id':1, 
        'start': new Date(2015, 3, 27, 12), 
        'end': new Date(2015, 3, 27, 13, 35),
        'title':'Lunch with Mike'
    },
    {
        'id':2, 
        'start': new Date(2015, 3, 28, 10), 
        'end': new Date(2015, 3, 28, 14, 45),
        'title':'Dev Meeting'
    }
];

To Use String try this:

var sampleEvents = '['+
    '{"id":1, "start": "'+new Date(2015, 3, 27, 12)+'", "end": "'+new Date(2015, 3, 27, 13, 35)+'","title":"Lunch with Mike"},'+
    '{"id":2, "start": "'+new Date(2015, 3, 28, 10)+'", "end": "'+new Date(2015, 3, 28, 14, 45)+'","title":"Dev Meeting"}'+
    ']';

var sampleEventsArray = JSON.parse(sampleEvents);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM