【问题标题】:Jquery Full Calendar Questions - Color on group of events & multiple event sourcesJquery Full Calendar Questions - 事件组和多个事件源的颜色
【发布时间】:2011-08-01 20:31:51
【问题描述】:

我正在查看 jquery full calendar 1.5 并且有几个问题。

多源会是什么样子?

jQuery $.ajax options

You can also specify any of the jQuery $.ajax options within the same object! This allows you to easily pass additional parameters to your feed script, as well as listen to ajax callbacks:

    $('#calendar').fullCalendar({

        eventSources: [

            // your event source
            {
                url: '/myfeed.php',
                type: 'POST',
                data: {
                    custom_param1: 'something',
                    custom_param2: 'somethingelse'
                }
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color: 'yellow',   // a non-ajax option
                textColor: 'black' // a non-ajax option
            }

            // any other sources...

        ]

    });

来自:http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

你会不会只是放一个逗号,然后基本上复制第一个?

我的第二个问题是。我将有一个事件源(因为它们都来自同一个源),但我将在其中有一组事件,每个组需要不同的颜色。

所以我可以拥有这个

Item 1 - group 1 (color red)
Item 2 - group 1 (color red)
Item 3 - group 2  (color green)
Item 4 - group 3 (color black)

现在这些颜色是由用户设置的,所以我永远不会知道第一个颜色组是什么颜色。一位用户可能会将其设置为红色,另一位用户可能会将其设置为蓝色。

所以我认为我需要在每个事件中发送颜色。因此,第 1 项将具有与之关联的颜色,第 2 项将具有与之关联的颜色等等。

如何做到这一点?我想一旦我得到事件回来我需要做点什么。我只是不确定是什么。

谢谢

【问题讨论】:

    标签: jquery jquery-plugins fullcalendar


    【解决方案1】:

    我设置不同颜色的方法是从源生成的 json 返回的事件对象中获取。在事件中我放了这样的东西:

     events: function(start, end, callback) {
        $.ajax({
            url: GlobalAjax.ajaxurl+'calendar/events',
            data: {
                // our hypothetical feed requires UNIX timestamps
                start: Math.round(start.getTime() / 1000),
                end: Math.round(end.getTime() / 1000)
            },
            success: function(doc) {
                var events = [];
                $(doc).each(function() {
                    events.push({
                        title: $(this).attr('title'),
                        start: $(this).attr('start'), // will be parsed
                        end: $(this).attr('end'), // will be parsed
                        color: $(this).attr('color') // will be parsed
                    });
                });
                callback(events);
            }
        });
    

    它的作用就像魅力

    【讨论】:

    • 设置不同颜色的方法是从事件对象中获取得到什么?
    【解决方案2】:

    我所做的是:

    $color = '#000';
    
    if($i == even){
      $color = '#fff';
    }
    
    {
    
    "title": "your title",
    "start": date('Y m d'),
    "end": date('Y m d', strtotime("+1 day")),
    "color": $color
    
    }
    

    它可以工作....不需要复杂的代码!

    【讨论】:

    • 你也可以用textColor:#fff改变textColor!
    【解决方案3】:

    要处理多个源,你是对的,只需在 eventSources 数组中添加更多:

    $('#calendar').fullCalendar({
    
        eventSources: [
    
            // your event source
            {
                url: '/myfeed.php',
                type: 'POST',
                data: {
                    custom_param1: 'something',
                    custom_param2: 'somethingelse'
                }
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color: 'yellow',   // a non-ajax option
                textColor: 'black' // a non-ajax option
            },
    
            // your second event source
            {
                url: '/myfeed.php',
                type: 'POST',
                data: {
                    custom_param3: 'somethingelseelse',
                    custom_param4: 'somethingelseelseelse'
                }
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color: 'red',   // a non-ajax option
                textColor: 'white' // a non-ajax option
            }
    
            // any other sources...
    
        ]
    
    });
    

    就多个组的不同颜色而言,fullCalendar 仅允许每个事件源使用一种颜色。因此,您必须为您拥有的每个组添加一个源到 eventSource。至于让用户自定义他们的颜色,使用上面的例子,你可以有这样的东西:

    $('#calendar').fullCalendar({
    
        eventSources: [
    
            // your event source
            {
                url: '/myfeed.php',
                type: 'POST',
                data: {
                    custom_param1: 'something',
                    custom_param2: 'somethingelse'
                }
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color: settings.customBackgroundColors(userId, groupX),   // a non-ajax option
                textColor: settings.customTextColors(userId, groupX) // a non-ajax option
            },
    
            // your second event source
            {
                url: '/myfeed.php',
                type: 'POST',
                data: {
                    custom_param3: 'somethingelseelse',
                    custom_param4: 'somethingelseelseelse'
                }
                error: function() {
                    alert('there was an error while fetching events!');
                },
                color: settings.customBackgroundColors(userId, groupY),   // a non-ajax option
                textColor: settings.customTextColors(userId, groupY) // a non-ajax option
            }
    
            // any other sources...
    
        ]
    
    });
    


    编辑

    如果您想从 json 提要中为每个事件提供单独的属性,例如单独的颜色,请执行以下操作。

    public int id { get; set; }
    public string title { get; set; }
    public bool allDay { get; set; }
    public string start { get; set; }
    public string end { get; set; }
    public string color { get; set; }
    public string textColor { get; set; }
    

    用你想要的颜色填充字符串,收集我为每个事件所拥有的内容,然后将它发送回一个 json 结果,每个任务都应该使用你在颜色属性中设置的任何内容。

    【讨论】:

    • @Milimetric - 对于颜色,我不能在渲染单元格之前以某种方式更改颜色吗?我一直在查看“eventRender”回调,但我不确定如何强制它改变颜色。我不能按照你的建议去做,因为我会有未知数量的组(可能是一组,也可能是 50 个)。我也会尝试在一个请求中将其全部发回。
    • 嗯,你可以检查一下日历加载后 HTML 的样子。然后你可以使用 jQuery 来选择和设置你想要的所有样式。
    • @Milimetric - 是的,我就是这样做的,但有点糟糕,因为我必须删除生成的类和东西。如果你能在渲染之前控制颜色并改变它会很好,但我想现在不可能。
    • 是的,也许他们稍后会更新。您不能只挂钩到生成的类并覆盖样式吗?像$('.generated-class').css('background-color', 'purple') 什么的?
    • @Milimetric - 这会添加内联样式吗?
    【解决方案4】:

    我知道你已经就这个问题进行了相当多的讨论,但我想我会提出一个建议。

    如果您有一组预定义的颜色供用户选择,您可以设置预定义的 CSS 类,然后使用 fullcalendar 的 className 属性。

    在我的日历上,我基本上会为未来或已经过去的事件着色,并且过去的事件使用这个 css。

    .fc-event,
    .fc-agenda .past .fc-event-time,
    .past a {
        background-color: green;
        boarder-color: green;
        color: white;
     }
    

    其中.past 指的是我的className

    然后当我在 jsp 中编译我的 JSON 时,它看起来像这样。

    {
        "title": "<%=e.getName()%>",
        "start": "<%=startString %>",
        "end": "<%=endString%>",
        "allDay": false
        <%if(e.isFinished()){%>,"className": "past"<%}%> 
    }
    

    我尝试过使用颜色选项之类的,但这种方法对我来说效果最好。

    【讨论】:

    • 我不想使用内联 javascript,因为用户可以拥有 X 组事件和 X 种颜色,我无法真正定义它们,除非样式表也是动态制作的。
    猜你喜欢
    • 1970-01-01
    • 2013-06-15
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多