這個功能實現的步驟如下所示:
最終實現全選和反選,代碼如下所示:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react.js"></script> <script src="./js/react-dom.js"></script> <script src="./js/browser.min.js"></script> </head> <body> <div id="dome"></div> <script type="text/babel"> //搜索區域 var Ck = React.createClass({ //處理搜索事件的函數 handleKey:function(e){ //alert('test'); //判斷回車enter鍵才處理,keyCode13==回車鍵 if(e.keyCode == 13){ //alert('test'); //如果搜索內容是空的讓他不走了 if(!e.target.value) return; //否則添加任務了 var ckcon = { text : e.target.value, isDown: false } //利用屬性完成 this.props.addCkcon(ckcon); //清空搜索框的內容 e.target.value = ''; } }, render:function(){ return( <div> <input type="text" placeholder="你要干嘛?" onKeyUp={this.handleKey} /> </div> ); } }); //列表項區域 var Lists = React.createClass({ handleClick:function(){ //alert('test'); this.props.deleteCkcon(this.props.index); }, //處理單選框的變化事件 handleChange:function(e){ //修改那個任務,修改的值是什么 this.props.changeStatus(this.props.index,e.target.checked); }, render:function(){ return( <li> <label> <input type="checkbox" checked={this.props.todo.isDown} onChange={this.handleChange} /> {this.props.todo.text} </label> <button onClick={this.handleClick}>刪除</button> </li> ); } }); //列表框區域 var Ul = React.createClass({ render:function(){ //保存this指針 var _this = this; return( <ul> { this.props.todos.map(function(item,index){ return <Lists todo={item} key={index} index={index} deleteCkcon={_this.props.deleteCkcon} changeStatus={_this.props.changeStatus} /> }) } </ul> ); } }); //狀態組建 var Status = React.createClass({ handleClick:function(){ //alert('test'); //刪除已完成的 this.props.deleteDown(); }, handleChange:function(e){ this.props.changeAllStatus(e.target.checked); }, render:function(){ return( <div> <input type="checkbox" checked={this.props.isAllChecked} onChange={this.handleChange} /> {this.props.countDown} 已完成 / {this.props.total} 總數 <button onClick={this.handleClick}>清除已完成</button> </div> ); } }); //總組建 var Zong = React.createClass({ getInitialState:function(){ return { todos :[ {text:'6點起床',isDown:true}, {text:'7點出門',isDown:true}, {text:'8點吃早飯',isDown:false}, {text:'9點上班',isDown:true}, {text:'12點下班',isDown:false} ], isAllChecked: false } }, addCkcon:function(todo){ //接收到用戶的添加的內容然后鋪push過去即可 this.state.todos.push(todo); //然后更新state this.setState({ todos : this.state.todos }); }, //處理刪除任務 deleteCkcon:function(index){ //用函數splice來刪除掉指定的數組元素 this.state.todos.splice(index,1); //刪除完成后來更新下頁面的內容 this.setState({ todos : this.state.todos }); }, //任務單選框的屬性 changeStatus:function(index,isDown){ this.state.todos[index].isDown = isDown this.setState({ todos : this.state.todos }) this.checkAll(); }, //如果所有任務都完成了,就設置isAllChecked都為true checkAll:function(){ if(this.state.todos.every(function(todo){ return todo.isDown})){ this.setState({ isAllChecked:true }); }else{ this.setState({ isAllChecked:false }); } }, //統計總的任務個數 total:function(){ return this.state.todos.length || 0 }, //統計已完成的 countDown:function(){ var arr = this.state.todos.filter(function(todo){ return todo.isDown }); return arr.length || 0; }, //刪除已完成的任務 deleteDown:function(){ var arr = this.state.todos.filter(function(todo){ return !todo.isDown }); this.setState({ todos:arr }); this.checkAll(); }, changeAllStatus:function(isDown){ //修改todos里面每個任務的狀態 this.state.todos.forEach(function(todo){ todo.isDown = isDown; }); //修改isAllChecked里面的值 this.setState({ todos:this.state.todos, isAllChecked:isDown }); }, render:function(){ return( <div> <Ck addCkcon={this.addCkcon} /> <Ul todos={this.state.todos} deleteCkcon={this.deleteCkcon} changeStatus={this.changeStatus} /> <Status total={this.total()} countDown={this.countDown()} deleteDown={this.deleteDown} isAllChecked={this.state.isAllChecked} changeAllStatus={this.changeAllStatus} /> </div> ); } }); ReactDOM.render( <Zong />, document.getElementById('dome') ); </script> </body> </html>