Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to import json array into jQuery calendar


Go to End


3 Posts   13058 Views

Avatar
joelg

Community Member, 134 Posts

12 July 2009 at 11:04pm

Hi

I'm trying to feed a jQuery calendar with data from Silverstripe. I've installed the perfectly well-working Calendar Event module, but all I want now is to pull out some Calendar Events and return them as a json array. Here is the code for initiating the calendar:


$(document).ready(function() {
	
		$('#calendar').fullCalendar({
			draggable: true,
			events: "json_events.php",
			eventDrop: function(event, delta) {
				alert(event.title + ' was moved ' + delta + ' days\n' +
					'(should probably update your database)');
			},
			loading: function(bool) {
				if (bool) $('#loading').show();
				else $('#loading').hide();
			}
		});
		
	});

And here is the json_events.php:


<?php

	$year = date('Y');
	$month = date('m');

	echo json_encode(array(
	
		array(
			'id' => 1,
			'title' => "Event1",
			'start' => "$year-$month-10",
			'url' => "http://yahoo.com/"
		),
		
		array(
			'id' => 2,
			'title' => "Event2",
			'start' => "$year-$month-20",
			'end' => "$year-$month-22",
			'url' => "http://yahoo.com/"
		)
	
	));

?>

The question is just, how or what should I do to get this working with silverstripe. Should I use the restfull server API? And if yes, how would I do that since I can't find any documentation on how to use the restfull server with json export.

Many thanks

Joel

Avatar
bummzack

Community Member, 904 Posts

13 July 2009 at 1:41am

Hm. Just write a method in your Controller, that returns your Data as json?
Something like:

// put this in your Page_Controller or <whatever>_Controller
public function json(){
	// gather event data to an array
	return json_encode($arrayData);
}

Then you can call the URL of that page and append /json to it.
eg. http://localhost/silverstripe/page/json (will call the json method of the page-Controller)

Avatar
joelg

Community Member, 134 Posts

13 July 2009 at 9:35pm

But of course, yes, thanks, works like a charm.