1

I'm trying to make a dynamic calendar with jQuery Fullcalendar, I want to display a different title on the events if they are on month, week or day view. I'm, trying with addEventSource and removeEventSource, but I can't get the event to get render. This is my code:

<script type='text/javascript'>

$(document).ready(function() {

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();
    
    
daySource = new Object();       
daySource.title = 'MONTH'; // this should be string
daySource.start = new Date(y, m, d); // this should be date object
daySource.end = new Date(y, m, d);

var day =new Array();
day[0]= daySource;

monthSource = new Object();       
monthSource.title = 'MONTH'; // this should be string
monthSource.start = new Date(y, m, d); // this should be date object
monthSource.end = new Date(y, m, d);

var month =new Array();
month[0]= monthSource;

$('#calendar').fullCalendar({
            header: {
                left: 'prev,next today',
                center: 'title',
                right: 'month,agendaWeek,agendaDay'
            },
            columnFormat: {
                month: 'ddd',
                week: 'ddd d/M',
                day: 'dddd d/M'
            },          
            defaultView: 'agendaDay',     

  




        viewDisplay: function(view) {
            if (lastView == undefined) { lastView = 'firstRun';  }

            if (view.name != lastView ) {

            if (view.name == 'agendaWeek') { 
                $('#calendar').fullCalendar( 'addEventSource', month ); 
                $('#calendar').fullCalendar( 'removeEventSource', day ); 
                $('#calendar').fullCalendar( 'removeEventSource', day ); 
                $('#calendar').fullCalendar('renderEvents');
            }
            if (view.name == 'agendaDay') { 
                $('#calendar').fullCalendar( 'addEventSource', day ); 
                $('#calendar').fullCalendar( 'removeEventSource', month ); 
                $('#calendar').fullCalendar( 'removeEventSource', month ); 
                $('#calendar').fullCalendar('renderEvents');
            }

            if (view.name == 'month') { 
                $('#calendar').fullCalendar( 'addEventSource', month ); 
                $('#calendar').fullCalendar( 'removeEventSource', day ); 
                $('#calendar').fullCalendar( 'removeEventSource', day );
                $('#calendar').fullCalendar('renderEvents'); 
            }
            lastView = view.name;
            }
        },

        timeFormat: { // for event elements
            agendaDay: '',
            agendaWeek: '',
            month: '',
            '': 'h(:mm)t' // default
        },          

    });  $('#calendar').fullCalendar( 'addEventSource', month );        $('#calendar').fullCalendar('rerenderEvents'); 
});

First I don't know why the avents doesn't render, and then if the title will change depending on the view. I'll appreciate any help.

5
  • The eventSources need to be arrays, not just event objects. Encapsulate your object in arrays and see if that works.
    – ganeshk
    Commented Feb 21, 2013 at 19:04
  • I updated the code, but still doesnt show me any event on the calendar Commented Feb 21, 2013 at 20:12
  • Works for me - jsfiddle.net/100thGear/CBPW8/1
    – ganeshk
    Commented Feb 21, 2013 at 22:04
  • thanks, i copied your code and works for me too, i dont know why it didnt earlyer, but still have a problem. each time i switch between nonth and week it duplicates the event Commented Feb 22, 2013 at 0:11
  • Cool - I added this as an answer, if you want to accept that and close this question.
    – ganeshk
    Commented Feb 22, 2013 at 0:20

1 Answer 1

2

The eventSources need to be arrays, not just event objects. Encapsulate your object in arrays and see if that works.

Check this fiddle for a demo - jsfiddle.net/100thGear/CBPW8/1

3
  • thanks, but the code still have a problem. When you switch between month and week view, it duplicates the event titled "MONTH" Commented Feb 22, 2013 at 0:32
  • Yes, because you are adding MONTH twice. Once in your 'agendaWeek' block and again in your 'month' block. Remove both month and day sources from the 'month' block before you add anything in there.
    – ganeshk
    Commented Feb 22, 2013 at 3:13
  • i did it that way because i read in an other post that some guy did it that way and it worked for him. I did the change you told me and it worked, thank you. Commented Feb 22, 2013 at 3:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.