Reputation: 400
How do you use a array to populate a alert list. For example if the array had a bunch of names.
array = [john,dixy,tom,jared];
I want the alert to pop up and show those names so they can be selected from. I am working with ionic 2.
Upvotes: 3
Views: 6607
Reputation: 44669
Since the alert
is created by using an object with the options, we can use that object to create the radio buttons with the names of the array.
import { Component } from "@angular/core";
import { AlertController } from "ionic-angular/index";
@Component({
templateUrl:"home.html"
})
export class HomePage {
private names: Array<string>;
constructor(private alertCtrl: AlertController) {
this.names = [ 'john', 'dixy', 'tom', 'jared'];
}
presentConfirm() {
// Object with options used to create the alert
var options = {
title: 'Choose the name',
message: 'Which name do you like?',
buttons: [
{
text: 'Cancel',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
},
{
text: 'Ok',
handler: data => {
console.log(data);
}
}
]
};
options.inputs = [];
// Now we add the radio buttons
for(let i=0; i< this.names.length; i++) {
options.inputs.push({ name : 'options', value: this.names[i], label: this.names[i], type: 'radio' });
}
// Create the alert with the options
let alert = this.alertCtrl.create(options);
alert.present();
}
}
Hope this helps :)
Upvotes: 12