在动态表格的“选择选项”中设置值

时间:2019-11-26 07:58:08

标签: javascript angular angular6 dynamic-forms

我创建了动态表格,该表格有一个下拉菜单。在该下拉菜单中,我想有一个财政周,我想在ts函数中运行循环。

form.ts-

<div class="col-md-9" [ngSwitch]="field.type">
    <dropdown *ngSwitchCase="'dropdown'" [field]="field" [form]="form"></dropdown>
</div>

dropdown.ts-

public field: any[] = [
    {
      type: 'dropdown',
      name: 'fiscalweek',
      label: 'fiscalweek',
      placeholder: 'Fiscal Week',
      required: true,
      options: this.getWeek()
    }
];

getWeek(){
    this.week = 

    [
      { key: 'Select', label: 'ALL'},
      { key: '1', label: '1'},
      { key: '2', label: '2'},
      { key: '3', label: '3'},
      { key: '4', label: '4'},
      { key: '5', label: '5'},
      { key: '6', label: '6'},
      .
      .
      .
      { key: '53', label: '53'}
    ]
    ;

    return this.week;
  }

我想在此getWeek中从1到53运行循环,而不是硬编码。

有人可以建议我该怎么做吗?

1 个答案:

答案 0 :(得分:1)

您可以通过简单的for循环填充week数组

尝试这样:

Working Demo

 getWeek() {
    this.week = [];
    this.week.push({ key: "Select", label: "ALL" });
    for (var i = 1; i <= 53; i++) {
      this.week.push({ key: i.toString(), label: i.toString() });
    }
    return this.week;
 }