How to populate the alert input with arrays?

I have seen the document on how to create an alert with radio button.

  async presentAlertRadio() {
    const alert = await this.alertController.create({
      header: 'Radio',
      inputs: [
        {
          name: 'radio1',
          type: 'radio',
          label: 'Radio 1',
          value: 'value1',
          checked: true
        },
        {
          name: 'radio2',
          type: 'radio',
          label: 'Radio 2',
          value: 'value2'
        },
        {
          name: 'radio3',
          type: 'radio',
          label: 'Radio 3',
          value: 'value3'
        },
        {
          name: 'radio4',
          type: 'radio',
          label: 'Radio 4',
          value: 'value4'
        },
        {
          name: 'radio5',
          type: 'radio',
          label: 'Radio 5',
          value: 'value5'
        },
        {
          name: 'radio6',
          type: 'radio',
          label: 'Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 Radio 6 ',
          value: 'value6'
        }
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('Confirm Cancel');
          }
        }, {
          text: 'Ok',
          handler: () => {
            console.log('Confirm Ok');
          }
        }
      ]
    });

    await alert.present();
  }

But I want to know is there a way to populate inputs with an array?
For example, if I have array like this:

names = [ ‘john’, ‘dixy’, ‘tom’, ‘jared’]

how can I populate the alert input with this array instead of typing one by one?

Can someone help me?

You could do something like:

names = [ ‘john’, ‘dixy’, ‘tom’, ‘jared’] // with other necessary attributes like name,type, label etc

 const alert = await this.alertController.create({
      header: 'Radio',
      inputs: names,
      buttons: [
          // your buttons logic here
        ]

alert.present();

You could also use addInput() if you are on ionic version 3. Not sure about version 4 though.

async presentAlertRadio() {
    let input={data:[]};
    for (let i = 1; i < 21; i++) {
      input.data.push({name:"radio"+i,type: 'radio',label:"Radio"+i,value: "value"+i})
    }
    console.log(input);
    const alert = await this.alertController.create({
      header: 'Radio',
      inputs: input.data,
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          cssClass: 'secondary',
          handler: () => {
            console.log('Confirm Cancel');
          }
        }, {
          text: 'Ok',
          handler: (data) => {
            console.log(data);
          }
        }
      ]
    });
    await alert.present();
  }
2 Likes