如何根据活动更改特定日期事件的颜色?

huangapple go评论70阅读模式
英文:

How can I change the color of events of certain day regarding to their activities?

问题

[![示例图片][1]][1]

  [1]: https://i.stack.imgur.com/oFRyo.png

在这个日历中,我想根据活动类型更改事件的颜色,例如长事件应该是绿色。

以下是我尝试获取的示例数据。

这是我从后端收到的数据。

```json
{
    'title': '全天事件 非常长的标题',
    'allDay': true,
    'start': new Date(2023, 1, 1),
    'end': new Date(2023, 1, 6),
    'event': '常规'
},
{
    'title': '长事件',
    'start': new Date(2023, 1, 1),
    'end': new Date(2023, 1, 1),
    'event':'假日',
},

Front.js

<Calendar
    views={["day", "agenda", "work_week", "month"]}
    selectable
    localizer={localizer}
    defaultDate={new Date()}
    defaultView="month"
    events={eventsData}
    style={{ height: "95%" }}
    onSelectEvent={(event) => alert(event.title)}
    onSelectSlot={handleSelect}
/>
英文:

如何根据活动更改特定日期事件的颜色?

So in this calendar, I want to change the color of event depending to their activities, so for example the long event should have a color green.

so this is the sample data i'm trying to get.

this is what I received from my backend.

  {
      &#39;title&#39;: &#39;All Day Event very long title&#39;,
      &#39;allDay&#39;: true,
      &#39;start&#39;: new Date(2023, 1, 1),
      &#39;end&#39;: new Date(2023, 1, 6),
      &#39;event&#39;: &#39;regular&#39;
    },
    {
      &#39;title&#39;: &#39;Long Event&#39;,
      &#39;start&#39;: new Date(2023, 1, 1),
      &#39;end&#39;: new Date(2023, 1, 1),
      &#39;event&#39;:&#39;holiday&#39;,
    },

Front.js

&lt;Calendar

    views={[&quot;day&quot;, &quot;agenda&quot;, &quot;work_week&quot;, &quot;month&quot;]}
    selectable
    localizer={localizer}
    defaultDate={new Date()}
    defaultView=&quot;month&quot;
    events={eventsData}
    style={{ height: &quot;95%&quot; }}
    onSelectEvent={(event) =&gt; alert(event.title)}
    onSelectSlot={handleSelect}
        /&gt;

答案1

得分: 1

我认为你可以在事件对象上提供 colorbgcolor 属性以及其他属性,如果这个包不支持它,可以使用 Full Calendar,你肯定可以在完整的日历上这样做,

events=[
  {
    groupId: '2023-02-05',
    title: '全天',
    allDay: true,
    backgroundColor: '#A685DB',
    textColor: 'white',
    borderColor: '#A685DB',
    start: '2023-02-05T12:00',
  },
  {
    groupId: '2023-02-05',
    title: 'KLB003',
    backgroundColor: '#E31BCF',
    textColor: 'white',
    borderColor: '#E31BCF',
    start: '2023-02-05T14:00',
  }
]

尝试按照这个结构提供。

英文:

I think you can provide color and bgcolor attributes along with other properties on event object,
If this package doesn't support's it, Use Full calendar, You surely can do that on full calendar,

events={[
          {
            groupId: &#39;2023-02-05&#39;,
            title: &#39;Full Time&#39;,
            allDay: true,
            backgroundColor: &#39;#A685DB&#39;,
            textColor: &#39;white&#39;,
            borderColor: &#39;#A685DB&#39;,
            start: &#39;2023-02-05T12:00&#39;,
          },
          {
            groupId: &#39;2023-02-05&#39;,
            title: &#39;KLB003&#39;,
            backgroundColor: &#39;#E31BCF&#39;,
            textColor: &#39;white&#39;,
            borderColor: &#39;#E31BCF&#39;,
            start: &#39;2023-02-05T14:00&#39;,
          }]

Try supplying on this structure.

答案2

得分: 0

可以通过处理库提供的eventPropGetter函数,并返回需要应用的样式来实现。然后,您可以通过以下结构来决定哪个事件更短/更长:

const eventPropGetter = (event) => {
    console.log("event", event.isLong);
    if (event.isLong) {
      return {
        style: {
          backgroundColor: "#f0ad4e",
          borderRadius: "0px",
          opacity: 0.8,
          color: "white",
          border: "none"
        }
      };
    }

    // 为短事件自定义样式
    return {
      style: {
        backgroundColor: "#5cb85c",
        borderRadius: "0px",
        opacity: 0.8,
        color: "white",
        border: "none"
      }
    };
};

然后,您可以通过以下方式来定义事件,以确定哪些事件是较短的或较长的:

export const events = [
  {
      'title': '全天事件,非常长的标题',
      'allDay': true,
      'start': new Date(2023, 1, 1),
      'end': new Date(2023, 1, 6),
      'event': '常规',
      // 这里还有一个键
      'isLong': true
    },
    {
      'title': '长事件',
      'start': new Date(2023, 1, 1),
      'end': new Date(2023, 1, 1),
      'event':'假日',
      // 这里还有一个键
      'isLong': false
    },
];

请注意,这是JavaScript代码的翻译,不包括任何其他内容。

英文:

It is possible by handling eventPropGetter function provided by lib and returing your style that needs to be applied.

const eventPropGetter = (event) =&gt; {
    console.log(&quot;event&quot;, event.isLong);
    if (event.isLong) {
      return {
        style: {
          backgroundColor: &quot;#f0ad4e&quot;,
          borderRadius: &quot;0px&quot;,
          opacity: 0.8,
          color: &quot;white&quot;,
          border: &quot;none&quot;
        }
      };
    }

    // Customize styles for short events
    return {
      style: {
        backgroundColor: &quot;#5cb85c&quot;,
        borderRadius: &quot;0px&quot;,
        opacity: 0.8,
        color: &quot;white&quot;,
        border: &quot;none&quot;
      }
    };
  };

And then you only decide which event is shorter/longer by structuring like this,

export const events = [
  {
      &#39;title&#39;: &#39;All Day Event very long title&#39;,
      &#39;allDay&#39;: true,
      &#39;start&#39;: new Date(2023, 1, 1),
      &#39;end&#39;: new Date(2023, 1, 6),
      &#39;event&#39;: &#39;regular&#39;,
      //one more key here
      &#39;isLong&#39;: true
    },
    {
      &#39;title&#39;: &#39;Long Event&#39;,
      &#39;start&#39;: new Date(2023, 1, 1),
      &#39;end&#39;: new Date(2023, 1, 1),
      &#39;event&#39;:&#39;holiday&#39;,
      //one more key here
      &#39;isLong&#39;: false
    },

];

huangapple
  • 本文由 发表于 2023年2月27日 15:00:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定