angular - ngb-datepicler 中更改事件的更改日期
问题描述
我在这里有来自 ngb-datepicker 的范围选择弹出窗口。我从后端得到了初始日期,它工作正常。但是当我更新日期时它在输入字段中没有改变。如何解决此错误?
<div class="form-group">
<div class="input-group">
<input
#dpFromDate
class="form-control"
placeholder="From (YYYY-MM-DD)"
name="dpFromDate"
[(ngModel)]="splitRuleDetail.startDate"
[ngModelOptions]="{updateOn: 'blur'}"
(input)="fromDate = validateInput(fromDate, dpFromDate.value)"
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="datepicker.toggle()"
type="button"
></button>
</div>
</div>
</div>
<div class="form-group ml-2">
<div class="input-group">
<input
#dpToDate
class="form-control"
placeholder="To (YYYY-MM-DD)"
name="dpToDate"
[(ngModel)]="splitRuleDetail.endDate"
[ngModelOptions]="{updateOn: 'blur'}"
(input)="toDate = validateInput(toDate, dpToDate.value)"
/>
<div class="input-group-append">
<button
class="btn btn-outline-secondary calendar"
(click)="datepicker.toggle()"
type="button"
></button>
</div>
</div>
</div>
我添加了 TS 文件,这与给定的 exmaple 相同。但我认为 ts 文件没有问题。
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date && date.after(this.fromDate)) {
this.toDate = date;
} else {
this.toDate = null;
this.fromDate = date;
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return this.toDate && date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || (this.toDate && date.equals(this.toDate)) || this.isInside(date) || this.isHovered(date);
}
validateInput(currentValue: NgbDate | null, input: string): NgbDate | null {
const parsed = this.formatter.parse(input);
return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
}
解决方案
要遵循的步骤
- 使用
(value)
而不是[(ngModel)]
- 消除
[ngModelOptions]="{updateOn: 'blur'}"
- 在日期选择上手动关闭数据选择器
onDateSelection(date: NgbDate) {
if (!this.fromDate && !this.toDate) {
this.fromDate = date;
} else if (this.fromDate && !this.toDate && date && date.after(this.fromDate)) {
this.toDate = date;
this.sub.close(); // close manually
} else {
this.toDate = null;
this.fromDate = date;
}
}
isHovered(date: NgbDate) {
return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
}
isInside(date: NgbDate) {
return this.toDate && date.after(this.fromDate) && date.before(this.toDate);
}
isRange(date: NgbDate) {
return date.equals(this.fromDate) || (this.toDate && date.equals(this.toDate)) || this.isInside(date) || this.isHovered(date);
}
validateInput(currentValue: NgbDate | null, input: string): NgbDate | null {
const parsed = this.formatter.parse(input);
return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
}
推荐阅读
- java - 如何在apache poi-5.0.0中设置类别轴标记间隔和标签间隔?
- java - 使用 groovy 将特定文件移动到另一个文件夹
- go - 有环有向图中的最长路径
- reactjs - 如何测试子组件中是否收到了一些道具
- terraform - How to access the resource ids between two different terraform code
- vue.js - 在进行范围选择时,如何获取引导 vue 表中当前选定行的索引?
- reactjs - 反应三纤维Drei HTML变换模式不起作用
- python - Python检查是否选中了应用程序中的复选框
- java - Spring JPA 是否对已在当前上下文/请求中检索到的缓存实体做任何聪明的事情?
- java - 当并发请求数量增加时,Spring Apache Hadoop 库将文件缓慢保存到 hdfs