I'm learning Angular
. I've created a custom month-picker
from the scratch. It selects a range of months, no dates and no weeks. I wanted to highlight the range of months that'd been selected. You can think of primeng
month picker but with range. I tried this, this and many other solutions but I failed. Here is my code:
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;
}
}
}
And here is the css I'm applying monthpicker.component.css
.extraStyle {
background-color: #1474A4 !important;
color: white !important;
text-align: center !important;
}
See, CSS is applied to all the months even though the selection is from Jan to Jul only.
Is there any way that I can apply css to selected index
numbers only. Because index is starting from 0 to 11 and is kind of fixed for each month. Ok, This is what I thought, I'm sorry I may be completely wrong. Please correct me and help me implement that.