I came across many similar questions on stackoverflow. But trust me I implement at least ten solutions but I'm not getting correct results and also that most of them are using jquery
or javascript
while I've to use typescript
. I'm creating a simple month range picker with no other rocket science features. After implementing one solution after the other my actual code has become very dirty. So I'm creating a fresh minimal project on stackblitz, here. Also I'm giving my code before making any changes.
My monthpicker.component.html:
<div class="dropdown">
<span>
<input placeholder="{{sampletext}}, {{inputText}}">
</span>
<div class="my-table-div dropdown-content">
<div class="year-div">
<input type="number" value="2018" min="2018" max="2024" [(ngModel)]="inputText">
</div>
<hr>
<div *ngFor="let row of matrix" class="my-table">
<span *ngFor="let x of row">
<span class="month-name" (click)="onClick(x)">{{ x }}</span>
</span>
</div>
</div>
</div>
monthpicker.component.ts
import ...;
@Component({
...
})
export class MonthpickerComponent implements OnInit {
dt = new Date( );
arr = [
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec'
];
sampletext = ""+this.arr[this.dt.getMonth()];
inputText :number = this.dt.getFullYear();
clickCount: number=0;
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));
constructor() { }
onClick(x) {
this.clickCount++;
console.log("Month: " + x);
this.sampletext = x;
console.log("Year: " + this.inputText);
console.log("Clicked "+this.clickCount +" times.");
if(this.clickCount%2==0) {
this.sampletext+=" " +this.sampletext+", "+this.inputText;
}
}
ngOnInit() {
}
}
Please suggest a solution.
PS: I don't want to use any third party library. Not even bootstrap. I've to make it from the scratch.