gpt4 book ai didi

javascript - Ng Bootstrap 日期范围选择器 [markDisabled] 对输入不起作用

转载 作者:行者123 更新时间:2023-12-04 17:39:15 25 4
gpt4 key购买 nike

我正在尝试禁用 ng bootstrap range picker 上的某些日期

目前,我在弹出窗口中有一个范围选择器,我正在使用 [markDisabled] 来禁用某些日期。

HTML

<form class="form-inline">
<div class="form-group">
<div class="input-group">
<input
#myRangeInput
class="form-control"
placeholder="mm/dd/yyyy - mm/dd/yyyy"
name="dp"
[(ngModel)]="model"
ngbDatepicker
[dayTemplate]="t"
[autoClose]="false"
[displayMonths]="2"
[maxDate]="maxDate"
[minDate]="minDate"
#d="ngbDatepicker" [markDisabled]="isDisabled">

<ng-template #t let-date="date" let-focused="focused" >
<span class="custom-day"
[class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
[class.faded]="isHovered(date) || isInside(date)"
(click)="onDateSelection(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null"
>
{{ date.day }}
</span>
</ng-template>
<div class="input-group-append">
<button class="btn btn-outline-secondary" (click)="d.toggle()" type="button"> click
</button>
</div>
</div>
</div>
</form>

组件:

 const now = new Date();
const equals = (one: NgbDateStruct, two: NgbDateStruct) =>
one && two && two.year === one.year && two.month === one.month && two.day
=== one.day;

const before = (one: NgbDateStruct, two: NgbDateStruct) =>
!one || !two ? false : one.year === two.year ? one.month === two.month ?
one.day === two.day
? false : one.day < two.day : one.month < two.month : one.year < two.year;

const after = (one: NgbDateStruct, two: NgbDateStruct) =>
!one || !two ? false : one.year === two.year ? one.month === two.month ? one.day === two.day
? false : one.day > two.day : one.month > two.month : one.year > two.year;



export class NgbdDatepickerRange implements OnInit{
isDisabled = (date: NgbDate, current: {month: number}) => date.day === 13;

startDate: NgbDateStruct;
maxDate: NgbDateStruct;
minDate: NgbDateStruct;
hoveredDate: NgbDateStruct;
fromDate: any;
toDate: any;
model: any;
private _subscription: Subscription;
private _selectSubscription: Subscription;
@ViewChild("d") input: NgbInputDatepicker;
@ViewChild(NgModel) datePick: NgModel;
@ViewChild('myRangeInput') myRangeInput: ElementRef;

isHovered = date =>
this.fromDate && !this.toDate && this.hoveredDate && after(date, this.fromDate) && before(date, this.hoveredDate)
isInside = date => after(date, this.fromDate) && before(date, this.toDate);
isFrom = date => equals(date, this.fromDate);
isTo = date => equals(date, this.toDate);
constructor(element: ElementRef, private renderer: Renderer2, private _parserFormatter: NgbDateParserFormatter) {

}
ngOnInit() {
this.startDate = {year: now.getFullYear(), month: now.getMonth() + 1, day: now.getDate()};
this.maxDate = { year: now.getFullYear() + 1, month: now.getMonth() + 1, day: now.getDate()};
this.minDate = {year: now.getFullYear() - 1, month: now.getMonth() + 1, day: now.getDate()};
}

onDateSelection(date: NgbDateStruct) {
let parsed = '';
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && after(date, this.fromDate)) {
this.toDate = date;
// this.model = `${this.fromDate.year} - ${this.toDate.year}`;
this.input.close();
} else {
this.toDate = null;
this.fromDate = date;
}
if(this.fromDate) {
parsed += this._parserFormatter.format(this.fromDate);
}
if(this.toDate) {
parsed += ' - ' + this._parserFormatter.format(this.toDate);
}

this.renderer.setProperty(this.myRangeInput.nativeElement, 'value', parsed);
}
}

[markDisabled] 在如下使用时工作正常

 <ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" 
[dayTemplate]="t" outsideDays="hidden" [markDisabled]="isDisabled">

我想在输入元素内使用 markdisabled 属性,因为我希望范围选择器作为弹出窗口。

这是 Demo

最佳答案

我不确定为什么,但不知何故,您的自定义日期模板 (ng-template let-date="date") 似乎阻止了禁用日期在弹出日历。您可能不小心覆盖了一些属性。

我没有深入查看您的代码,但我尝试了以下方法,它似乎有效。

首先,在您的 component.html 上,我使用了 disabled 属性,它是 DayTemplateContext 的一部分。 .之后,我将 text-muted 类设置为 true,用于 disabled 日期。这将为 disabled 日期提供灰色外观。请确保您的

<ng-template #t let-date="date" let-focused="focused" let-disabled="disabled">
<span class="custom-day"
[class.text-muted]="disabled"
[class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
[class.faded]="isHovered(date) || isInside(date)"
(click)="onDateSelection(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>

在您的 component.ts 上,我对您的 onDateSelection() 方法进行了以下更改。这不是很优雅,但它现在可以完成工作。我基本上用 if 语句将其包装起来,以检查日期是否在 13 日(禁用日期)。这将防止选择日期本身。

onDateSelection(date: NgbDateStruct) {
let parsed = '';
if (date.day!==13) {
.
.
// rest of your code
}
}

已编辑:感谢@Eliseo 的提示,是的,我们可以简单地在点击事件上添加检查 disabled。这样,您的 onDateSelection() 语句中就不需要 if 语句了。我已经更新了演示以反射(reflect)更改。

<ng-template #t let-date="date" let-focused="focused" let-disabled="disabled">
<span class="custom-day"
[class.text-muted]="disabled"
[class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
[class.faded]="isHovered(date) || isInside(date)"
(click)="!disabled && onDateSelection(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>

可以引用我的demo here

关于javascript - Ng Bootstrap 日期范围选择器 [markDisabled] 对输入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com