简体   繁体   English

使用JavaScript在全日历上显示/隐藏事件

[英]Show/Hide events on fullcalendar using javascript

I'm making a religious calendar for a project that shows different events for all major religions. 我正在为一个项目制作宗教日历,该项目显示所有主要宗教的不同事件。 I'm using full calendar, and I have a "religion key" at the top of the calendar, and I want to make it so that when I click, for example, Jewish, it will hide all religious events/holidays on the calendar that don't pertain to the Jewish faith. 我使用的是完整日历,并且在日历的顶部有一个“宗教钥匙”,因此我要使其保留,以便在单击“犹太人”时将其隐藏在日历上的所有宗教活动/节日与犹太信仰无关的东西。 I'm having some trouble. 我有麻烦了 My thinking was that I would set the ".fc-event" class in css to display:none" and then have a class called .show that would be display:block. and toggle that class / remove that class when the user clicks the menu item that pertains to the holiday. Is there a better / easier way to do this? would love some guidance/tips/help :) thank you. 我的想法是,我将在CSS中将“ .fc-event”类设置为display:none,然后有一个名为.show的类,该类将为display:block。当用户单击该类时,切换该类/删除该类。与假期有关的菜单项,是否有更好/更轻松的方法来完成此操作?需要一些指导/提示/帮助:)谢谢。

Here is my code for the calendar js (using made up dates obviously): 这是我的日历js代码(显然是使用补全日期):

events: [
        {
            title: 'xmas',
            start: '2017-03-19',
            className: 'catholic show',
            description: 'blablablablabla'
        },
        {
            title: 'Hanukkah',
            start: '2017-03-19',
            className: 'jewish show',
            description: 'blablablabla'
        },
        {
            title: 'Hindu Holiday',
            start: '2017-03-24',
            end: '2017-03-27',
            className: 'hindu show',
            description: 'blablablabla'
        }
    ]
});

$('.calendar-nav a').each(function() {

    $(this).on('click', function(e) {
        e.preventDefault();

        var x = $(this).attr('rel');

        $('.fc-event-container').each(function() {

            $(this).removeClass('show');

            if($(this).hasClass(x)) {

                $(this).addClass('show');
            }
        });
    })
});

$('.show-all a').on('click', function() {
    $('.fc-event-container').each(function() {
        if(!$(this).hasClass('show')) {
            $(this).toggleClass('show');
        }
    });
});
});

And the php code for the calendar and calendar-key: 日历和日历键的php代码:

<nav class="calendar-nav">
<ul class="menu">
    <li class="christianity-text"><a href="#" rel="christian">><img src="/images/calendar/christianity.png" />CHRISTIAN</a>
        <ul class="sub-menu">
            <li><a href="#">Catholic</a></li>
            <li><a href="#">Protestant</a></li>
            <li><a href="#">Coptic</a></li>
            <li><a href="#">Mormon</a></li>
            <li><a href="#">Neo</a></li>
            <li><a href="#">Anglican</a></li>
            <li><a href="#">Celtic</a></li>
            <li><a href="#">Jehova's Witness</a></li>
        </ul>
    </li>
    <li class="buddhist-text"><a  href="#" rel="buddhist"><img src="/images/calendar/buddhism.png" />BUDDHIST</a></li>
    <li class="hindu-text"><a  href="#" rel="hindu"><img src="/images/calendar/hindu.png" />HINDU</a></li>
    <li class="jewish-text"><a  href="#" rel="jewish"><img src="/images/calendar/jewish.png" />JEWISH</a></li>
    <li class="islam-text"><a  href="#" rel="islam"><img src="/images/calendar/islam.png" />ISLAM</a></li>
    <li class="sikh-text"><a  href="#" rel="sikh"><img src="/images/calendar/sikh.png" />SIKH</a></li>

    <li class="pagan-text"><a  href="#" rel="pagan"><img src="/images/calendar/pagan.png" />PAGAN</a></li>
    <li class="wiccan-text"><a  href="#" rel="wiccan"><img src="/images/calendar/wiccan.png" />WICCAN</a></li>
    <li class="other-text"><a  href="#" rel="other"><img src="/images/calendar/other.png" />OTHER</a></li>
    <li class="all-text"><a href="#" rel="all">ALL</a></li>
</ul>
</nav>

<div id='calendar'></div>

You can do the following, 您可以执行以下操作


A. You can add/remove a class that has display:none and display:block 答:您可以添加/删除具有display:nonedisplay:block

$(this).addClass('hideClass'); and $(this).addClass('showClass'); $(this).addClass('showClass');

css CSS

hideClass
{ display:none; }

showClass
{ display:block; }

B. you can toggle hide and show B.您可以切换隐藏和显示

$(this).hide(); and $(this).show(); $(this).show();


C. You can change the css property itself via using css variables . C.您可以使用css变量来更改css属性本身。 This is the better option, especially when you are redrawing your elements asynchronously.. the changes with the css stay. 这是一个更好的选择,尤其是当您异步重绘元素时。.使用CSS保留更改。

be warned that chrome does not support this though, which leads us to... 请注意,chrome不支持此功能,这导致我们...


D. Appending CSS files to override class D.附加CSS文件以覆盖类

you can add a .css file with classes that have !important to override your current settings 你可以添加.css文件与上课!important覆盖您当前的设置

$("head").append("<link id='yournewcss' href='hi-res.css' type='text/css' rel='stylesheet' />");

This is to ensure that if you redraw the page asynchronously, the property stays. 这是为了确保如果异步重绘页面,该属性将保留。

Think this sort of thing is a lot easier to manage with CSS and a tiny bit of JS. 认为使用CSS和少量JS可以更轻松地管理这种事情。 The example below is a sort of minimal use case version but the idea is the same. 下面的示例是一种最小用例版本,但是想法是相同的。

 /* Listen for clicks on the nav. Use each <a>'s "data-show" property to determine which class to add to the #calendar. That class, along with the CSS will determine which to show (hide, actually). By default, all events are shown. Only when the #calendar element gets a class associated with our CSS does anything change. Since we're using the :not() selector to _hide_ everything else (instead of showing something specific) there's no need for any 'show all'. Once #calendar has 'show-all', the default CSS comes through. */ $('.calendar-nav').on('click', 'a', function(evt) { var showType = $(this).data('show'); var $calendar = $('#calendar'); if(showType !== undefined) { $calendar.attr('class', '').attr('class', 'show-' + showType); } }); 
 .show-hindu li:not(.hindu), .show-pagan li:not(.pagan) /* ... continue ... */ { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav class="calendar-nav"> <a href="#" data-show="hindu">Hindu</a> <a href="#" data-show="pagan">Pagan</a> <a href="#" data-show="all">All</a> </nav> <div id="calendar"> <ul class="events"> <li class="hindu">Hindu 1</li> <li class="pagan">Pagan 1</li> <li class="hindu">Hindu 2</li> <li class="pagan">Pagan 2</li> <li class="hindu">Hindu 3</li> <li class="pagan">Pagan 3</li> </ul> </div> 

Another solution could be add a custom attribute 'religion' to events and save it to the database. 另一种解决方案是向事件添加自定义属性“宗教”并将其保存到数据库。 Fetch the events from the database which has the religion that you want to show: 从具有您要显示的宗教信仰的数据库中获取事件:

JavaScript: JavaScript的:

...
$.ajax({
        url: 'process.php',
        type: 'POST',
        data: 'religion=1",
        async: false,
        success: function(response){
            json_events = response;

        }
});
$(#calendar).fullCalendar({
    events: JSON.parse(JSON.stringify(json_events)),

});     

process.php process.php

$religion = $_POST['religion'];
$query = "SELECT * FROM calendar WHERE religion='$religion'";
...

I want to give you an advise ! 我想给你一个建议! I prefer doing with javascript logic .You have your event object so you can recreate object that your needed data 我更喜欢使用javascript逻辑。您拥有事件对象,因此您可以重新创建需要数据的对象

$(document).ready(function(){
var events = [
    {
        title: 'xmas',
        start: '2017-03-19',
        className: 'catholic show',
        description: 'blablablablabla'
    },
    {
        title: 'Hanukkah',
        start: '2017-03-19',
        className: 'jewish show',
        description: 'blablablabla'
    },
    {
        title: 'Hindu Holiday',
        start: '2017-03-24',
        end: '2017-03-27',
        className: 'hindu show',
        description: 'blablablabla'
    }
];
//init calendar 
$("#calendar").fullCalendar({
  events : events
});

$('.calendar-nav a').click(function(e) {    
        e.preventDefault();    
        var x = $(this).attr('rel');
        //if all, shows original events object
        if(x == "all") {
          newEvent = events;
        } else{
        var newEvent = checkEvent(x,events); //recreate object function
        }
        $('#calendar').fullCalendar('destroy');
        $("#calendar").fullCalendar({
          events : newEvent
        });
});
});

function checkEvent(x,events) {  
  var newEvent = [];
  for(var i in events) {
    var key = events[i].className.split(" ")[0];
    if(key == x) { //check same religious name
      newEvent.push(events[i]);
    }
  }
  return newEvent;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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