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
| import React, { Component } from 'react'; import { stringify } from 'querystring'; import storage from './model/storage.js';
class TodoList extends Component { constructor(props){ super(props); this.state={ msg:'todolist:', list:[ {title:'录制ionic',checked:true}, {title:'录制nodejs',checked:false}, {title:'录制egg.js',checked:true}, {title:'录制vue',checked:false} ] } }
addList=(e)=>{ if(e.keyCode==13){ let tempList=this.state.list; let title=e.target.value; tempList.push({title:title,checked:false}) this.setState({ list:tempList }); e.target.value=''; storage.set('todolist',tempList); } }
handleCheck=(key)=>{ let tempList=this.state.list; tempList[key].checked=!tempList[key].checked; this.setState({ list:tempList }) storage.set('todolist',tempList); }
dellist=(key)=>{ let tempList=this.state.list; tempList.splice(key,1) this.setState({ list:tempList }) storage.set('todolist',tempList); }
componentDidMount(){ var todolist=JSON.parse(localStorage.getItem('todolist')); if(todolist){ this.setState({ list:todolist }) } }
render() { return ( <div> {this.state.msg}<input onKeyUp={this.addList} /><br/> <hr/> <h2>待办事项:</h2> <ul>{ this.state.list.map((value,key)=>{ if(value.checked==false){ return( <li key={key}> <input type='checkbox' checked={value.checked} onChange={this.handleCheck.bind(this,key)} /> {value.title}-- <button onClick={this.dellist.bind(this,key)}>删除</button> </li> ) } }) }</ul><br/>
<hr/> <h2>已完成事项:</h2> <ul>{ this.state.list.map((value,key)=>{ if(value.checked==true){ return( <li key={key}> <input type='checkbox' checked={value.checked} onChange={this.handleCheck.bind(this,key)} /> {value.title}-- <button onClick={this.dellist.bind(this,key)}>删除</button> </li> ) } }) }</ul><br/> </div> ); } } export default TodoList;
|