1

I'm currently working on a fullcalendar (v3) project; I'm new to this library and also a noob in Javascript, just know basic stuff.

First at all: to access my calendar I implemented a login session (so when u access the calendar the page has a $_SESSION['user'], where user is saved as 'nomeUtente') and my calendar fecth events from a database with this details details

nomeUtente (in this case 'dip7') it's a variable saved that coincide with the $_SESSION['nomeUtente'] at the moment someone is logged in and save a new Events

I also have two checkboxes (orePersonali and Assenze) checkboxes (the actual $_SESSION['nomeUtente'] is dip5)

This is their code:

    <input type="checkbox" id="OP" name="calendario" value="OP" checked>
    <input type="checkbox" id="assenze" name="calendario" value="assenze">

At the moment both of the checkboxes hide and show every events, throught this function:

    $('#calendar').fullCalendar('render');
                function eventsHidden(context){
                    let x = $(".fc-event-container");                   
                    if (context.prop("checked") ) { 
                      x.css({
                        visibility: "visible"
                      });
                    } else {
                      x.css({
                        visibility: "hidden"
                      });
                    } 
                  };                  
                  function eventsHiddenA(context){ 
                    let x = $(".fc-event-container");                   
                    if (context.prop("checked")) { 
                      x.css({
                        visibility: "visible"
                      });
                    } else {
                      x.css({
                        visibility: "hidden"
                      });
                    } 
                  };

                  $("#OP").on("change", function () {
                    eventsHidden($(this))
                  });
                  $("#assenze").on("change", function () {
                    eventsHiddenA($(this))
                  });

Recalled in the fullcalendar section by dayRender:

dayRender: function(view, element,render, cell) {
                            render = !render ? (
                                false
                            ) : true
                            setTimeout(() => {
                                eventsHidden($("#OP"))
                                eventsHiddenA($("#assenze"))                                
                                render = false
                            }, 0)
                        }

Want i would like to do is: when "Assenze" is unchecked to hide all events that have a 'nomeUtente' != from $_SESSION['nomeUtente'], basically to the user who's logged at the moment (in the case of the previusly screen 'dip5')

aim0d
  • 129
  • 7
  • https://stackoverflow.com/a/29993265/5947043 – ADyson Mar 24 '22 at 12:27
  • okay ? I'm trying to applied to my project but kinda difficult, i dont have a selector as this guy project and i dont get how i should connect my checkboxes – aim0d Mar 24 '22 at 12:49
  • Well you can test against all of the checked checkboxes individually then – ADyson Mar 24 '22 at 12:50
  • okay, and then? i need for both of them. Also it's saying that 'all' inside the return of the eventRender function in `it is not a non-null object` – aim0d Mar 24 '22 at 12:53
  • i appreciate that u look for an answer and send it, but i repeat, i'm a noob and the documentation i looked for (using the asnwer as starting point) doesnt help fpr what i'm looking for – aim0d Mar 24 '22 at 12:54
  • Also my "value" is not a number like the case u linked, but a string, i don't know how it can be related to mine :( – aim0d Mar 24 '22 at 13:00
  • The fact it's a number and not a string is irrelevant - it's doing a simple value comparison which will work in either case. Don't get distracted by issues you've assumed or guessed will exist (with no evidence, as far as I can see) rather than ones which do exist! The main change you need is just to read from a list of checkboxes, rather than a single dropdown. I posted a solution below. – ADyson Mar 24 '22 at 13:05

1 Answers1

0

You can use the answer at https://stackoverflow.com/a/29993265/5947043 and adapt it very slightly just to read the values from the checked checkboxes instead of from a dropdown list:

var currentUser = "dip5"; //hard-coded for demo. For PHP use, replace with var currentUser = '<?php echo $_SESSION["nomeUtente"]; ?>';

$('#mycalendar').fullCalendar({
    defaultDate: "2015-05-01",
    events: [
        {
            title: 'Event 1',
            start: '2015-05-01',
            nomeUtente: 'dip7'
        },
        {
            title: 'Event 2',
            start: '2015-05-02',
            nomeUtente: 'dip5'
        },
        {
            title: 'Event 3',
            start: '2015-05-03',
            nomeUtente: 'dip7'
        },
        {
            title: 'Event 4',
            start: '2015-05-04',
            nomeUtente: 'dip5'
        },
        {
            title: 'Event 5',
            start: '2015-06-04',
            nomeUtente: 'dip7'
        },
        {
            title: 'Event 6',
            start: '2015-06-05',
            nomeUtente: 'dip5'
        }        
    ],
    eventRender: function eventRender( event, element, view ) {
        //get the values from all selected checkboxes
        var selections = [];
        $("input[name=calendario]:checked").each(function(){
            selections.push($(this).val());
        });
        
        var showEvent = false;
        if (selections.indexOf("OP") >= 0 && event.nomeUtente == currentUser) showEvent = true; //show if the OP box is ticked and the event belongs to the current user
        if (selections.indexOf("assenze") >= 0 && event.nomeUtente != currentUser) showEvent = true; //show if the assenze box is ticked and the event belongs to another user
        
        return showEvent;
    }
});

$('input[name=calendario]').on('change',function(){
    $('#mycalendar').fullCalendar('rerenderEvents');
})
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js"></script>
<script src="http://fullcalendar.io/js/fullcalendar-2.3.1/fullcalendar.min.js"></script>
<link rel="stylesheet" href="http://fullcalendar.io/js/fullcalendar-2.3.1/fullcalendar.min.css" />

OP<input type="checkbox" id="OP" name="calendario" value="OP" checked>
assenze<input type="checkbox" id="assenze" name="calendario" value="assenze">

<div id="mycalendar"></div>

N.B. This will work on fullCalendar versions 2 and 3. It would need adapting again for newer versions.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Thank you! I'm gonna try and see how it works on my project. My fullcalendar version is actually 3, because i'm using a template and cannot change it, so it's perfect – aim0d Mar 24 '22 at 13:08
  • It doesnt render my events, like i dont get errors, but my events are disappeared and doesnt change anything if i check or unchecked the checkboxes – aim0d Mar 24 '22 at 13:20
  • also, maybe i wasnt clear in my question but my events has nomeUtente who's basically a foreign key of the login database, not the "op" or "assenze" value – aim0d Mar 24 '22 at 13:22
  • You must have done something different than what I did then, because as you can see from my runnable demo, my code works. I can't see what you did exactly. Update your question with the latest version of your code, incorporating this solution – ADyson Mar 24 '22 at 13:22
  • `my events has nomeUtente who's basically a foreign key of the login database, not the "op" or "assenze" value`...er, in your screenhot, your event clearly has a property called "nomeUtente". Its value is dip7. So if you want to be able to filter by that, you'll need to have a "dip7" option in one of your checkboxes. – ADyson Mar 24 '22 at 13:23
  • Or maybe I misunderstood - what are the OP and assenze checkboxes supposed to do, if they are not filtering on the nomeUtente value? – ADyson Mar 24 '22 at 13:24
  • Yes, but i add that property to the events because it shows who created the event, the person who was logged in at the moment the event was created – aim0d Mar 24 '22 at 13:26
  • Ok. Isn't that what you want to filter by? – ADyson Mar 24 '22 at 13:27
  • Assenze should turn on and off events that have the property 'nomeUtente' different from user that it's logged in – aim0d Mar 24 '22 at 13:28
  • it's a multiuser calendar, and every user should be able to see his own events and others, but also to hide others – aim0d Mar 24 '22 at 13:29
  • Ah ok I see. Perhaps because it's not in English I didn't know what the word meant, and therefore didn't know what the checkbox was for. – ADyson Mar 24 '22 at 13:30
  • sorry, but english is not my first language so it's kinda difficult to explain clearly myself – aim0d Mar 24 '22 at 13:31
  • `it's not in English` it's obviusly not your fault, so no problem and sorry if i wasnt very clear – aim0d Mar 24 '22 at 13:31
  • ok I think I understand now - I have rewritten the code in the answer, please look at it and try it. – ADyson Mar 24 '22 at 13:35
  • It's a little bit buggy, like to show the current user events i need to change months, probably doesnt render but its a big step, i was stuck and desperate – aim0d Mar 24 '22 at 13:48
  • You don't need to change months in my version to make that work, so I don't know if you have some other code in your environment which is still interfering with the display of events? Glad it's helped you though. – ADyson Mar 24 '22 at 14:04
  • 1
    it was so simple; my fullcalendar id in HTML is not "mycalendar" but just "calendar". So simple i wasnt looking at it – aim0d Mar 24 '22 at 14:35
  • Ah yes, of course my code is just an example, and does not reflect your own fullCalendar code (mainly because I can't see that). Glad you fixed it :-) – ADyson Mar 24 '22 at 14:40