[英]How to disable specific dates in ion-datetime?
I am using ion-datetime for selecting a particular date in my Ionic3/Angular
application.我正在使用ion-datetime在我的Ionic3/Angular
应用程序中选择特定日期。 I want to disable certain specific dates in ion-datetime
.我想禁用ion-datetime
中的某些特定日期。 I have checked the documentation but there is no hint on how can i achieve that.我已经检查了文档,但没有提示我如何实现这一点。
Can anyone help on that?有人可以帮忙吗?
Looks like it is not possible to disable particular days/dates using ion-datetime
of ionic3.看起来无法使用 ionic3 的ion-datetime
禁用特定的日期/日期。
Check out documentation for more functions and information:查看文档以获取更多功能和信息:
https://ionicframework.com/docs/api/components/datetime/DateTime/ https://ionicframework.com/docs/api/components/datetime/DateTime/
Alternatively, you can do it with libraries like this:或者,您可以使用这样的库来执行此操作:
https://github.com/rajeshwarpatlolla/ionic-datepicker https://github.com/rajeshwarpatlolla/ionic-datepicker
TL;DR - [isDateEnabled]="isBlockedDate"
- calculate blocked dates with this, passes a string of the date being checked (eg '2022-08-23') TL;DR - [isDateEnabled]="isBlockedDate"
- 用这个计算被阻止的日期,传递一个被检查日期的字符串(例如'2022-08-23')
-- --
In Ionic 6 you can utilise the new isEnabledDate
property to check against dates you'd like to block.在 Ionic 6 中,您可以利用新的isEnabledDate
属性来检查您想要阻止的日期。
isBlockedDate = (dateString: string) => {
const result = this.current_month_blockout_dates.some((date) => {
let interstitial_date = `${date.years}-${('0' + (date.months)).slice(-2)}-${date.date}`;
// eg. comparing 2022-08-21 with 2022-08-12
return dateString == interstitial_date;
});
if(result === true) {
console.log(`Blocked confirmation for date: ${dateString}`, result);
}
return !result;
}
So provided you have a this.current_month_blockout_dates
populated with an array of dates;因此,只要您有一个this.current_month_blockout_dates
填充日期数组; read how the code interprets, above:阅读上面的代码解释方式:
this.current_month_blockout_dates = [{years:'2022', months:'10', date:'14'}];
Usage would then be [isDateEnabled]="isBlockedDate"
ie:然后使用[isDateEnabled]="isBlockedDate"
即:
<ion-datetime
#orderDatePicker
id="orderDatePicker"
size="fixed"
(click)="createDateListeners()"
[isDateEnabled]="isBlockedDate"
presentation="date"
max="2025"
[min]="todaysDate">
</ion-datetime>
Bonus Tip奖金提示
You can add an observer to know when the month is changed and recalculate your blocked dates like so:您可以添加观察员以了解月份何时更改并重新计算您的阻止日期,如下所示:
createDateListeners() {
// Observe Date changes
const self = this;
var previous = '';
const targetNode = document.querySelector('ion-datetime#orderDatePicker');
const config = { attributes: true, childList: true, subtree: true };
const callback = function(mutationsList, observer) {
for(const mutation of mutationsList) {
if (mutation.type === 'attributes') {
var e = document.querySelector('ion-datetime#orderDatePicker').shadowRoot.querySelector('ion-label').textContent;
if(e !== previous)
{
previous = e;
console.log('[Date Listener]: e', e);
let date_interpret = new Date(e);
self.current_month = date_interpret.getMonth()+1;
console.log('[Date Listener]: Current month', self.current_month);
self.current_month_blockout_dates = self.checkMonth(self.current_month);
return;
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
My code uses a function called checkMonth
, yours can be your own.我的代码使用了一个名为checkMonth
的函数,你的可以是你自己的。 Good luck.祝你好运。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.