我正在学习Angular
。我从头开始创建了一个自定义month-picker
。它选择一个月,无日期和无周的范围。我想强调选择的月份范围。您可以想到primeng
月份选择器,但范围较大。我尝试了this,this和许多其他解决方案,但失败了。这是我的代码:
monthpicker.component.html
<div class="my-table-div dropdown-content">
...
<br>
<div *ngFor="let row of matrix" class="my-table">
<span *ngFor="let x of row">
<span class="month-name" (click)="onClick(x)" [class.extraStyle]="someProperty">
{{ x }}
</span>
</span>
</div>
</div>
monthpicker.component.ts
...
clickCount: number =0; /*to determine lower bound and upper bound selection*/
someProperty:boolean=false;
flag:boolean= false;
...
arr = [
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'JuL', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'
];
...
n = 4;
matrix = Array
.from({ length: Math.ceil(this.arr.length / this.n) }, (_, i) => i)
.map(i => this.arr.slice(i * this.n, i * this.n + this.n));
...
onClick(x) {
this.clickCount++;
console.log("Month: " + x);
if(this.clickCount%2!=0) {
this.lowerMonth= x;
this.lowerYear=this.year;
}
console.log("Year: " + this.lowerYear);
console.log("Clicked "+this.clickCount +" times.");
if(this.clickCount%2==0) {
this.upperMonth=" "+x;
this.upperYear =this.year;
if(this.flag) {
this.someProperty=true;
}
else {
this.someProperty=false;
}
}
}
这是我正在应用的CSS monthpicker.component.css
.extraStyle {
background-color: #1474A4 !important;
color: white !important;
text-align: center !important;
}
请注意,即使选择的时间范围是从1月到7月,CSS也适用于所有月份。
有什么方法可以将CSS仅应用于选定的index
数字。因为索引从0到11开始,并且每个月都是固定的。好的,这就是我的想法,对不起,我可能完全错了。请纠正我并帮助我实施。
答案 0 :(得分:1)
您可以在行数组中添加isSelected键。 像这样:
matrix: any = Array.from(
{ length: Math.ceil(this.arr.length / this.n) },
(_, i) => i
).map(i =>
this.arr.slice(i * this.n, i * this.n + this.n).map(x => ({
monthName: x,
isSelected: false
}))
);
不是像这样在html中使用isSelected键:
<div *ngFor="let row of matrix" class="my-table">
<span *ngFor="let x of row">
<span class="month-name" (click)="onClick(x); x.isSelected = !x.isSelected" [ngClass]="{'extraStyle' : x.isSelected }">
{{ x.monthName }}
</span>
</span>
</div>