selectCompleted function

This commit is contained in:
imelilabourne 2020-09-21 09:09:46 +08:00
parent 8ebdedea6b
commit 7206da41eb
4 changed files with 50 additions and 32 deletions

View File

@ -1,10 +1,16 @@
{ {
"tasks": [ "tasks": [
{ {
"title": "ty po sa milktea hahaha", "title": "coffee",
"completed": false, "completed": false,
"editing": false, "editing": false,
"id": 2 "id": 2
},
{
"title": "sleep",
"completed": false,
"editing": false,
"id": 3
} }
] ]
} }

View File

@ -4,7 +4,7 @@
<div class="header"> <div class="header">
<h1>TODO LIST <i class="fa fa-pencil"></i></h1> <h1>TODO LIST <i class="fa fa-pencil"></i></h1>
</div> </div>
<div class="info selectall"><label class="checkbox-inline"><input type="checkbox" (change)="selectAll()">Select All</label></div> <div class="info selectall"><label class="checkbox-inline"><input type="checkbox" (change)="selectAll($event)">Select All</label></div>
<div class="inputDiv"> <div class="inputDiv">
<input type="text" class="form-control addInput" placeholder="Things you want to do today?" [(ngModel)]="taskTitle" (keyup.enter) = "addTask()"> <input type="text" class="form-control addInput" placeholder="Things you want to do today?" [(ngModel)]="taskTitle" (keyup.enter) = "addTask()">
</div> </div>
@ -29,7 +29,7 @@
</ng-template> </ng-template>
<div> <div>
<button class="btn btn-danger listBtn" (click)="deleteTask(task)"><i class="fa fa-trash"> </i> Del</button> <button class="btn btn-danger listBtn" (click)="deleteTask(task.id)"><i class="fa fa-trash"> </i> Del</button>
</div> </div>
</li> </li>
</ul> </ul>
@ -43,10 +43,9 @@
<div>{{ remaining()}} uncompleted tasks</div> <div>{{ remaining()}} uncompleted tasks</div>
</div> </div>
<div *ngIf="atleastOneCompleted()"> <div *ngIf="atleastOneCompleted()">
<button class="btn btn-warning btn-block" (click) ="clearCompleted(tasks)">Clear Completed</button> <button class="btn btn-warning btn-block" (click) ="deleteAllTask()">Clear Completed</button>
<div class="alert alert-warning">You've selected an item</div> <div class="alert alert-warning">You've selected an item</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -29,6 +29,7 @@ export class TodoList{
tasks:Tasks[] = []; tasks:Tasks[] = [];
beforeEditing: string; beforeEditing: string;
today: number = Date.now(); today: number = Date.now();
completed: Tasks[] = [];
constructor(private todoService: TodoService){ constructor(private todoService: TodoService){
@ -45,7 +46,6 @@ export class TodoList{
} }
addTask(){ addTask(){
//restricting empty string
if(this.taskTitle.trim().length === 0){ if(this.taskTitle.trim().length === 0){
return; return;
} }
@ -66,8 +66,6 @@ export class TodoList{
editing: false editing: false
}) })
this.taskTitle =''; this.taskTitle ='';
// this.taskId++;
} }
toggleEdit(event: Tasks){ toggleEdit(event: Tasks){
@ -87,9 +85,6 @@ export class TodoList{
})); }));
} }
console(){
console.log("try function");
}
doneEditing(task: Tasks):void{ doneEditing(task: Tasks):void{
if(task.title.trim().length === 0){ if(task.title.trim().length === 0){
@ -110,28 +105,41 @@ export class TodoList{
atleastOneCompleted(): boolean{ atleastOneCompleted(): boolean{
return this.tasks.filter(task => task.completed).length > 0; return this.tasks.filter(task => task.completed).length > 0;
} }
//delete function //delete function
deleteTask(tasks: Tasks){ deleteTask(taskId){
this.tasks = this.tasks.filter(task => task.id !== tasks.id); this.tasks = this.tasks.filter(task => task.id !== taskId);
this.todoService.deleteTask(tasks) this.todoService.deleteTask(taskId)
.subscribe(data => this.tasks.filter(task => { .subscribe(data => this.tasks.filter(task => {
return task.id !== data; return task !== data;
})) }))
}
deleteAllTask(){
const selectedProducts = this.tasks.filter(product => product.completed).map(p => p.id);
console.log (selectedProducts);
selectedProducts.forEach(value => {
this.todoService.deleteTask(value)
.subscribe(res => {
this.tasks = this.tasks.filter(task => !task.completed);
});
});
// if(selectedProducts && selectedProducts.length === 1) {
// }
// else{
// this.todoService.deleteAllTask(selectedProducts)
// .subscribe(data => console.log("more than 1 elements"));
// }
} }
clearCompleted(tasks: Tasks){ selectAll(event):void{
// this.tasks = this.tasks.filter(task => !task.completed); this.tasks.forEach(x => x.completed = event.target.checked)
// this.todoService.deleteAllTask(tasks)
// .subscribe(data => this.tasks.filter(task => {
// return task.completed !== data;
// }))
}
selectAll():void{
this.tasks.forEach(task => task.completed =
(<HTMLInputElement>event.target).checked);
} }
} }

View File

@ -26,12 +26,17 @@ export class TodoService{
.pipe(map((response: any) => response.json())); .pipe(map((response: any) => response.json()));
} }
deleteTask(task: Tasks){ deleteTask(id:number){
return this.http.delete(URL + '/' + task.id); return this.http.delete(URL + '/' + id);
} }
deleteAllTask(task: Tasks){ deleteAllTask(id: number[]){
return this.http.delete(URL + '/' + task.id); return this.http.delete<Tasks[]>(URL + '/' + id);
// .pipe(map((res:any) => {
// res.json();
// }))
} }