1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
// <-----html----->
#app
input(type="text" v-model="newTodo")
button(type="button" @click="addTodo") 新增
hr
button(@click=" tag = 'all' ") 全部
button(@click=" tag = 'undone' ") 未完成
button(@click=" tag = 'completed' ") 已完成
hr
ul
li(v-for="item in filterTodo" @dblclick="editTodo(item)")
.div(v-if="item.id != cacheTodo.id")
input(type="checkbox" :id="item.id" v-model="item.checkbox")
label(:for="item.id" :class="{ 'deleteLine' : item.checkbox }") {{item.title}}
button(@click="removeTodo(item)") ×
input(type="text" v-if="item.id == cacheTodo.id" v-model="cacheTitle" @keyup.enter="editDone(item)")
hr
//3.
span 還有{{undoneNum.length}}筆任務未完成
//4.
button(@click="clearTodo") 清除所有任務
// <-----css----->
.deleteLine
text-decoration: line-through
// <-----js----->
let app = new Vue({
el: "#app",
data: {
newTodo: "",
todos: [],
tag: "all",
cacheTodo: {},
cacheTitle: ""
},
methods: {
addTodo: function(){
if(!this.newTodo){
return;
}else{
let id = Date.now();
let title = this.newTodo;
this.todos.push(
{
id: id,
title: title,
checkbox: false
}
);
this.newTodo = "";
}
},
removeTodo: function(item){
let key;
this.todos.forEach(function(a,b){
if(item.id == a.id){
key = b;
}
});
this.todos.splice(key,1);
},
editTodo: function(item){
this.cacheTodo = item;
this.cacheTitle = item.title;
},
editDone: function(item){
item.title = this.cacheTitle;
this.cacheTodo = {};
},
//5.
clearTodo: function(){
if( confirm("確認清除所有項目嗎?") ){
this.todos = [];
}else{
return;
}
}
},
computed:{
filterTodo: function(){
switch(this.tag){
case "all":
return this.todos;
break;
case "undone":
return this.todos.filter(item => item.checkbox == false);
break;
case "completed":
return this.todos.filter(item => item.checkbox == true);
break;
}
},
//1.
undoneNum: function(){
//2.
return this.todos.filter(item => item.checkbox == false);
}
}
});
|