Ng Bootstrap日期范围选择器[markDisabled]在输入上不起作用 [英] Ng Bootstrap date range picker [markDisabled] doesn't work on input

查看:82
本文介绍了Ng Bootstrap日期范围选择器[markDisabled]在输入上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ng引导程序范围选择器上禁用某些日期

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

Currently, I have a range picker inside a pop-up, and I am using [markDisabled] to disable certain dates.

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]如下使用时可以正常工作

[markDisabled] works fine when used as following

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

我想在输入元素中使用markdisabled属性,因为我希望将范围选择器弹出.

I would like to use markdisabled property inside input element since I want range picker as a pop up.

这是演示

推荐答案

我不确定为什么,但是以某种方式,您的自定义日模板(ng-template let-date="date")似乎阻止了禁用日期的显示/标记为禁用日期.弹出式压光机.您可能会无意中覆盖了某些属性.

I am not sure why, but somehow, your custom Day Template (ng-template let-date="date") seem to have prevented the disabled dates from being rendered/marked as disabled on the popout calender. You might have overwritten some properties by accident.

我没有深入研究您的代码,但是我尝试了以下方法,它似乎可以工作.

I haven't had an in-depth look at your code, but I have tried the following and it seems to work.

首先,在您的component.html上,我使用了disabled属性,该属性是

First, on your component.html, I have made use of the disabled property which is part of the DayTemplateContext. After which I set the text-muted class as true, for disabled dates. This will give the greyed out appearance for disabled dates. Do make sure that your

<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号(这是禁用日期).这将防止选择日期本身.

On your component.ts, I have made the following changes to your onDateSelection() method. This is not very elegant, but it does the job for now. I have basically wrapped it with an if-statement to check if the date is on the 13th (which is the disabled date). This will prevent the date itself from being selected.

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

感谢@Eliseo的提示,是的,我们可以简单地在click事件上添加对disabled的检查.这样,就不需要onDateSelection()语句上的if语句.我已经更新了演示以反映所做的更改.

EDITED: Thanks @Eliseo for the tips, and yes, we can simply add check for disabled on the click event. This way, there is no need for that if-statement on your onDateSelection() statement. I have updated the demo to reflect the changes.

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

您可以在此处参阅我的演示 a>

You may refer to my demo here

这篇关于Ng Bootstrap日期范围选择器[markDisabled]在输入上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆