【发布时间】:2019-08-29 22:12:32
【问题描述】:
能否请更有经验的人帮助我。我有这样的问题。
如果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>
);
}
}
【问题讨论】:
-
你为什么使用 React?
-
@RokoC.Buljan 这只是我项目的一部分
-
@Umbro 是否可以将
people从数组更改为对象?这样就更容易改变事件的颜色了。 -
@MurliPrajapati 无法更改。这是来自服务器的结构、数据(响应)。我无法改变
标签: javascript reactjs react-big-calendar