码迷,mamicode.com
首页 > 其他好文 > 详细

react-todoList

时间:2019-07-02 21:08:43      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:hang   text   todolist   set   cli   asd   native   efault   targe   

import React from ‘react‘

class TodeList extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            hasDoneList: [‘看书‘, ‘跑步‘, ‘仰卧起坐‘, ‘俯卧撑‘], // 已经做了的
            noDoneList: [‘node‘, ‘python‘, ‘angular‘, ‘react-native‘], // 还没开始的
            value: ‘‘ // 输入框的值
        }
    }

    successThing = (index, event) => {  // 当传递参数时,要带上this,之后的参数默认排在前面,event对象在后面
        var temp = this.state.noDoneList
        var addDone = this.state.hasDoneList
        addDone.push(temp[index])
        temp.splice(index, 1)
        this.setState({
            noDoneList: temp,
            hasDoneList: addDone
        })
    }
    failThing = (index, event) => {
        var temp = this.state.noDoneList
        var addDone = this.state.hasDoneList
        temp.push(addDone[index])
        addDone.splice(index, 1)
        this.setState({
            noDoneList: temp,
            hasDoneList: addDone
        })
    }
    getInputValue = (event) => {
        this.setState({
            value: event.target.value
        })
    }
    saveData = () => {
        if (!this.state.value) {
            alert(‘请先输入值‘)
        } else {
            var temp = this.state.noDoneList
            temp.push(this.state.value)
            this.setState({
                noDoneList: temp,
                value: ‘‘
            })
        }

    }
    deleteList = (index) => {
        var temp = this.state.noDoneList
        temp.splice(index, 1)
        this.setState({
            noDoneList: temp
        })
    }
    deleteListDone = (index) => {
        var temp = this.state.hasDoneList
        temp.splice(index, 1)
        this.setState({
            hasDoneList: temp
        })
    }

    render() {
        return (
            <div>
                <h3>新增代办事项</h3>
                <input type="text" value={this.state.value} placeholder={‘新增事项‘} onChange={this.getInputValue}/>
                <button onClick={this.saveData}>确定</button>
                <h3>计划但是未完成的</h3>
                {
                    this.state.noDoneList.map((value, index) => {
                        return (
                            <div key={index}>
                                {/*输入框需有一个key,不然会留有缓存影响其他输入框*/}
                                <input type="checkbox" key={value} onChange={this.successThing.bind(this, index)}/>
                                <span>{value}</span>
                                <span onClick={this.deleteList.bind(this, index)}>X</span>
                            </div>
                        )
                    })
                }
                <h3>已经完成了的</h3>
                {
                    this.state.hasDoneList.map((value, index, array) => {
                        return (
                            <div key={index}>
                                <input checked={true} key={index} type="checkbox"
                                       onChange={this.failThing.bind(this, index)}/> <span>{value}</span>
                                <span onClick={this.deleteListDone.bind(this, index)}>X</span>
                            </div>
                        )
                    })
                }
            </div>
        )
    }
}

export default TodeList

 

react-todoList

标签:hang   text   todolist   set   cli   asd   native   efault   targe   

原文地址:https://www.cnblogs.com/cazj/p/11122910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!