简体   繁体   English

单击删除按钮时删除了所有项目,以及如何使用React.js实现编辑/更新功能

[英]Deleted all item when click on delete button and how to implement the edit/update functionality using React.js

I am new to React.js and I need to implement the delete and update functionality. 我是React.js ,我需要实现删除和更新功能。 Here I have done some coding but in case of delete one element, its deleting all from the array list. 在这里,我做了一些编码,但是如果删除一个元素,则将其从数组列表中删除。

Here is my code: 这是我的代码:

TodoList.js: TodoList.js:

import React, { Component } from "react";
import TodoItems from "./TodoItems";
import "./TodoList.css";
class TodoList extends Component {
  constructor(props, context){
    super(props, context);
    this.state={
      items:[]
    }
    this.addItem=this.addItem.bind(this);
    this.deleteItem = this.deleteItem.bind(this);
    this.editItem = this.editItem.bind(this);
  }
  addItem(e){
    var itemArray = this.state.items;
    if (this.inputElement.value !== '') {
      itemArray.unshift({
        text:this.inputElement.value,
        key:Date.now()
      })
      this.setState({
        items:itemArray
      })
      this.inputElement.value='';
    }
    e.preventDefault();
  }
  deleteItem(key) {
    var filteredItems = this.state.items.filter(function (item) {
      return (item.key !== key);
    });

    this.setState({
      items: filteredItems
    });
  }
  editItem(key){

  }
  render() {
    return (
      <div className="todoListMain">
        <div className="header">
          <form onSubmit={this.addItem}>
            <input ref={(a)=>this.inputElement=a} placeholder="enter task">
            </input>
            <button type="submit">Add</button>
          </form>
        </div>
        <TodoItems entries={this.state.items} delete={this.deleteItem} edit={this.editItem}/>
      </div>
    );
  }
}

export default TodoList;

TodoItems.js: TodoItems.js:

import React, { Component } from "react";
class TodoItems extends Component {
  constructor(props, context) {
    super(props, context);

    this.createTasks = this.createTasks.bind(this);

  }
  edit(key){
    this.props.edit(key);
  }
  delete(key){
    this.props.delete(key);
  }
  createTasks(item) {
    return <li key={item.key}>{item.text}<a href="" className="button bg_green" onClick={()=>this.edit(item.key)}>Edit</a><a href=""className="button bg_red" onClick={()=>this.delete(item.key)}>Delete</a></li>
  }

  render() {
    var todoEntries = this.props.entries;
    var listItems = todoEntries.map(this.createTasks);

    return (
      <ul className="theList">
          {listItems}
      </ul>
    );
  }
};


export default TodoItems;

My problem is when I am trying to delete any item from the list, All items are removing. 我的问题是,当我尝试从列表中删除任何项目时,所有项目都将被删除。 Here Also I need to implement the edit and update functionality means once user will click on edit button the respective value will display on the text box and user will change it and save after click on Add button. 在这里,我还需要实现edit and update功能,这意味着一旦用户单击编辑按钮,相应的值将显示在文本框上,用户将在单击添加按钮后对其进行更改并保存。

Issue is this: 问题是这样的:

href=""

Either use href="#" or href="javascript:void(0)" , or remove the href from a tag. 要么使用href="#"href="javascript:void(0)"或删除hrefa标签。 It will work properly. 它会正常工作。

Working fiddle. 工作提琴。 (changed only href="" to href="#" ) (仅将href=""更改为href="#"

Your delete button is inside a anchor tag which has a href, so it is causing your page to navigate and hence reloading the page making you believe everything is getting deleted but in reality the page is reloading. 您的删除按钮位于具有href的锚标记内,因此这会导致页面导航并因此重新加载页面,使您相信所有内容都将被删除,但实际上页面正在重新加载。 Simply removing the href tag will solve your issue you need to however apply some css to give it a link feel. 只需删除href标签即可解决您的问题,但是您需要应用一些CSS使其具有链接感。

Replace your createTasks function with the one below to solve your delete problem. 将您的createTasks函数替换为下面的函数,以解决您的删除问题。

  createTasks(item) {
return (
  <li key={item.key}>
    {item.text}
    <a
      className="button bg_green"
      onClick={() => this.edit(item.key)}
    >
      Edit
    </a>
    <a
      className="button bg_red"
      onClick={() => this.delete(item.key)}
    >
      Delete
    </a>
  </li>
);}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM