【问题标题】:How to create a month range picker in Angular如何在 Angular 中创建月份范围选择器
【发布时间】:2019-12-01 05:42:27
【问题描述】:

我在 stackoverflow 上遇到了许多类似的问题。但相信我,我实施了至少十个解决方案,但我没有得到正确的结果,而且他们中的大多数都在使用jqueryjavascript,而我必须使用typescript。我正在创建一个没有其他火箭科学功能的简单月份范围选择器。在一个接一个地实施一个解决方案之后,我的实际代码变得非常脏。所以我在 stackblitz 上创建了一个全新的最小项目,here。另外,在进行任何更改之前,我会提供我的代码。

我的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() {
  }
}

请提出解决方案。

PS:我不想使用任何第三方库。甚至没有引导程序。我必须从头开始。

【问题讨论】:

  • 我没有得到你的确切要求
  • @RajSan 我想用角度创建一个月份范围选择器。
  • 您想使用 1 个输入字段制作月份范围选择器吗?
  • 你想要两个月,从一年到一年?
  • @RajSan.Yes。确切地。但我会在一年后处理其他一些组件。现在让它硬编码。现在我想在同一个文本字段中显示该范围。请看一下我的 stackblitz,我刚刚做了一些更改。我非常感谢你的帮助。 :-)

标签: javascript angular typescript monthcalendar


【解决方案1】:

您可以简单地使用两个选择菜单或自动完成来选择月份和年份。例如,你可以有一个这样的简单代码:

<form [formGroup]="MonthYearFormGroup">
  <h4>Select a month here:</h4>
  <mat-form-field>
    <mat-label>Month</mat-label>
    <mat-select formControlName="selected_month">
      <mat-option *ngFor="let month of monthList" [value]="month.value">
        {{month.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>

  <h4>Select a year here:</h4>
  <mat-form-field>
    <mat-label>Year</mat-label>
    <mat-select formControlName="selected_year">
      <mat-option *ngFor="let year of yearList" [value]="year.value">
        {{year.viewValue}}
      </mat-option>
    </mat-select>
  </mat-form-field>
  <button (click)="save()">Save Selected Date</button>
</form>
import {Component} from '@angular/core';
import { FormBuilder, FormGroup, Validators, FormControl } from '@angular/forms';

export interface MonthYear {
  value: string;
  viewValue: string;
}

@Component({
  selector: 'select-overview-example',
  templateUrl: 'select-overview-example.html',
  styleUrls: ['select-overview-example.css'],
})
export class SelectOverviewExample {

  public MonthYearFormGroup: FormGroup();


 yearList: MonthYear[] = Array(50).fill(1).map( (_, index) => { return { value: String(index + 2019), viewValue: String(index + 2019) } })

  monthList: MonthYear[] = [
    {value: '1', viewValue: 'Jan'},
    {value: '2', viewValue: 'Feb'},
    {value: '3', viewValue: 'Mar'},
    {value: '4', viewValue: 'Apr'},
    {value: '5', viewValue: 'May'},
    {value: '6', viewValue: 'Jun'},
    {value: '7', viewValue: 'Jul'},
    {value: '8', viewValue: 'Aug'},
    {value: '9', viewValue: 'Sep'},
    {value: '10', viewValue: 'Oct'},
    {value: '11', viewValue: 'Nov'},
    {value: '12', viewValue: 'Dec'},
  ];

  constructor(
    private _formBuilder: FormBuilder,
  ) { }

  ngOnInit() {
    this.MonthYearFormGroup= this._formBuilder.group({
      selected_month: [{ value: '', required: true }],
      selected_year: [{ value: '', required: true }],
  });

  save() {
   //Your process here...
   console.log(this.MonthYearFormGroup.value.selected_month);
   console.log(this.MonthYearFormGroup.value.selected_year);
  }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-07
相关资源
最近更新 更多