简体   繁体   English

如何从待办事项列表中更新值:单击更新按钮时,应在文本字段中编辑值,并应将其更新为相同的索引

[英]How to update the value from todo list : when click on update button value should get edited on text field and should get updated to same index

React Todo list : I want to update the text when click on update button.when update button is clicked the value should get appeared on text field from where the text is added and again after editing the value,the value should get updated on same index after clicking on add button. React Todo list:我想在单击更新按钮时更新文本。单击更新按钮时,该值应该出现在添加文本的文本字段中,并在编辑该值后再次在相同的索引上更新该值单击添加按钮后。

import React from 'react';

 class Todo extends React.Component{
    constructor(props){
        super(props);
        this.onChange = this.onChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.handleClick= this.handleClick.bind(this);
        this.state={
            todoList:[],
            todoString:"",
            a:''

        }
    };
    onChange(event)
        {
        this.setState({todoString:event.target.value});

            }

    onSubmit()
    {

        let todoList =  this.state.todoList;
           let todoObject = {"todoText":this.state.todoString, "isDone": false, "isUpdated":false};
           todoList.push(todoObject); // todo-object is push in todolist
           this.setState({todoList:todoList,todoString:''});


    }
    handleClick(todoItem) {
        todoItem.isDone = true;

       this.setState({todoList:this.state.todoList});

    }
updatedClick(value) {

    this.setState({todoString: value.todoText});
}
   render()
   {
       return(
           <div>
            <div>{

            }
                <input  value={this.state.todoString} onChange={(e) => this.onChange(e)}/> <button  onClick={(e) =>this.onSubmit(e)}> Add </button>
                {
                    this.state.todoList.map((value, index) => {
                        return(
                            <div>


                                {value.isDone === true ? <span style={{'text-decoration':'line-through','padding':'10px'}}>{value.todoText}</span>: <span>{value.todoText}</span>}

                                <button  onClick={()=>this.handleClick(value)}> Mark Done</button>

                                <button  onClick={(e)=>this.updatedClick(value)}> update</button>

                            </div>



                        )
                    })
                }

            </div>
           </div>
       )
   }
}
export default Todo;

we have to use one extra property in state for insert/update mode. 我们必须在状态中为插入/更新模式使用一个额外的属性。 on update click we will set index for element that is going to be updated and if index is set and not equal to -1 then we are in edit mode else we use insert mode. 在更新单击时,我们将为要更新的元素设置索引,如果设置了索引并且不等于-1,则说明我们处于编辑模式,否则使用插入模式。

class Todo extends React.Component{
    constructor(props){
        super(props);
        this.onChange = this.onChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);
        this.handleClick= this.handleClick.bind(this);

        this.state={
            todoList:[],
            todoString:"",
            editIndex : -1 // init with insert mode.
        }
    };
    onChange(event) { this.setState({todoString:event.target.value}); }

    onSubmit() {
      let todoList = this.state.todoList;

      if(this.state.editIndex != -1) { // update mode
        todoList[this.state.editIndex].todoText = this.state.todoString;
      } else { // insert mode
        let todoObject = {"todoText" : this.state.todoString, "isDone": false, "isUpdated":false};
        todoList = [...todoList , todoObject];
      }

      this.setState({todoList:[...todoList] , todoString:'' , editIndex : -1 }); // reset insert mode
    }

    handleClick(todoItem) { // improved for toggling insted of only mark as done. 
      todoItem.isDone = !todoItem.isDone; 
      this.setState({todoList:[...this.state.todoList]}); 
    }

    updatedClick(value) { // set update mode
      let i = this.state.todoList.findIndex(l => {return l.todoText == value.todoText});
      this.setState({editIndex : i, todoString : value.todoText}); 
    }

   render()
   {
       return(
           <div>
            <div>
                <input  value={this.state.todoString} onChange={(e) => this.onChange(e)}/> <button  onClick={(e) =>this.onSubmit(e)}> Add </button>
                {
                    this.state.todoList.map((value, index) => {
                        return(
                            <div>
                                 { 
                                  value.isDone === true ? 
                                  <span style={{'text-decoration':'line-through','padding':'10px'}}>{value.todoText}</span> : 
                                  <span>{value.todoText}</span> }

                                <button  onClick={()=>this.handleClick(value)}> Toggle</button>

                                <button  onClick={(e)=>this.updatedClick(value)}> update</button>

                            </div>



                        )
                    })
                }

            </div>
           </div>
       )
   }
}
export default Todo;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户单击更新按钮时,如何在 TODO 应用程序的输入字段中设置 li 值(li 内的文本)? - How to set the li value (text inside the li) in the input filed in TODO application when user click on update button? 当我单击该行的更新按钮时,如何从jquery数据表中获取文本框值 - How to get textbox value from jquery datatables when I click the update button of that row 在gridview的更新按钮单击时,从gridview的editItem模板字段获取值 - Get the value from editItem template field of gridview on update button click of gridview 将当前或更新的文本字段标签值传递给更新按钮? - Passing current or updated text field tag value for update button? 更新 localStorage 值(待办事项列表) - Update localStorage value (todo list) 如何获取输入字段的值以更新 EJS 中的按钮颜色 - How to get Value of Input Field to Update button color in EJS 单击更新按钮 Angular 如何检测表单值是否更新 - How to detect form value is updated or not on click of update button Angular 如何具有多个具有相同ID值的按钮,并且单击任何按钮时都应该弹出窗口? - How to have multiple buttons of same id value and when click on any button the pop-up should come? 如何在onclick时获取值,并再次单击值应在jquery中删除? - how to get the value when onclick and again on click value should be removed in jquery? 如何将按钮的值传递给输入文本字段,但使用javascript在文本字段中值应消失 - How to pass the value of button to input text field but value should be disappear in text field using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM