我在从ng-bootstrap范围选择日期选择器中选择日期时遇到问题。我只能看到一个日期的值,而看不到第二个日期的值。使用反应式表格时,如何选择第二个日期。谢谢。请查看此stackblitz链接CLICK HERE
<ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" [dayTemplate]="t" outsideDays="hidden" formControlName="date">
</ngb-datepicker>
<ng-template #t let-date let-focused="focused">
<span class="custom-day"
[class.focused]="focused"
[class.range]="isRange(date)"
[class.faded]="isHovered(date) || isInside(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>
答案 0 :(得分:0)
您可以做这样的事情
示例this
import {Component} from '@angular/core';
import {NgbDate, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NgbDateParserFormatter } from './NgbDateParserFormatter';
@Component({
selector: 'ngbd-datepicker-range',
templateUrl: './datepicker-range.html',
styles: [`
.custom-day {
text-align: center;
padding: 0.185rem 0.25rem;
display: inline-block;
height: 2rem;
width: 2rem;
}
.custom-day.focused {
background-color: #e6e6e6;
}
.custom-day.range, .custom-day:hover {
background-color: rgb(2, 117, 216);
color: white;
}
.custom-day.faded {
background-color: rgba(2, 117, 216, 0.5);
}
`]
})
export class NgbdDatepickerRange {
hoveredDate: NgbDate;
alldate:String;
fromDate: any;
toDate: any;
list: NgbDate[] = [];
dateForm: FormGroup;
constructor(calendar: NgbCalendar, private formBuilder: FormBuilder) {
this.fromDate = calendar.getToday();
this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
this.dateForm = this.formBuilder.group({
date: [null, Validators.required],
});
}
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
console.log("if " +JSON.stringify(date));
} else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
this.toDate = date;
this.list.push(date);
console.log("else if " +JSON.stringify(date));
} else {
this.toDate = null;
this.list.push(date);
this.fromDate = date;
console.log("else " +JSON.stringify(date));
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date);
}
onSubmit(form: FormGroup) {
console.log(this.list);
console.log(form.value)
}
}
答案 1 :(得分:0)
您没有在反应形式中插入第二个日期,这就是为什么您看不到它的原因。我的方法是使用setValue
在反应形式中插入日期/从日期
相关的 HTML :
<form [formGroup]="myDateForm" (ngSubmit)="onSubmit(myDateForm)">
<ngb-datepicker #dp (select)="onDateSelection($event)" [displayMonths]="2" [dayTemplate]="t" outsideDays="hidden" >
</ngb-datepicker>
<ng-template #t let-date let-focused="focused">
<span class="custom-day"
[class.focused]="focused"
[class.range]="isRange(date)"
[class.faded]="isHovered(date) || isInside(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>
<!--
<pre>From: {{ fromDate | json }} </pre>
<pre>To: {{ toDate | json }} </pre>
-->
<br/>
<button type="submit" class="btn btn-sm btn-primary">Submit</button>
</form>
<b>Form Status:</b>{{myDateForm.value | json}}
相关的 TS :
import { Component } from '@angular/core';
import { NgbDate, NgbCalendar } from '@ng-bootstrap/ng-bootstrap';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
@Component({
selector: 'ngbd-datepicker-range',
templateUrl: './datepicker-range.html',
styles: [`
.custom-day {
text-align: center;
padding: 0.185rem 0.25rem;
display: inline-block;
height: 2rem;
width: 2rem;
}
.custom-day.focused {
background-color: #e6e6e6;
}
.custom-day.range, .custom-day:hover {
background-color: rgb(2, 117, 216);
color: white;
}
.custom-day.faded {
background-color: rgba(2, 117, 216, 0.5);
}
`]
})
export class NgbdDatepickerRange {
hoveredDate: NgbDate;
fromDate: NgbDate;
toDate: NgbDate;
dateForm: FormGroup;
myDateForm: FormGroup;
myFromDate: FormControl;
myToDate: FormControl;
constructor(calendar: NgbCalendar, private formBuilder: FormBuilder) {
this.fromDate = calendar.getToday();
this.toDate = calendar.getNext(calendar.getToday(), 'd', 10);
this.dateForm = this.formBuilder.group({
date: [null, Validators.required],
endDate: [null, Validators.required],
});
this.myDateForm = new FormGroup({
myFromDate: new FormControl(''),
myToDate: new FormControl(''),
});
}
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
this.toDate = date;
let dateObj = new Date(this.toDate.year, this.toDate.month, this.toDate.day);
this.myDateForm.controls.myFromDate.setValue(dateObj);
} else {
this.toDate = null;
this.fromDate = date;
let dateObj = new Date(this.fromDate.year, this.fromDate.month, this.fromDate.day);
this.myDateForm.controls.myToDate.setValue(dateObj);
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date);
}
onSubmit(form: FormGroup) {
console.log(form.value)
}
}
答案 2 :(得分:0)
另一个。您的表格就像
this.dateForm = new FormGroup({
from:new FormControl(),
to:new FormControl()
})
请勿将formControlName放在.html中,并通过以下方式更改onDateSeleccion函数
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
this.dateForm.get('from').setValue(date);
} else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
this.toDate = date;
this.dateForm.get('to').setValue(date);
} else {
this.toDate = null;
this.fromDate = date;
this.dateForm.get('from').setValue(date);
this.dateForm.get('to').setValue(null);
}
}