简体   繁体   English

如何在Reactjs中删除元素onclick?

[英]How to delete element onclick in Reactjs?

I have made CARD's which display's username . 我已经制作了显示username CARD。 When I click on the delete button ie cross or cancel button it should remove the CARD's from the tasklist here tasklist is state variable. 当我点击删除按钮即交叉或取消按钮时,它应该从tasklist删除CARD,这里tasklist是状态变量。 I am using .map() method to iterate over each task and display it. 我使用.map()方法迭代每个任务并显示它。 I want to delete the task card of a particular user when I click on the red cross button (see screenshot) currently only the window appears saying -> are you sure you want to delete it if I click yes it should delete it. 我想删除特定用户的任务卡当我点击红色的十字按钮(见截图)目前只在窗口出现称- > are you sure you want to delete it ,如果我按一下是应该将其删除。

Code: 码:

import React, {Component} from "react"; 

export default class Tasks extends Component{
    constructor(props){
        super(props);
        this.state = {
            taskList:[],
            taskName:"",
            type:"classification",
            datasetName:"",
            allDatasets:[],
            users:[],
            names:[]
        }
    }

triggerDelete(task){
        if(window.confirm("Are you sure you want to delete this task?")){

        }
    }

render(){
        return(
            <div className="tasks-wrap">
                <h1 onClick={()=>{
                   this.props.history.push("/taskdetails");
                }}>Your Tasks</h1>
                {
                            this.state.taskList.map((task,index)=>{
                                return(
                                    <div key={index} className="item-card" onClick={()=>{
                                        window.sessionStorage.setItem("task",JSON.stringify(task));
                                        this.props.history.push("/taskdetails/");
                                    }}>
                                        <div className="name">{task.name}</div>
                                        <div className="sub">
                                            <div className="type">Dataset: {task.dateSetName}</div>
                                            <div className="members">{task.userList.length + " participants"}</div>
                                        </div>
                                        <div className="del-wrap" onClick={(e)=>{
                                            e.stopPropagation();
                                            e.preventDefault();
                                            this.triggerDelete(task);
                                        }}>
                                            <img src={require("../../images/cancel.svg")}/>
                                        </div>
                                    </div>
                                );
                            })
                        }
                        </div>
                    </div>

                </div>
            </div>
        )
    }
}

How should I modify my triggerDelete() method? 我该如何修改triggerDelete()方法? So that the particular card gets deleted. 这样特定的卡就会被删除。

在此输入图像描述

在此输入图像描述

Pass index of the deleting task to the triggerDelete function and then just remove that index from this.state.taskList array. 将删除任务的index传递给triggerDelete函数,然后从this.state.taskList数组中删除该索引。

<div className="del-wrap" onClick={(e)=>{
      e.stopPropagation();
      e.preventDefault();
      this.triggerDelete(task, index);
   }}>
        <img src={require("../../images/cancel.svg")}/>
</div> 

And in triggerDelete function 并在triggerDelete函数中

triggerDelete(task, index){
   if(window.confirm("Are you sure you want to delete this task?")){
      let taskList = [...this.state.taskList]
      taskList.splice(index, 1);
      this.setState({taskList: taskList})
   }
}

you need to write the logic to delete the task, its easier to do it if you pass the index of the task to triggerDelete. 你需要编写删除任务的逻辑,如果你将任务的索引传递给triggerDelete,它就更容易做到。 window.confirm returns a boolean value indicating whether OK or Cancel was selected (true means OK). window.confirm返回一个布尔值,指示是选择了OK or Cancel (true means OK).

import React, {Component} from "react"; 

export default class Tasks extends Component{
    constructor(props){
        super(props);
        this.state = {
            taskList:[],
            taskName:"",
            type:"classification",
            datasetName:"",
            allDatasets:[],
            users:[],
            names:[]
        }
    }

triggerDelete(index){
        if(window.confirm("Are you sure you want to delete this task?")){

           this.setState(prevState => ({
               taskList: [...prevState.taskList.slice(0, index), ...prevState.taskList.slice(index + 1)]
           }))

        }
    }

render(){
        return(
            <div className="tasks-wrap">
                <h1 onClick={()=>{
                   this.props.history.push("/taskdetails");
                }}>Your Tasks</h1>
                {
                            this.state.taskList.map((task,index)=>{
                                return(
                                    <div key={index} className="item-card" onClick={()=>{
                                        window.sessionStorage.setItem("task",JSON.stringify(task));
                                        this.props.history.push("/taskdetails/");
                                    }}>
                                        <div className="name">{task.name}</div>
                                        <div className="sub">
                                            <div className="type">Dataset: {task.dateSetName}</div>
                                            <div className="members">{task.userList.length + " participants"}</div>
                                        </div>
                                        <div className="del-wrap" onClick={(e)=>{
                                            e.stopPropagation();
                                            e.preventDefault();
                                            this.triggerDelete(index);
                                        }}>
                                            <img src={require("../../images/cancel.svg")}/>
                                        </div>
                                    </div>
                                );
                            })
                        }
                        </div>
                    </div>

                </div>
            </div>
        )
    }
}

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

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