这个功能实现的步骤如下所示:

最终实现全选和反选,代码如下所示:

<!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>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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} 总数
&nbsp;&nbsp;&nbsp;
<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>

夺命雷公狗-----React---28--小案例之react经典案例todos(全选和反选)完的更多相关文章

  1. 夺命雷公狗-----React---12--添加类和样式

    <!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></ ...

  2. 夺命雷公狗-----React---11--添加css样式的方法

    <!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></ ...

  3. 夺命雷公狗-----React---10--组建嵌套进行数据遍历

    先写一个组建... 然后进行嵌套.. <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  4. 夺命雷公狗-----React---9--map数据的遍历

    比如我们要实现的是这种效果: 用这种方法来写,她只能写死在哪,没啥意思,所以我们定义一个数据,然后来测试下map方法对她遍历出来的数据 <!DOCTYPE html> <html l ...

  5. 夺命雷公狗-----React---8--react官方提供的组建实现双向绑定

    首先要引入她.. <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. 夺命雷公狗-----React---7--组建的状态props和state

    props:组建初始要渲染的数据,他是不可以改变的 state:组建状态发生改变,调用render重新渲染数据 我们来写一个例子: <!DOCTYPE html> <html lan ...

  7. 夺命雷公狗-----React---6--props多属性的传递

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 夺命雷公狗-----React---5--props对象的传递

    提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  9. 夺命雷公狗-----React---4--props变量的传递

    提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

随机推荐

  1. Windows程序内部运行机制 转自http://www.cnblogs.com/zhili/p/WinMain.html

    一.引言 要想熟练掌握Windows应用程序的开发,首先需要理解Windows平台下程序运行的内部机制,然而在.NET平台下,创建一个Windows桌面程序,只需要简单地选择Windows窗体应用程序 ...

  2. IBatis按条件分页查询

    XML中代码  <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE sqlMap PUBLIC & ...

  3. 判断.NET4.0是否安装

    Clinet 和 Full存在一个都说明安装了Framework "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client"&qu ...

  4. 深入浅出Android App耗电量统计

    前言 在Android统计App耗电量比较麻烦,直至Android 4.4,它仍没公开“电量统计”API或文档……额,是的,仅没有公开,并不是没有.平时在手机“设置- 电量”看到的数据 就是系统调用内 ...

  5. HTML基础篇之HTML基本结构

    课堂知识总结 第一接触和学习HTML知识在学习过程中对所属的标签的自己认为的理解和解释. HTML元素:文档里面的标签和内容. 比如:<h1>大家好</h1>  左边的是开始标 ...

  6. int(*f)(int)

    int(*f)(int): 为指向函数的指针变量的定义方法,其中f为指向函数的指针变量,第一个int为函数返回值类型,第二个int为函数的形参类型.

  7. Linux Vim编辑器使用简单讲解

    在Linux中,主要编辑器为vi或者vim,本文围绕vim做简单的讲解说明:Linux默认自带vi(vim)编辑器,其程序包为:[root@linuxidc.com ~]# rpm -qf `whic ...

  8. Python强化训练笔记(六)——让字典保持有序性

    python的字典是一个非常方便的数据结构,使用它我们可以轻易的根据姓名(键)来找到他的成绩,排名等(值),而不用去遍历整个数据集. 例如:{'Lee': [1, 100], 'Jane': [2, ...

  9. Open Data Structure Templates

    数据结构模板 Chen 2016/12/22 前言 本篇博客的模板,全部是我纯手打的,如果有发现错误,请在下方留言指正:).欢迎大家参考. 有一些地方还不是很完善,等过一阵子用C++实现和部分重构下. ...

  10. SpringBoot list查询方法

    SpringBoot中根据不同查询条件,获取list列表.@Componentpublic class QuerySpecifications {} (1)根据商户,查询商品全部列表 public S ...