首页 技术 正文
技术 2022年11月15日
0 收藏 453 点赞 3,729 浏览 2745 个字

这一个其实是比较容易的,只需要统计他的总数和已完成的即可,

夺命雷公狗—–React—26–小案例之react经典案例todos(统计部分的完成)

夺命雷公狗—–React—26–小案例之react经典案例todos(统计部分的完成)

效果如下所示:

夺命雷公狗—–React—26–小案例之react经典案例todos(统计部分的完成)

夺命雷公狗—–React—26–小案例之react经典案例todos(统计部分的完成)

代码如下所示:

<!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({
render:function(){
return(
<div>
<input type="checkbox" />
{this.props.countDown} 已完成 / {this.props.total} 总数
&nbsp;&nbsp;&nbsp;
<button>清除已完成</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
})
},
//统计总的任务个数
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;
},
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()} />
</div>
);
}
});
ReactDOM.render(
<Zong />,
document.getElementById('dome')
);
</script>
</body>
</html>

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,893
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,421
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,240
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,052
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,683
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,719