Skip to content

Commit 8f9e472

Browse files
committed
ステップ⑤ タスクの変更・削除をAPIと組み合わせて実装する
1 parent 39ae3ab commit 8f9e472

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/app/task-list/task-list.page.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
<ion-content>
1111
<ion-list>
12-
<ion-item *ngFor="let t of tasks">
12+
<ion-item *ngFor="let t of tasks; let i = index" [button]="true"
13+
(click)="changeTask(i)">
1314
<ion-label>{{t.name}}</ion-label>
1415
</ion-item>
1516
</ion-list>

src/app/task-list/task-list.page.ts

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { ActionSheetController, AlertController } from '@ionic/angular';
23

34
@Component({
45
selector: 'app-task-list',
@@ -10,7 +11,10 @@ export class TaskListPage implements OnInit {
1011
{ name: 'タスク1' },
1112
{ name: 'タスク2' },
1213
];
13-
constructor() { }
14+
constructor(
15+
public actionSheetController: ActionSheetController,
16+
public alertController: AlertController,
17+
) { }
1418

1519
ngOnInit() {
1620
}
@@ -21,4 +25,60 @@ export class TaskListPage implements OnInit {
2125
}
2226
}
2327

28+
async changeTask(index: number) {
29+
const actionSheet = await this.actionSheetController.create({
30+
header: 'タスクの変更',
31+
buttons: [
32+
{
33+
text: '削除',
34+
role: 'destructive',
35+
icon: 'trash',
36+
handler: () => {
37+
this.tasks.splice(index, 1);
38+
localStorage.tasks = JSON.stringify(this.tasks);
39+
}
40+
}, {
41+
text: '変更',
42+
icon: 'create',
43+
handler: () => {
44+
this._renameTask(index);
45+
}
46+
}, {
47+
text: '閉じる',
48+
icon: 'close',
49+
role: 'cancel',
50+
handler: () => {
51+
console.log('Cancel clicked');
52+
}
53+
},
54+
]
55+
});
56+
actionSheet.present();
57+
}
58+
59+
async _renameTask(index: number) {
60+
const prompt = await this.alertController.create({
61+
header: '変更後のタスク',
62+
inputs: [
63+
{
64+
name: 'task',
65+
placeholder: 'タスク',
66+
value: this.tasks[index].name
67+
},
68+
],
69+
buttons: [
70+
{
71+
text: '閉じる'
72+
},
73+
{
74+
text: '保存',
75+
handler: data => {
76+
this.tasks[index] = { name: data.task };
77+
localStorage.tasks = JSON.stringify(this.tasks);
78+
}
79+
}
80+
]
81+
});
82+
prompt.present();
83+
}
2484
}

0 commit comments

Comments
 (0)