夺命雷公狗-----React---27--小案例之react经典案例todos(清除已完成)

这个功能其实也是很简单的,就只是让todos里面的内isDown进行取反即可

然后在Zong里面进行下修改即可

效果如下所示:

代码如下所示:

<!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();
            },
            render:function(){
                return(
                    <div>
                        <input type="checkbox" />
                        {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
                })
            },
            //统计总的任务个数
            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
                });
            },
            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} />
                    </div>
                );
            }
        });
        ReactDOM.render(
            <Zong />,
            document.getElementById(‘dome‘)
        );
    </script>
</body>
</html>
时间: 2024-08-24 06:44:43

夺命雷公狗-----React---27--小案例之react经典案例todos(清除已完成)的相关文章

夺命雷公狗---微信开发47----获取用户地理位置接口(2)

我们现在要做的是查找距离最近的“肯德基”,我们需要通过百度提供的LBS云服务定位距离您最近的肯德基,该程序需要到LBS后台进行相关设置,然后在完成程序 用户在客户端输入“肯德基”公众号就会自动回复距离用户最近的”肯德基“ 废话不多说,我们先到http://developer.baidu.com/里面找到LBS云------再到服务接口--------再到LBS云,如下图所示: 点击进来后,我们首先要获取一个密钥, 然后出了红色框部分填下数据即可,别的地方都不用改,直接提交即可 上面的0.0.0.

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

比如我们要实现的是这种效果: 用这种方法来写,她只能写死在哪,没啥意思,所以我们定义一个数据,然后来测试下map方法对她遍历出来的数据 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react-with-addons.

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

先写一个组建... 然后进行嵌套.. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react-with-addons.js"></script> <script src="

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

首先要引入她.. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react-with-addons.js"></script> <script src="./js/react

夺命雷公狗---微信开发46----获取用户地理位置接口(1)

我们先来看下手册有啥要求的先 我们先来写一个小功能,客户端每隔5s(秒)钟自动向公众平台报告自己的地理位置,服务器返回该地理位置给客户端. 我们要先到公众号管理界面里打开他才可以,因为默认的情况下他是关闭的,打开方法如下图所示: 如果开启成功后,他将会显示修改陈功,如下图所示: 我们在从新写过一个index.php的文件,代码如下所示: <?php /** * wechat php test */ //define your token require_once "common.php&q

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

提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react.js"></script> <script src="./js/react

夺命雷公狗-----React---3--标签的规则

<!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"><

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

提示:props的值是不可以改变的... <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="./js/react.js"></script> <script src="./js/react

夺命雷公狗-----React---1--页面的渲染

<!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"><

夺命雷公狗-----React---11--添加类和样式

<!DOCTYPE> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/react.js"></script> <script src="./js/react-dom.js"></script> <script src="