Updating and Deleting — Ionic Part 3

Sameeha Rahman
Jul 22 · 5 min read

Step 1 — Change that look

<ion-item *ngFor="let todo of taskList; let i = index">
{{todo}}
<div class="item-note" item-end>
<button ion-button clear (click)="updateTask(i)">
<ion-icon name="create"></ion-icon>
</button>
<button ion-button clear (click)="deleteTask(i)">
<ion-icon name="trash"></ion-icon>
</button>
</div>
</ion-item>

Step 2 — Behind the scenes

deleteTask(index){
this.taskList.splice(index, 1);
}
import { NavController, AlertController } from 'ionic-angular';
constructor(public navCtrl: NavController, public alertCtrl: AlertController)
updateTask(index) {
let alert = this.alertCtrl.create({
title: 'Update Task?',
message: 'Type in your new task to update.',
inputs: [{ name: 'editTask', placeholder: 'Task' }],
buttons: [{ text: 'Cancel', role: 'cancel' },
{ text: 'Update', handler: data => {
this.taskList[index] = data.editTask; }}]
});
alert.present();
}

Step 3 — The unveiling

Bonus!! — Auto-focus

Input without autofocus
import { Component, ViewChild } from '@angular/core';
@ViewChild('taskInput') input;
ionViewDidLoad(){
setTimeout(() => {
this.input.setFocus();
},350);
}
Input with autofocus
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade