简体   繁体   English

如何将动态日期时间选择器添加到我的应用程序?

[英]How can I add a dynamic date time picker to my app?

I'm trying to create a web app that supports online reservation/booking system.我正在尝试创建一个支持在线预订/预订系统的 web 应用程序。 I want to create a dynamic date picker that allows the user to pick some specific dates, based on the availability.我想创建一个动态日期选择器,允许用户根据可用性选择一些特定日期。

Let's say the data for available dates is stored in an array slots .假设可用日期的数据存储在数组slots中。 How can modify the date picker to only allow the user to choose from set of these dates, while disabling the rest.如何修改日期选择器以仅允许用户从这些日期集中进行选择,同时禁用 rest。

I'm use DatePicker from @material-ui/pickers and I was able to find disablePast which disables all the previous dates but couldn't find any prop, that fits my use case.我使用来自@material-ui/pickers pickers 的 DatePicker,我能够找到disablePast禁用所有以前的日期但找不到任何适合我的用例的道具。

Can someone help me out?有人可以帮我吗?

What you can do for your this is, you can use the shouldDisableDate prop in which you have to pass a function that accepts date as an argument and return a boolean true/false whether the date should be rendered or not.您可以为此做的是,您可以使用shouldDisableDate道具,您必须在其中传递接受date作为参数的 function 并返回 boolean true/false ,无论是否应呈现日期。

For example:例如:

In your component's shouldDisableDate prop you can pass a function like this在组件的shouldDisableDate道具中,您可以像这样传递 function

<DatePicker  
  shouldDisableDate={renderDateFunction}
  ...

and then define the function as然后将 function 定义为

const renderDateFunction = (renderDate) => {
    
   // If the current date passed to this function is included in the slots
   // then enable selection of that date
   if(slots?.includes(renderDate)) return true;

   // else prevent the user from selecting that date
    return false;
  };

You can read more about the DatePicker Component API from the documentation here: https://material-ui-pickers.dev/api/DatePicker您可以从此处的文档中阅读有关 DatePicker 组件 API 的更多信息: https://material-ui-pickers.dev/api/DatePicker

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何与日期选择器一起添加时间选择器 - how to add time picker along with date picker 如何将日期选择器更改为仅一年的选择器? - How can I change my date picker into a year only picker? 我如何更改我的日期范围选择器的第一次显示月份 - how can i change my date range picker first time showing month 如何动态地向我的角度应用程序添加动态指令? - How can I add dynamic directives to my angular app on the fly? 如何在 MUI 5 日期范围选择器中添加预设范围 - How can i add Preset Ranges in MUI 5 date range picker 如何添加日期和时间选择器引导插件? - How to add date and time picker bootstrap plugin? 如何添加和删除时间或日期选择器? - How to add and remove time or date picker materialize? 如何在Bootstrap日期时间选择器中添加秒数 - How to add seconds to Bootstrap date time picker 如何结合日期选择器和时间选择器的输出在 JavaScript 中创建单个 date.toISOString? - How can I combine the output of a date picker and time selector to make a single date.toISOString in JavaScript? 如何默认使用日期选择器的占位符? - How can I default to placeholder for date picker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM