离子2.0列表编辑 - 预填充警报字段



我正在我的Ionic 2.0应用程序中的可编辑列表上工作。我把它的时间分别为90%,但是很容易就很容易。

用户单击列表项目的编辑按钮时,它使用以下脚本显示警报。但是,我希望列表项目的当前名称在编辑时会预先填充在警报字段中。我似乎无法弄清楚如何完成此操作。

脚本:

editSupplier(supplier){
     let prompt = this.alertCtrl.create({
        title: 'Edit Supplier',
        inputs: [{
            name: 'title',
            value: // dynamic value should go here
        }],
        buttons: [
            {
                text: 'Cancel'
            },
            {
                text: 'Save',
                handler: data => {
                    let index = this.suppliers.indexOf(supplier);
                    if(index > -1){
                      this.suppliers[index] = data;
                    }
                }
            }
        ]
    });
    prompt.present();       
}

项目代码:

    <ion-item-sliding *ngFor="let supplier of suppliers">
        <ion-item>
            <h2 color="dark">{{supplier.title}}</h2>
        </ion-item>
        <ion-item-options side="right">
          <button ion-button (click)="editSupplier(supplier)" color="attention">
            <ion-icon name="create"></ion-icon>
          </button>
          <button ion-button (click)="deleteSupplier(supplier)" color="danger">
            <ion-icon name="trash"></ion-icon>
          </button>
        </ion-item-options>
    </ion-item-sliding>

好吧,所以我已经弄清楚了。我添加了对父组(供应商)的引用,然后捕获了特定供应商的索引:

let currentIndex = this.suppliers.indexOf(supplier);
然后,我用以下方式替换了代码的"值"行:
value: this.suppliers[currentIndex].title

和鲍勃是你的叔叔,警报字段已预先填充。

尝试

editSupplier(supplier){
 let prompt = this.alertCtrl.create({
    title: 'Edit Supplier',
    inputs: [{
        name: 'title',
        value: this.supplier.title
    }],
    buttons: [
        {
            text: 'Cancel'
        },
        {
            text: 'Save',
            handler: data => {
                let index = this.suppliers.indexOf(supplier);
                if(index > -1){
                  this.suppliers[index] = data;
                }
            }
        }
    ]
});
prompt.present();       
}

最新更新