【问题标题】:calendar Ant Design: how to show event only in specific day in month using React JS日历 Ant 设计:如何使用 React JS 仅在一个月中的特定日期显示事件
【发布时间】:2022-01-10 18:52:12
【问题描述】:

我正在研究 ant design 日历documentation

我想在当月的特定日期设置事件。我正在使用这个code,它提供了 ant 设计文档。但在他们的代码中,每个月都会显示创建的事件。

我怎样才能仅在四月展示?

因为在我的情况下,我应该从后端收到不同年份、月份和日期的事件列表并显示在日历中。

这是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")
    );

这是我从后端收到的代码。

formattedBooking 是我来自后端的data

     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}
        />
     );

请帮我解决这个问题。

【问题讨论】:

    标签: javascript reactjs antd


    【解决方案1】:

    发送到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 || [];
    }
    

    Here 是您的沙盒的编辑版本。

    【讨论】:

    • Saeed Shamloo,感谢您的帮助,这对我很有帮助。我的代码也从 formattedBookings[value.date()] 更改为 formattedBookings[value.format("YY/MM/DD")]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多