Jump to:

23361 Posts in 18124 Topics by 2862 members

General Questions

SilverStripe Forums » General Questions » How to import json array into jQuery calendar

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

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

Page: 1
Go to End
Author Topic: 6841 Views
  • joelg
    Avatar
    Community Member
    129 Posts

    How to import json array into jQuery calendar Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How to import json array into jQuery calendar Link to this post

    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)

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: How to import json array into jQuery calendar Link to this post

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

    6841 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.