离子-如何在离子选择时显示[selectOptions]中定义的自定义按钮?

时间:2018-11-25 14:28:38

标签: ionic2 ionic3 alert ion-select

我试图通过在[selectOptions]中传递如下选项来在离子选择中定义自定义按钮:

HTML:

<ion-select [(ngModel)]="selectedSkypeUser" [selectOptions]="getSelectOptions()">
    <ion-option *ngFor="let user of skypeUsers" [value]="user.name"> 
        {{user.name}}
    </ion-option>
</ion-select>

TS:

private skypeUserButtons = {
    title: "Skype users",
    subTitle: "Select the user you want to change",
    buttons: [
        {
            text: 'Cancel',
            role: 'cancel',
            handler: () => {}
        },
        {
            text: 'Delete',
            handler: () => {
                this.deleteSkypeUser();
            }
        },
        {
            text: 'Add new user',
            handler: () => {
                this.addSkypeUser();
            }
        }
    ]
};

getSelectOptions() {
    return this.skypeUserButtons;
}

标题和子标题显示正常,但是按钮只是默认按钮。我究竟做错了什么?我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:0)

这不能完全回答问题,但实际上是一种解决方法。 (也许是目前唯一的解决方案。)

我刚刚创建了一个简单的按钮来触发弹出窗口,但这可以是样式设置为看起来像常规选择的元素,并显示选定的选项或您想要的任何内容:

HTML:

<button ion-button icon-left round (click)="showUserDialog()">
    Edit users
</button>

TS:

showUserDialog() {
  let inputs = [];
  let users = this.skypeUsers;
  users.forEach(user => {
      inputs.push({
          type: 'radio',
          label: user.name,
          value: user.name
      });
  });

  let alert = this.alertCtrl.create({
    title: "Select user",
    inputs,
    buttons: [
        {
          text: 'Cancel',
          role: 'cancel'
        },
        {
            'Delete',
            handler: data => {
                // data == selected user.name (value)
            }
        }
    },
    {
        text: 'Add new user',
        handler: () => {
            // Trigger logic to add new user
            this.createNewUser()

            // Return true to close the alert - false to keep it open
            return true;
        }
    }]
  });

  alert.present();
}