繁体   English   中英

将ng-bootstrap datepicker填充的输入初始化为空白

[英]Initialize input populated by ng-bootstrap datepicker to blank

我有两个输入,每个输入由它们自己的ng-bootstrap datepicker填充,当我单击按钮时会弹出该输入。 当您选择一个新日期时,输入将正确填充,但是我的问题是,如果我想将输入初始化为null或仅仅是另一个值。 我知道我的getDate函数妨碍了我的工作,我只是不确定如何更改它以允许这样做。

屏幕截图(日期选择器的打开是通过单击每个输入右侧的按钮,每个输入一个)的: 在此处输入图片说明

HTML:

<form class="form-inline">
  <div>
    <div class="form-group" [ngClass]="{'has-error':!secondForm.controls['startDate'].valid && secondForm.controls['startDate'].touched}">
      <label>Start Date:</label>
      <input style="width:250px" [value]="getDate('start')" class="form-control" type="text" [formControl]="secondForm.controls['startDate']">
    </div>
    <div style="display:inline-block">
      <ngb-datepicker id="special" *ngIf="startCheck;" [(ngModel)]="startDate" (ngModelChange)="showDatePick(0)" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
    </div>
    <button type="button" class="btn icon-calendar" (click)="showDatePick(0)"></button>
    <div class="form-group" [ngClass]="{'has-error':!secondForm.controls['endDate'].valid && secondForm.controls['endDate'].touched}">
      <label>End Date:</label>
      <input style="width:250px" [value]="getDate('end')" class="form-control" type="text" [formControl]="secondForm.controls['endDate']">
    </div>
    <div style="display:inline-block">
      <ngb-datepicker id="special" *ngIf="endCheck;" [(ngModel)]="endDate" (ngModelChange)="showDatePick(1)" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
    </div>
    <button type="button" class="btn icon-calendar" (click)="showDatePick(1)"></button>
    <button type="submit" class="btn icon-search" [disabled]="!secondForm.valid"></button>
    <span [hidden]="!secondForm.hasError('endDateLessThanStartDate')" class="alert alert-danger first">End Date must be equal to or after Start Date</span>
  </div>
</form>

打字稿:

import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import {NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
import {DatePipe} from "@angular/common";

@Component({
  selector: 'calendar-pick',
  styleUrls: ['../app.component.css'],
  templateUrl: './calendarpick.component.html',
  providers: [DatePipe]
})

export class CalendarPickComponent {
  public dt: NgbDateStruct;
  public dt2: NgbDateStruct;
  public startCheck: boolean = false;
  public endCheck: boolean = false;
  secondForm : FormGroup;

  public constructor(fb: FormBuilder, private datePipe: DatePipe) {
    this.secondForm = fb.group({
    'startDate' : [null, Validators.required],
    'endDate' : [null, Validators.required]
    }, {validator: this.endDateAfterOrEqualValidator})
  }

  public getDate(dateName: string) {
    let workingDateName = dateName + 'Date';
    let timestamp = this[workingDateName] != null ? new Date(this[workingDateName].year, this[workingDateName].month-1, this[workingDateName].day).getTime() : new Date().getTime();
    this.secondForm.controls[dateName + 'Date'].setValue(this.datePipe.transform(timestamp, 'MM/dd/yyyy'));
  }

  public showDatePick(selector):void {
    if(selector === 0) {
      this.startCheck = !this.startCheck;
    } else {
      this.endCheck = !this.endCheck;
    }
  }

  endDateAfterOrEqualValidator(formGroup): any {
    var startDateTimestamp, endDateTimestamp;
    for(var controlName in formGroup.controls) {
      if (controlName.indexOf("startDate") !== -1) {
        startDateTimestamp = Date.parse(formGroup.controls[controlName].value);
      }
      if (controlName.indexOf("endDate") !== -1) {
        endDateTimestamp = Date.parse(formGroup.controls[controlName].value);
      }
    }
    return (endDateTimestamp < startDateTimestamp) ? { endDateLessThanStartDate: true } : null;
  }
 }

getDate函数的第二行更改为let timestamp = this[workingDateName] != null ? new Date(this[workingDateName].year, this[workingDateName].month - 1, this[workingDateName].day).getTime() : null; let timestamp = this[workingDateName] != null ? new Date(this[workingDateName].year, this[workingDateName].month - 1, this[workingDateName].day).getTime() : null;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM