簡體   English   中英

Ng Bootstrap日期范圍選擇器[markDisabled]在輸入上不起作用

[英]Ng Bootstrap date range picker [markDisabled] doesn't work on input

我正在嘗試在ng bootstrap范圍選擇器上禁用某些日期

當前,我在彈出窗口中有一個范圍選擇器,並且我正在使用[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屬性,因為我希望將范圍選擇器彈出。

這是演示

我不確定為什么,但是以某種方式,您的自定義“天模板”( ng-template let-date="date" )似乎阻止了禁用日期在彈出式日歷上呈現/標記為禁用。 您可能無意中覆蓋了某些屬性。

我沒有深入研究您的代碼,但是我嘗試了以下方法,它似乎可以工作。

首先,在您的component.html上,我使用了DayTemplateContext一部分中的disabled屬性。 之后,針對disabled日期,將text-muted類設置為true。 對於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的提示,是的,我們可以簡單地在click事件中添加disabled檢查。 這樣,就不需要onDateSelection()語句上的if語句。 我已經更新了演示以反映更改。

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

您可以在這里參考我的演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM