【发布时间】:2020-11-30 07:21:04
【问题描述】:
我正在尝试使用 angular 从头开始创建日历视图。我已经完成了动态显示该月的所有日期。我的问题是我需要使用我在互联网上搜索过的 Moment 为周末日期(例如周五和周六)显示不同的背景颜色,但我找不到任何资源来帮助我解决这个问题。除此之外,我需要在第一行中显示日期,而不是在日历视图中显示为单独的行。 我的 appcomponent.html 代码
<div class="flex-container flex-center">
<button
mat-icon-button
matTooltip="Previous Month"
[matTooltipPosition]="'above'"
class="iconsSize ml-1 my-auto"
(click)="previousmonth()"
>
<mat-icon>
chevron_left
</mat-icon>
</button>
<h4><b>{{date.format('MMMM ')}}{{date.format('YYYY ')}}</b></h4>
<span class="material-icons" style="padding: 7px; font-size: 20px;">
event
</span>
<button
mat-icon-button
matTooltip="Next Month"
[matTooltipPosition]="'above'"
class="iconsSize ml-1 my-auto"
(click)="nextmonth()"
>
<mat-icon>
chevron_right
</mat-icon>
</button>
</div>
<div class="container-fluid">
<div class="flex-container flex-wrap p-3 m-2">
<div *ngFor ="let day of daysarr">
<div class="calendar-days flex-container flex-center">{{day}}</div>
</div>
</div>
</div>
我的 appcomponent.ts 文件
export class AppComponent implements OnInit {
public date = moment();
public daysarr;
constructor() { }
ngOnInit(): void {
this.daysarr = this.createCalendar(this.date);
}
createCalendar(month)
{
let firstDay = moment(month).startOf('M');
let days = Array.apply(null,{length: month.daysInMonth()+1})
.map(Number.call, Number)
.slice(1);
for(let n=0; n < firstDay.weekday(); n++)
{
days.unshift(null);
}
return days;
}
public nextmonth()
{
this.date.add(1,'M');
this.daysarr = this.createCalendar(this.date);
}
public previousmonth()
{
this.date.subtract(1,'M');
this.daysarr = this.createCalendar(this.date);
}
}
appcomponent.css
.calendar-days {
width: 155px;
height: 120px;
background: #fff;
border: 1px solid #ccc;
padding: 3px;
}
.flex-container {
display: -webkit-flex;
display: flex;
}
.flex-center {
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
【问题讨论】:
标签: angular typescript momentjs