Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Array (object)?

    Hi,

    I am trying to create an array to pass to JQuery, but after several hours of trying, am falling over. I am trying to use Ajax to populate the javascript FullCalendar.

    I am trying to pass an arry to the following property:

    This works:

    $('#calendar').fullCalendar({
    events: [{"id":1112,"title":"Event1","start":"2009-10-10"},{"id":222,"title":"Event2","start":"2009-10-20","end":"2009-10-22"}]
    };

    something like:

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

    I will be using AJAX to populate MyJSArray().

    I have tried the likes of: MyJSArray = new array("id"=>112,"title"=>"Event1", etc);
    But no joy

    I don't know how to get a javascript array into that format. I have my database values, but I need to put them into a format that the "Event" will accept.


    I have used PHP and arrays fine, but coming to grips with JS arrays,etc are a bit finiky at the moment.

    I am a little stuck, any help or points in the right direction will be much appreciated.

    Thanks Blueevo.

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    well you line that says that works will actually not work since you are missing a )

    The format is nothing special it is just JSON. Since you are using jQuery, you should be using getJSON() to get the data from the server. There should be plenty of examples out there in php on how to return JSON.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your reply.

    Sorry about the missing ")", I had to trim the code down a little to make is simple enough for someone to help me.

    I am very new to this level of JS really. I am not using PHP (or any server side scripts for that matter).

    The calendar is an open source calendar that I found, but I am struggling to populate the "hard coded" data with dynamic data from my database.

    I can query the data and get my data back, I just need to format it into a way the JQuery will understand.

    For example, their only example is how to get google calendar events, like this:

    Code:
    events: $.fullCalendar.gcalFeed('http://www.google.com/calendar/feeds/uk__en%40holiday.calendar.google.com/public/basic'),
    I would like to be able to populate "events" with my data from the DB, but I am unsure on how to create an object place after events.

    I have been trying for many hours and after not getting anywhere, I have resolved to forum advice. I am not asking someone to do it for me, just a little help?

    I will further investigate JSON to see what I can comeup with. Meanwhile, if you or anyone else has any other advice, it would be much appreciated.

    Thanks,
    Blueevo.

  • #4
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Getting closer:
    Code:
    var applicationList = [];
    // schedule_resources is my data from database.
    for(var i in schedule_resources){
        applicationList[i] = [];
        applicationList[i]['id'] = i;
        applicationList[i]['title'] = schedule_resources[i].Subject;
        applicationList[i]['start'] = schedule_resources[i].Due_Date;
    }
    
    $('#calendar').fullCalendar({
        events: applicationList
    });
    This seems to have resolved my issue! Thanks very much for pointing me in the right direction. If anyone has any comments about my code, please let me know

  • #5
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Is it really hard for you to build a php page that returns a string on the server that looks like:

    Code:
    { myEvents : [{"id":1112,"title":"Event1","start":"2009-10-10"},{"id":222,"title":"Event2","start":"2009-10-20","end":"2009-10-22"}] }
    than on the clientside you just call that php page like this:

    Code:
    $.getJSON("myDates.php",
            function(data){
                $('#calendar').fullCalendar({
                    events: data.myEvents
                });
            }
    );
    There is nothing really hard to it at all. I think you are over complicating it in your head!

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #6
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi A1ien51,

    I am not using PHP and cannot use PHP in this environment. Believe me, I would much rather use PHP than JS for looping and object use,etc.

    I think I may have solved my issue anyway.

    P.S. Things are always complicated when you do not understand them

    I wouldn't have done it without your nudge in the right direction - cheers.

  • #7
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Looking at their docs, you can also just do

    Code:
    events: function(start, end, callback) {
            // do some asynchronous ajax
            $.getJSON("myDates.php",
                    {
                            start: start.getTime(),
                            end: end.getTime()
                    },
                    function(result) {
                            callback(result.myEvents);
                    });
    }
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #8
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    I thought you said "I have used PHP and arrays fine" so it made it sound like you were doing it with PHP and I got Ajax in the picture with it too. I am not sure why you are not outputting it into the format you need to begin with instead of looping through with JavaScript which will slow down your page if you have a large amount of dates to handle.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #9
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is the bit I am working on now!

    I just fed all my dates in to get it working.

    Now this is the bit I need to work with my Javascript array (as I am not using external files).

    I'll try to create a function which returns my data based on the start and end date.

    Thanks.

  • #10
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, my mistake for not being clear.

    I have had experience with PHP using objects and arrays.

    I am currently working on a JS only application, where I cannot use any external files or environments.

    The reason why I am looping is becuase it is my way of learning. As time goes by, I'll learn how to put the data into an object when it is called from the DB. I'll start with the simple things, before I get more advanced with this

    Just getting to grips with the JS syntax, etc is a bit rusty as I am in and out of PHP and VB development.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •