3

What is the best way to bind drop downs values with numbers from 1 to 100 using a loop using Angular2?

For a limited number of values I am using Ngprime dropdown, but how do I achieve this for 'n' number of values?

Template:

<p-dropdown [options]="tests" [(ngModel)]="selectedCar" [style]="{'width':'150px'}" editable="editable" placeholder="Select a Brand"></p-dropdown>

Component:

this.tests = [];
this.test.push({label: 'Audi', value: 'Audi'});

Can anyone guide me?

4 Answers 4

3

A dropdown with options from 1 to 100 in Angular 2 would be something like this:

In component:

export class DropDownClass {
  constructor() {
    this.numbers = new Array(100).fill(0).map((x,i + 1)=>i); // [1,2,3,4,...,100]
  }
}

In template:

<select name="my-dropdown" [(ngModel)]="myDropdownModel">
    <option *ngFor="let number of numbers" [value]="number">{{number}}</option>
</select>
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the response, I am getting an error as 'Supplied parameters do not match any signature of call target'.(method) Array<any>.fill(value: any, start?: number, end?: number): any[]...Should i specify start and end value?
@AnnMary No problem. But it worked for you? Did it solve your issue/question? Did you encounter any difficulties? Have a nice day! :D
error_handler.js:54 EXCEPTION: Uncaught (in promise): TypeError: Cannot read property 'fill' of undefined TypeError: Cannot read property 'fill' of undefined
What is the reason of typeerror I am facing here,TypeError: Cannot read property 'fill' of undefined
If you don't use new you are not instancing/creating the Array. And therefore the array doesn't exist and the compiler throws error because you try to use fill() on a non existing element.
|
1
  public quantities: Array<number> = [];
 getQuantity() {
    for (let i = 1; i <= 100; i++) {
      this.quantities.push(i)
    }
  }


<mat-form-field>
        <mat-select placeholder="QTY">
            <mat-option *ngFor="let quantity of quantities" [value]="quantity">
                  {{quantity}}
            </mat-option>
       </mat-select>
 </mat-form-field>

Comments

0

Working Code

In template:

<label for="percentage">Percentage</label>
  <p-dropdown formControlName="PercentageAbsence" [options]="numbers" [style]="{'width':'200px'}">
  </p-dropdown>

In component:

 import { SelectItem } from 'primeng/primeng';
  export class Test{
   numbers: SelectItem[] = [];
   constructor(){
        Array(100).fill(0).map((x, i) => {
        this.numbers.push({ label: `${i + 1}`, value: i + 1 })
    });
  }
}

Comments

0

In component: (YourComponent.ts)

export class YourComponent {
itemQuantity = []; // instantiates the object to hold array of numbers

public ngOnInit() {
    this.itemQuantity = Array(5).fill(0).map((x,i)=>i+1); // fills array with 5 incremental entries starting from 1 (hence the "i+1"). To start at zero remove the "+1"
}

}

In template: (YourComponent.html)

<option *ngFor="let number of itemQuantity" value="{{number}}">{{number}}</option>

The result is a dropdown list with five entries numbered 1 to 5

"value="{{number}}" is optional, in case you want different text and value for each select item, e.g. to get Number 1 you can use Number {{number}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.