-1

有经验的人可以帮助我。我有这样的问题。如果items.tasks.person_id等于items.people.id事件应该有颜色people.color

如何根据分配给人员的颜色更改事件的颜色?事件有一个人 id,人对象有一个颜色。

示例items.tasks.person_id (123)===items.people.id (123)事件应该有颜色items.people.color (# e31e24)

此处代码:https ://stackblitz.com/edit/react-v71fhc

class App extends Component {
  constructor() {
    super();
    this.state = {
      events: []
    };  
  }

  componentDidMount() {

    let appointments = {
      "items": {
          "tasks": [
              {
                  "id": "1",
                  "person_id": "123",
                  'title': 'Some Event',
                  'start': new Date(2019, 7, 28, 0, 0, 0),
                  'end': new Date(2019, 7, 28, 0, 0, 0)
              },
              {
                  "id": "2",
                  "person_id": "456",
                  'title': 'DTS ENDS',
                  'start': new Date(2019, 7, 28, 0, 0, 0),
                  'end': new Date(2019, 7, 28, 0, 0, 0)          
              }
          ],
          "people": [
              {
                  "id": "456",
                  "color": "#5cb85c"
              },
              {
                  "id": "123",
                  "color": "#e31e24"
              }
          ]
      }
    }

    let appoint = appointments.items.tasks
    console.log(appoint)

        for (let i = 0; i < appoint.length; i++) {
          appoint[i].id = appoint[i].id;
          appoint[i].title = appoint[i].title;
          appoint[i].start = moment.utc(appoint[i].start).toDate();
          appoint[i].end = moment.utc(appoint[i].end).toDate();      
        }
        this.setState({
          events:appoint
        })


  }

  render() {
    return (
      <div>
        <Calendar
          localizer={localizer}
          events={this.state.events}
          startAccessor="start"
          endAccessor="end"
          defaultView="week"
          defaultDate={moment().toDate()}
        />
        </div>
    );
  }
}
4

1 回答 1

1

你可以试试这个。

import React, { Component } from 'react';
import { render } from 'react-dom';
import { Calendar, momentLocalizer } from 'react-big-calendar';
import moment from 'moment';
import 'react-big-calendar/lib/css/react-big-calendar.css';
const localizer = momentLocalizer(moment);

class App extends Component {
  constructor() {
    super();
    this.state = {
      events: []
    };
  }

  componentDidMount() {

    let appointments = {
      "items": {
        "tasks": [
          {
            "id": "1",
            "person_id": "123",
            'title': 'Some Event',
            'start': new Date(2019, 7, 28, 0, 0, 0),
            'end': new Date(2019, 7, 28, 0, 0, 0)
          },
          {
            "id": "2",
            "person_id": "456",
            'title': 'DTS ENDS',
            'start': new Date(2019, 7, 28, 0, 0, 0),
            'end': new Date(2019, 7, 28, 0, 0, 0)
          }
        ],
        "people": [
          {
            "id": "456",
            "color": "#5cb85c"
          },
          {
            "id": "123",
            "color": "#e31e24"
          }
        ]
      }
    }

    let appoint = appointments.items.tasks
    console.log(appoint)

    for (let i = 0; i < appoint.length; i++) {
      appoint[i].id = appoint[i].id;
      appoint[i].title = appoint[i].title;
      appoint[i].start = moment.utc(appoint[i].start).toDate();
      appoint[i].end = moment.utc(appoint[i].end).toDate();
      const color = appointments.items.people.find(aPeople => aPeople.id === appoint[i].person_id).color

      appoint[i].hexColor = color
    }
    this.setState({
      events: appoint
    })


  }

  eventStyleGetter = (event, start, end, isSelected) => {
    console.log(event);
    var backgroundColor = event.hexColor;
    var style = {
      backgroundColor: backgroundColor,
      borderRadius: '0px',
      opacity: 0.8,
      color: 'black',
      border: '0px',
      display: 'block'
    };
    return {
      style: style
    };
  }

  render() {
    return (
      <div>
        <Calendar
          localizer={localizer}
          events={this.state.events}
          startAccessor="start"
          endAccessor="end"
          defaultView="week"
          defaultDate={moment().toDate()}
          eventPropGetter={(this.eventStyleGetter)}
        />
      </div>
    );
  }
}

render(<App />, document.getElementById('root'));

这应该改变事件的颜色。

于 2019-08-29T22:49:58.700 回答