伍引导日期范围选择器[markDisabled]输入时不工作(Ng Bootstrap date r

2019-09-27 03:34发布

我试图禁用特定日期纳克引导范围选择器

目前,我有一个弹出窗口内的一系列选择器,和我使用[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性质,因为我想的范围选择器的弹出。

这里的演示

Answer 1:

我不知道为什么,但不知何故,您的自定义模板日( ng-template let-date="date" )似乎已经阻止了禁用日期被渲染/标记为禁用的跳隙压延机。 你可能已经覆盖意外的某些属性。

我还没有进行深入的看看你的代码,但我曾尝试以下,似乎工作。

首先,在你的component.html,我已经使用的disabled属性,它是一部分DayTemplateContext 。 这之后,我设置的text-muted级为真,为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上的click事件。 这样一来,就没有必要对您的onDateSelection()语句,如果语句。 我已经更新了演示,以反映更改。

<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>

你可以参考我的演示在这里



文章来源: Ng Bootstrap date range picker [markDisabled] doesn't work on input