简体   繁体   中英

Filtering FullCalendar events with checkboxes (client-side with javascript)

I have a well working fullcalendar script that adds css-classes to events according to data attributes via eventRender . I now need to filter these specific attributes with checkboxes, but I have no idea at all how I should get this going!

My script looks as follows:

<script>
    $(document).ready(function() {
        $('#calendar').fullCalendar({
            eventRender: function(calEvent, element, view) {
                if (calEvent.risk == "normal") {
                    element.css('background-color', '#99FF99');
                }
                if (calEvent.risk == "event") {
                    element.css('background-color', '#415eec');
                }
                if (calEvent.risk == "whisper") {
                    element.css('background-color', '#D7CDD5');
                }
            },
            allDaySlot: true,
            displayEventTime: true,
            displayEventEnd: true,
            editable: false,
            firstDay: 1,
            weekNumbers: true,
            selectable: false,
            weekNumberCalculation: "ISO",
            eventLimit: true, 
            events: 'parts/events22.php'
        });
    });
</script>

The html checkboxes:

<div class="checkbox">
  <label><input type="checkbox" value="normal">Normal</label>
</div>
<div class="checkbox">
  <label><input type="checkbox" value="event">Event</label>
</div>
<div class="checkbox">
  <label><input type="checkbox" value="whisper">Whisper</label>
</div>

Does someone know how I can connect the checkbox-values to calEvent.risk , ie specific data attributes so as to filter them? Or does someone have a different idea for multiselect filtering with fullcalendar? I'd be really grateful for any help!

Set a change handler on checkboxes to rerender events. eventRender returning false keeps it from displaying, so make a filter function to get the checked values and return if the event.risk is in those values

  $(document).ready(function() { $('#calendar').fullCalendar({ eventRender: function(calEvent, element, view) { if (calEvent.risk == "normal") { element.css('background-color', '#99FF99'); } if (calEvent.risk == "event") { element.css('background-color', '#415eec'); } if (calEvent.risk == "whisper") { element.css('background-color', '#D7CDD5'); } return filter(calEvent); // Only show if appropriate checkbox is checked }, allDaySlot: true, displayEventTime: true, displayEventEnd: true, editable: false, firstDay: 1, weekNumbers: true, selectable: false, weekNumberCalculation: "ISO", eventLimit: true, events: 'parts/events22.php', events: [{ start: moment().add(1, 'day'), title: 'Normal', risk: 'normal' }, { start: moment().add(2, 'day'), title: 'Event', risk: 'event' }, { start: moment().add(3, 'day'), title: 'Whisper', risk: 'whisper' }] }); /* When a checkbox changes, re-render events */ $('input:checkbox.calFilter').on('change', function() { $('#calendar').fullCalendar('rerenderEvents'); }); }); function filter(calEvent) { var vals = []; $('input:checkbox.calFilter:checked').each(function() { vals.push($(this).val()); }); return vals.indexOf(calEvent.risk) !== -1; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.4.0/fullcalendar.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/2.4.0/fullcalendar.css" rel="stylesheet"/> <div class="checkbox"> <label> <input class='calFilter' type="checkbox" value="normal" checked>Normal</label> </div> <div class="checkbox"> <label> <input class='calFilter' type="checkbox" value="event">Event</label> </div> <div class="checkbox"> <label> <input class='calFilter' type="checkbox" value="whisper">Whisper</label> </div> <div id='calendar'></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM