0

i have this data for the calender

var codropsEvents = {
"09-10-2013" : "Event Name 1",
"09-11-2013" : "Event Name 2"
                    };

from the data its an array of object . I want to make it dynamic so i did something like this

var codropsEvents = [];
codropsEvents.push({"09-10-2013" : "Event Name 1"});

but its not showing up the event on calender . Am i initializing the array of object in wrong way or i am pushing the data wrong .

The calender is here http://tympanus.net/codrops/2012/11/27/calendario-a-flexible-calendar-plugin/comment-page-4/

3 Answers 3

1

The calendar accepts an object literal, not an array. See http://tympanus.net/Development/Calendario/js/data.js

You said (see comment below):

for (var x = 0; x < msg.length; x++) { 
  date = msg[x].date; event_name = msg[x].event;
}

Instead:

var codropsEvents = {};
for (var x = 0; x < msg.length; x++) { 
  codropsEvents[msg[x].date] = msg[x].event;
}

You have then an object literal ready to be bound to your calendar.

http://jsfiddle.net/XmgXG/

Sign up to request clarification or add additional context in comments.

5 Comments

i am not good in javascript . I am getting the data through ajax so its a string how can i initiate the object literal so it works ok with that thanks
What does the data look like? Just a couple of items to figure out how to start.
i have date and event will be in form of string . for (var x = 0; x < msg.length; x++) { date = msg[x].date; event_name = msg[x].event; // adding date and event_name to the object literal }
got an issue with the input string . Thanks alot for your guidence :)
1

Read about arrays and objects in javascript.

//Object, { Key : value }
var codropsEventsObject = {
 "09-10-2013" : "Event Name 1",
 "09-11-2013" : "Event Name 2"
};
//Array , Also, array of objects
var codropsEventsArr = [ 
 {"09-10-2013" : "Event Name 1"},
 {"09-11-2013" : "Event Name 2"}
];

Different ways of Adding new elements to an Object

codropsEventsObject.newEle = "newValue"  
codropsEventsObject["newEle"] = "newValue"
codropsEventsObject[newEle] = "newValue"    // newEle is a js variable

Different ways of Adding new elements to an Array

codropsEventsArr.push( {newEle , "newValue"})  // newEle is a js variable
codropsEventsArr.push( {"newEle" , "newValue"})

Hope it helps

Comments

0

Something is not quite clear in your question.

According to the top of your code it seems that variable codropsEvents is an object that contains name/value pair.

While your push operation pushes a new object to the array and does not add a new value to the object.

  • So what do you want to achieve here?
  • Do you want an array of object?
  • Do want want one object with multiple values?

In order to be dynamic you will have to change your code to support an array of objects.

Or you can do the following and use one object with multiple values

var codropsEvents = [];
codropsEvents["09-10-2013"] = "Event Name 1";
codropsEvents["09-10-2013"] = "Event Name 2";

3 Comments

i am new to javascript , i thought that its an array of object . What i want is to add the data to the calender dynamically so i might have done something wrong . The top code is ok i just want to know how can i make the top code in a way that it will be dynamic .
In order to make top code dynamic you have to use the example I gave at the bottom of my answer
i did exactly same but its not showing the values on calender :@

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.