【问题标题】:Close Angular 2 Bootstrap DatePicker after choosing date选择日期后关闭 Angular 2 Bootstrap DatePicker
【发布时间】:2016-11-04 13:21:17
【问题描述】:

我有两个不同的datepickers,我想在选择日期后关闭它们。我一直在网上找到关于autoclose: true 的东西,但我不知道如何/在哪里设置它。我可以选择日期并在输入和所有内容中显示更改,但我希望datepicker 在选择第一个日期后立即关闭。

<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]="getStartDate()" class="form-control" type="text" [formControl]="secondForm.controls['startDate']">
</div>
<div style="display:inline-block">
  <ngb-datepicker id="special" *ngIf="startCheck==true;" [(ngModel)]="dt" class="dropdown-toggle" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
</div>
<button type="button" class="btn icon-calendar" (click)="showStartDatePick()"></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]="getEndDate()" class="form-control" type="text" [formControl]="secondForm.controls['endDate']">
</div>
<div style="display:inline-block">
  <ngb-datepicker id="special" *ngIf="endCheck==true;" [(ngModel)]="dt2" class="dropdown-toggle" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
</div>
<button type="button" class="btn icon-calendar" (click)="showEndDatePick()"></button>
<button type="button" class="btn icon-search" [disabled]="!secondForm.valid"></button>
</div>
</form>

对应的打字稿:

import { Component } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
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' : [this.dt, Validators.required],
  'endDate' : [this.dt2, Validators.required]
})
this.secondForm.valueChanges.subscribe( (form: any) => {
    console.log('form changed to:', form);
  }
);
}

public getStartDate(): void {
let timestamp = this.dt != null ? new Date(this.dt.year, this.dt.month-1, this.dt.day).getTime() : new Date().getTime();
this.secondForm.controls['startDate'].setValue(this.datePipe.transform(timestamp, 'fullDate'));
}

public getEndDate(): void {
let timestamp = this.dt2 != null ? new Date(this.dt2.year, this.dt2.month-1, this.dt2.day).getTime() : new Date().getTime();
this.secondForm.controls['endDate'].setValue(this.datePipe.transform(timestamp, 'fullDate'));
}

public showStartDatePick():void {
if (this.startCheck == false){
  this.startCheck = true;
} else {
  this.startCheck = false;
}
}

public showEndDatePick():void {
if (this.endCheck == false){
  this.endCheck = true;
} else {
  this.endCheck = false;
}
}
}

【问题讨论】:

    标签: angular typescript bootstrap-datepicker ng-bootstrap


    【解决方案1】:

    这可以通过在ngModelChange 上附加一个函数来实现,如下所示:

    &lt;ngb-datepicker *ngIf="startCheck;" [(ngModel)]="startDate" (ngModelChange)="showDatePick(0)" class="dropdown-toggle" [ngModelOptions]="{standalone: true}" style="position:absolute; z-index:1"&gt;&lt;/ngb-datepicker&gt;

    这让我觉得 [(ngModel)] 和 (ngModelChange) 有点 hacky,但第一个是确保直接进行双向绑定,第二个是添加一层 在更改时执行此操作.

    要处理两次,将参数传递给showDatePick 并将适当的布尔值翻转为false:

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

    通过输入,我们将对设置进行一些巧妙的设置,以便我们可以重用相同的代码,因为如果有一段代码 99% 相同,那么可能有一种方法可以将其全部变成一件事:

    <input style="width:250px" [value]="getDate('start')" class="form-control" type="text" [formControl]="secondForm.controls['startDate']">

    还有getDate函数:

    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, 'fullDate'));
      }
    

    工作示例:http://plnkr.co/edit/BtBUvNYeUYFgr1VmACS3?p=preview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-25
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多