简体   繁体   中英

Set Value in Select Option of dynamic form

I have created dynamic form, in that form there is one dropdown. In that dropdown I want to have fiscal week for that I want to run loop inside ts function.

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;
  }

I want to run a loop inside this getWeek from 1 to 53 instead of hardcoding.

Can someone please suggest me how to do that ?

You can populate week array by a simple for loop

Try like this:

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;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM