som*_*one 5 javascript reactjs antd
我正在研究 ant design 日历文档。
我想在当月的特定日期设置事件。我正在使用这段提供蚂蚁设计文档的代码。但在他们的代码中,每个月都会显示创建的事件。
例如,我如何才能仅在四月显示?
因为就我而言,我应该从后端接收不同年份、月份和日期的事件列表,并显示在日历中。
这是ant设计文档代码
function getListData(value) {
let listData;
switch (value.date()) {
case 8:
listData = [
{ type: "warning", content: "This is warning event." },
{ type: "success", content: "This is usual event." }
];
break;
default:
}
return listData || [];
}
function dateCellRender(value) {
const listData = getListData(value);
return (
<ul className="events">
{listData.map((item) => (
<li key={item.content}>
<Badge status={item.type} text={item.content} />
</li>
))}
</ul>
);
}
ReactDOM.render(
<Calendar dateCellRender={dateCellRender} />,
document.getElementById("container")
);
Run Code Online (Sandbox Code Playgroud)
这是我从后端收到的代码。
formattedBooking我的数据来自后端
const getListData = value => {
const listData = [];
if (formattedBookings && formattedBookings[value.date()]) {
formattedBookings[value.date()].forEach(booking => {
listData.push({
type: 'success',
content: `${booking.address}`,
});
});
}
return listData;
};
const dateCellRender = value => {
const listData = getListData(value);
return (
<ul className='events'>
{listData.map((item, index) => (
<li key={`${item.content}-${index}`}>
<Badge status={item.type} text={item.content} />
</li>
))}
</ul>
);
};
return (
<Calendar
dateCellRender={dateCellRender}
/>
);
Run Code Online (Sandbox Code Playgroud)
请帮我解决这个问题。
发送到的每个值getListData都是与日历视图上的特定日期相关的时刻对象,因此您可以在后端响应中将其解析为相同的日期格式,并决定要在该特定日期渲染什么。这是一个例子:
function getListData(value) {
let listData;
let dateValue = value.format("yyyy/MM/DD"); // you can parse value in every format you want
switch (dateValue) {
case "2021/12/26":
listData = [
{ type: "warning", content: "This is warning event." },
{ type: "success", content: "This is usual event." }
];
break;
case "2022/01/12":
listData = [
{ type: "error", content: "This is error event 1." },
{ type: "error", content: "This is error event 2." },
{ type: "error", content: "This is error event 3." }
];
break;
case "2022/02/08":
listData = [
{ type: "success", content: "This is usual event1." },
{ type: "success", content: "This is usual event2." }
];
break;
default:
}
return listData || [];
}
Run Code Online (Sandbox Code Playgroud)
这是沙箱的编辑版本。
| 归档时间: |
|
| 查看次数: |
3017 次 |
| 最近记录: |