gpt4 book ai didi

angular - Angular 4 中的多个日期选择器并且没有 jquery

转载 作者:行者123 更新时间:2023-12-03 09:00:17 25 4
gpt4 key购买 nike

任何人都可以告诉我如何仅使用 Angular 4 和 TypeScript 来实现多个日期选择器。我找到了一个,但它只支持 angular1。

谢谢!!

最佳答案

如何制作基于 ng-bootstrap 日期选择器的多日期选择器?

我几乎没有基于 ng-bootstrap 中的 datepicker-range 示例。您可以在: https://stackblitz.com/edit/angular-he7fsp 中看到最终代码:

正如我评论的那样,我们唯一要做的就是将所选日期存储在数组中并更改自定义日期以在另一个背景中显示所选日期

multi-date-picker.html 与 ng-bootstrap datepicker 范围非常相似

<!-- we not use (select)="onDateSelection($event)"-->
<ngb-datepicker #dp [displayMonths]="2" [dayTemplate]="t">
</ngb-datepicker>

<ng-template #t let-date="date" let-focused="focused">
<!--we use (click) event pass the event and the date-->
<span class="custom-day" (click)="onDateSelection($event,date)"
[class.focused]="focused"
[class.range]="isFrom(date) || isTo(date) || isInside(date) || isHovered(date)"
[class.faded]="isHovered(date) || isInside(date)"
<!--add a class.selected if the date belong to our dates selected -->
[class.selected]="isDateSelected(date)"
(mouseenter)="hoveredDate = date"
(mouseleave)="hoveredDate = null">
{{ date.day }}
</span>
</ng-template>

代码有点复杂,但也不复杂。我只添加函数 isDateSelected、addRangeDate 和 addDate。好吧,我更改了 onDateSelection 函数以在用户按下 Crtl 键时考虑

import {Component,Input,Output,EventEmitter} from '@angular/core';
import {NgbDateStruct, NgbCalendar} from '@ng-bootstrap/ng-bootstrap';

const equals = (one: NgbDateStruct, two: NgbDateStruct) =>
one && two && two.year === one.year && two.month === one.month && two.day === one.day;

const before = (one: NgbDateStruct, two: NgbDateStruct) =>
!one || !two ? false : one.year === two.year ? one.month === two.month ? one.day === two.day
? false : one.day < two.day : one.month < two.month : one.year < two.year;

const after = (one: NgbDateStruct, two: NgbDateStruct) =>
!one || !two ? false : one.year === two.year ? one.month === two.month ? one.day === two.day
? false : one.day > two.day : one.month > two.month : one.year > two.year;

@Component({
selector: 'ngb-multi-date-picker',
templateUrl: 'multi-date-picker.html',
styles: [`
.custom-day {
text-align: center;
padding: 0.185rem 0.25rem;
display: inline-block;
height: 2rem;
width: 2rem;
}
.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);
}
.custom-day.selected{
background-color: rgba(255, 255, 0, .5);

}
`]
})
export class MultiDatePicker {

hoveredDate: NgbDateStruct;

fromDate: NgbDateStruct;
toDate: NgbDateStruct;

_datesSelected:NgbDateStruct[]=[];

@Input()
set datesSelected(value:NgbDateStruct[])
{
this._datesSelected=value;
}
get datesSelected():NgbDateStruct[]
{
return this._datesSelected?this._datesSelected:[];
}

@Output() datesSelectedChange=new EventEmitter<NgbDateStruct[]>();

constructor(calendar: NgbCalendar) {
}

onDateSelection(event:any,date: NgbDateStruct) {
//event is a MouseEvent, we can check if CtrlKey is pressed
event.target.parentElement.blur(); //make that not appear the outline
if (!this.fromDate && !this.toDate) {
if (event.ctrlKey==true) //If is CrtlKey pressed
this.fromDate = date;
else
this.addDate(date);

this.datesSelectedChange.emit(this.datesSelected);

} else if (this.fromDate && !this.toDate && after(date, this.fromDate)) {
this.toDate = date;
this.addRangeDate(this.fromDate,this.toDate);
this.fromDate=null;
this.toDate=null;
} else {
this.toDate = null;
this.fromDate = date;
}
}

addDate(date:NgbDateStruct)
{
let index=this.datesSelected.findIndex(f=>f.day==date.day && f.month==date.month && f.year==date.year);
if (index>=0) //If exist, remove the date
this.datesSelected.splice(index,1);
else //a simple push
this.datesSelected.push(date);
}
addRangeDate(fromDate:NgbDateStruct,toDate:NgbDateStruct)
{
//We get the getTime() of the dates from and to
let from=new Date(fromDate.year+"-"+fromDate.month+"-"+fromDate.day).getTime();
let to=new Date(toDate.year+"-"+toDate.month+"-"+toDate.day).getTime();
for (let time=from;time<=to;time+=(24*60*60*1000)) //add one day
{
let date=new Date(time);
//javascript getMonth give 0 to January, 1, to February...
this.addDate({year:date.getFullYear(),month:date.getMonth()+1,day:date.getDate()});
}
this.datesSelectedChange.emit(this.datesSelected);
}
//return true if is selected
isDateSelected(date:NgbDateStruct)
{
return (this.datesSelected.findIndex(f=>f.day==date.day && f.month==date.month && f.year==date.year)>=0);
}
isHovered = date => this.fromDate && !this.toDate && this.hoveredDate && after(date, this.fromDate) && before(date, this.hoveredDate);
isInside = date => after(date, this.fromDate) && before(date, this.toDate);
isFrom = date => equals(date, this.fromDate);
isTo = date => equals(date, this.toDate);
}

如何工作,我们有

<ngb-multi-date-picker [datesSelected]="datesSelected"
(datesSelectedChange)="change($event)"></ngb-multi-date-picker>

export class AppComponent {
datesSelected:NgbDateStruct[]=[];
change(value:NgbDateStruct[])
{
this.datesSelected=value;
}

关于angular - Angular 4 中的多个日期选择器并且没有 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51184763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com