-
Notifications
You must be signed in to change notification settings - Fork 1
/
C07_03.js
114 lines (94 loc) · 2.96 KB
/
C07_03.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React, { Component } from 'react';
import storage from '../model/storage'
class C07_03 extends Component {
constructor(props) {
super(props);
this.state = {
todoList:[
]
};
}
componentDidMount(){
let todoListStorage=storage.getTodo();
if(todoListStorage){
this.setState({
todoList:todoListStorage
})
}
}
add=()=>{
let tempList=this.state.todoList;
tempList.push({
title:this.refs.newTodo.value,
checked:false
})
this.setState({
todoList:tempList
})
storage.setTodo(tempList);
this.refs.newTodo.value=""
}
onHuiche=(e)=>{
if(e.keyCode==13){
this.add()
}
}
del=(key)=>{
let tempList=this.state.todoList;
tempList.splice(key,1);
this.setState({
todoList:tempList
})
storage.setTodo(tempList);
}
doneTode=(key)=>{
let temp=this.state.todoList;
temp[key].checked=!(temp[key].checked);
this.setState({
todoList:temp
})
storage.setTodo(temp);
}
render() {
return (
<div>
<div>
<h2>Todo List</h2>
<input ref='newTodo' onKeyUp={this.onHuiche} /> <button onClick={this.add} >添加</button>
<hr/>
</div>
<div>
<p>未完成</p>
{
this.state.todoList.map((value,key)=>{
if(value.checked===false){
return(
<li key={key} >
<input type='checkbox' onChange={this.doneTode.bind(this,key)} />
{value.title}
---<button onClick={this.del.bind(this,key)}>删除</button>
</li>
)
}
})
}
<p>已完成</p>
{
this.state.todoList.map((value,key)=>{
if(value.checked){
return(
<li key={key} >
<input type='checkbox' checked={true} onChange={this.doneTode.bind(this,key)} />
{value.title}
---<button onClick={this.del.bind(this,key)}>删除</button>
</li>
)
}
})
}
</div>
</div>
);
}
}
export default C07_03;