无法使用 ng bootstrap 和 angular 6 在同一日历中设置日期
Unable to set the dates in same calendar using ng bootstrap and angular 6
这里我使用 ng bootstrap 和 angular 6 作为内联日期选择器。在这里,我以编程方式设置日期,直到现在它很好,但问题是当我设置最近 7 天的日期时,它必须从日期显示为 02-9-2018,到日期显示为 17-09-2018。问题在于:当我显示日期单独的日历时,"From" 日期显示在日历 1 上,"To" 日期显示在日历 2 上。
现在我想弄明白的是,我怎样才能只显示这一个日历?我的意思是,如果过去 7 天或过去 15 天或一个月在同一个月内,则它不必显示在另一个日历中。如果它不在当前月份,则它必须显示不同日历中的 2 个日期。
下面是我的代码:
.ts代码
dateSelect(event, dpp, dpt) {
var target = event.target || event.srcElement || event.currentTarget;
var idAttr = target.attributes.id;
var value = idAttr.nodeValue;
if (value == 'seven') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "10"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'fifteen') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "02"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'thirty') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "18"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'month') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "01"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
}
.html代码
<ngb-datepicker #dpp [(ngModel)]="OneModel" (navigate)="dates = $event.next"></ngb-datepicker>
<ngb-datepicker #dpt [(ngModel)]="TwoModel" (navigate)="datess = $event.next"></ngb-datepicker>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="seven">Last 7 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="fifteen">Last 15 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="thirty">Last 30 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="month">This Month</button>
我的工作stackblitz
URL
第一步是创建一个自定义模板日,请参阅 https://ng-bootstrap.github.io/#/components/datepicker/examples#customday 并添加一个 class,以便您可以用不同的背景颜色显示一些日子。
您的自定义日期可以像
<ng-template #customDay let-date="date" let-currentMonth="currentMonth" let-selected="selected" let-disabled="disabled" let-focused="focused">
<!--see that you add a class "selected" is a function return true-->
<span class="custom-day" [class.selected]="isSelectedDate(date)" [class.focused]="focused"
[class.bg-primary]="selected" [class.fade]="date.month !== currentMonth" [class.text-muted]="disabled">
{{ date.day }}
</span>
</ng-template>
在你的.css
.custom-day {
text-align: center;
padding: 0.185rem 0.25rem;
border-radius: 0.25rem;
display: inline-block;
width: 2rem;
line-height: 2rem
}
.custom-day:hover, .custom-day.focused {
background-color: #e6e6e6;
}
.fade {
color:Gray;
}
.selected {
background-color:firebrick;
color: white;
}
在你的.ts
//This is the function that make a date change the class
isSelectedDate(date:any)
{
return this.fromDate &&this.toDate?
(date.day==this.fromDate.day && date.year==this.fromDate.year && date.month==this.fromDate.month) ||
(date.day==this.toDate.day && date.year==this.toDate.year && date.month==this.toDate.month):false;
}
那你必须计算出一二日历的时间
更新
我们可以使用 equal、before 和 after 改进函数 isSelectedDate。或者,如果我们想显示所有范围,将函数 isSelectedDate 替换为 isSelectedDayRange
isSelectedDate(date:any)
{
return this.OneModel &&this.TwoModel?
date.equals(this.OneModel) || date.equals(this.TwoModel)
:false;
}
//if you want to "select" a range, you can call to this function
isSelectedDayRange(date:any)
{
return this.OneModel && this.TwoModel?
date.equals(this.OneModel) || date.equals(this.TwoModel) || (date.after(this.OneModel) && date.before(this.TwoModel))
:false
}
这里我使用 ng bootstrap 和 angular 6 作为内联日期选择器。在这里,我以编程方式设置日期,直到现在它很好,但问题是当我设置最近 7 天的日期时,它必须从日期显示为 02-9-2018,到日期显示为 17-09-2018。问题在于:当我显示日期单独的日历时,"From" 日期显示在日历 1 上,"To" 日期显示在日历 2 上。
现在我想弄明白的是,我怎样才能只显示这一个日历?我的意思是,如果过去 7 天或过去 15 天或一个月在同一个月内,则它不必显示在另一个日历中。如果它不在当前月份,则它必须显示不同日历中的 2 个日期。
下面是我的代码:
.ts代码
dateSelect(event, dpp, dpt) {
var target = event.target || event.srcElement || event.currentTarget;
var idAttr = target.attributes.id;
var value = idAttr.nodeValue;
if (value == 'seven') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "10"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'fifteen') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "02"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'thirty') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "18"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
if (value == 'month') {
var fromDate = {
'year': "2018",
'month': "9",
'day': "01"
};
for (let property in fromDate) {
if (fromDate.hasOwnProperty(property)) {
fromDate[property] = +fromDate[property];
}
}
this.OneModel = fromDate;
dpp.navigateTo(this.OneModel);
this.TwoModel = this.calendar.getToday();
dpt.navigateTo(this.TwoModel);
console.log(this.OneModel);
}
}
.html代码
<ngb-datepicker #dpp [(ngModel)]="OneModel" (navigate)="dates = $event.next"></ngb-datepicker>
<ngb-datepicker #dpt [(ngModel)]="TwoModel" (navigate)="datess = $event.next"></ngb-datepicker>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="seven">Last 7 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="fifteen">Last 15 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="thirty">Last 30 Days</button>
<button type="button" class="btn btn-primary btn-space" (click)="dateSelect($event,dpp,dpt)" id="month">This Month</button>
我的工作stackblitz
URL
第一步是创建一个自定义模板日,请参阅 https://ng-bootstrap.github.io/#/components/datepicker/examples#customday 并添加一个 class,以便您可以用不同的背景颜色显示一些日子。
您的自定义日期可以像
<ng-template #customDay let-date="date" let-currentMonth="currentMonth" let-selected="selected" let-disabled="disabled" let-focused="focused">
<!--see that you add a class "selected" is a function return true-->
<span class="custom-day" [class.selected]="isSelectedDate(date)" [class.focused]="focused"
[class.bg-primary]="selected" [class.fade]="date.month !== currentMonth" [class.text-muted]="disabled">
{{ date.day }}
</span>
</ng-template>
在你的.css
.custom-day {
text-align: center;
padding: 0.185rem 0.25rem;
border-radius: 0.25rem;
display: inline-block;
width: 2rem;
line-height: 2rem
}
.custom-day:hover, .custom-day.focused {
background-color: #e6e6e6;
}
.fade {
color:Gray;
}
.selected {
background-color:firebrick;
color: white;
}
在你的.ts
//This is the function that make a date change the class
isSelectedDate(date:any)
{
return this.fromDate &&this.toDate?
(date.day==this.fromDate.day && date.year==this.fromDate.year && date.month==this.fromDate.month) ||
(date.day==this.toDate.day && date.year==this.toDate.year && date.month==this.toDate.month):false;
}
那你必须计算出一二日历的时间
更新 我们可以使用 equal、before 和 after 改进函数 isSelectedDate。或者,如果我们想显示所有范围,将函数 isSelectedDate 替换为 isSelectedDayRange
isSelectedDate(date:any)
{
return this.OneModel &&this.TwoModel?
date.equals(this.OneModel) || date.equals(this.TwoModel)
:false;
}
//if you want to "select" a range, you can call to this function
isSelectedDayRange(date:any)
{
return this.OneModel && this.TwoModel?
date.equals(this.OneModel) || date.equals(this.TwoModel) || (date.after(this.OneModel) && date.before(this.TwoModel))
:false
}