夺命雷公狗-----React---23--小案例之react经典案例todos(完成添加任务)

我们这次来处理用户添加的数据,我们还是赵老规矩看看组建大致图。。。

 

 

 

 

 

子组件对父组建进行数据的传递其实是react内部的机智进行处理的了,,

 

代码如下所示:

<!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({
            render:function(){
                return(
                    <li>
                        <label>
                            <input type="checkbox" checked={this.props.todo.isDown} />
                            {this.props.todo.text}
                        </label>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <button>删除</button>
                    </li>
                );
            }
        });
        //列表框区域
        var Ul = React.createClass({
            render:function(){
                return(
                    <ul>
                        {
                            this.props.todos.map(function(item,index){
                                return <Lists todo={item} key={index} />
                            })
                        }
                    </ul>
                );
            }
        });
        //状态组建
        var Status = React.createClass({
            render:function(){
                return(
                    <div>
                        <input type="checkbox" />
                        3 已完成  /  3 总数
                        &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
                });
            },
            render:function(){
                return(
                    <div>
                        <Ck addCkcon={this.addCkcon} />
                        <Ul todos={this.state.todos} />
                        <Status />
                    </div>
                );
            }
        });
        ReactDOM.render(
            <Zong />,
            document.getElementById('dome')
        );
    </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/leigood/p/6112298.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值