Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ReactJS在单击删除按钮时创建弹出窗口?_Javascript_Reactjs_Popup_Popupwindow_Sweetalert - Fatal编程技术网

Javascript 如何使用ReactJS在单击删除按钮时创建弹出窗口?

Javascript 如何使用ReactJS在单击删除按钮时创建弹出窗口?,javascript,reactjs,popup,popupwindow,sweetalert,Javascript,Reactjs,Popup,Popupwindow,Sweetalert,我想当我点击按钮删除,它会打开一个确认弹出窗口。 我尝试使用sweetAlert,但它没有显示任何弹出窗口 popupdel方法: popupdel() { swal({ title: "Are you sure?", text: "Your will not be able to recover this imaginary file!", type: "warning", showCancelButton: true, c

我想当我点击按钮删除,它会打开一个确认弹出窗口。 我尝试使用
sweetAlert
,但它没有显示任何弹出窗口

popupdel方法:

 popupdel() {
    swal({
      title: "Are you sure?",
      text: "Your will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonClass: "btn-danger",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    }, function() {
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
  }
delete(Code) {

axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {

this.setState({ clients: clients.records });

}.bind(this));

}

<button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>
删除方法:

 popupdel() {
    swal({
      title: "Are you sure?",
      text: "Your will not be able to recover this imaginary file!",
      type: "warning",
      showCancelButton: true,
      confirmButtonClass: "btn-danger",
      confirmButtonText: "Yes, delete it!",
      closeOnConfirm: false
    }, function() {
      swal("Deleted!", "Your imaginary file has been deleted.", "success");
    });
  }
delete(Code) {

axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {

this.setState({ clients: clients.records });

}.bind(this));

}

<button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>
删除(代码){
axios.delete('http://localhost:4000/app/deleteclient/“+代码)。然后(函数(客户端){
this.setState({clients:clients.records});
}.约束(这个);
}
this.delete(client.Code)}type=“button”>

如何使弹出窗口显示在单击按钮上

如果我理解你想要实现的目标:

  • 您可以为显示或不显示的弹出窗口创建一些状态。这将是一个布尔值
  • 将初始状态设置为false
  • 创建一个方法,当您单击按钮时,它将切换状态 这是真的
  • 当状态为true时,显示弹出窗口
  • 然后根据你的应用程序结构将方法传递给你的按钮

    class Example extends React.Component {
      constructor() {
          super();
          this.state = {
              isPopupShown: false
          }
    
         this.togglePopup = this.togglePopup.bind(this);
      }
    
    
      togglePopup() {
        this.setState(prevState => ({
          isPopupShown: !prevState.isPopupShown
        }));
      }
    
      render() {
        return (
          <div>
            <button onClick={ this.togglePopup }>
              toggle popup
            </button>
            { this.state.isPopupShown === true ? <div>popup shown!</div> : <div>popup hidden!</div> }
          </div>
        )
      }
    }
    
    ReactDOM.render(<Example />, document.querySelector("#app"))
    
    类示例扩展了React.Component{
    构造函数(){
    超级();
    此.state={
    isPopupShown:false
    }
    this.togglePopup=this.togglePopup.bind(this);
    }
    togglePopup(){
    this.setState(prevState=>({
    isPopupShown:!prevState.isPopupShown
    }));
    }
    render(){
    返回(
    切换弹出窗口
    {this.state.isPopupShown===true?显示弹出窗口!:弹出窗口隐藏!}
    )
    }
    }
    ReactDOM.render(,document.querySelector(“#app”))
    

    下面是一个jsfiddle,展示了如何切换某些内容:

    如果我了解您想要实现的目标:

  • 您可以为显示或不显示的弹出窗口创建一些状态。这将是一个布尔值
  • 将初始状态设置为false
  • 创建一个方法,当您单击按钮时,它将切换状态 这是真的
  • 当状态为true时,显示弹出窗口
  • 然后根据你的应用程序结构将方法传递给你的按钮

    class Example extends React.Component {
      constructor() {
          super();
          this.state = {
              isPopupShown: false
          }
    
         this.togglePopup = this.togglePopup.bind(this);
      }
    
    
      togglePopup() {
        this.setState(prevState => ({
          isPopupShown: !prevState.isPopupShown
        }));
      }
    
      render() {
        return (
          <div>
            <button onClick={ this.togglePopup }>
              toggle popup
            </button>
            { this.state.isPopupShown === true ? <div>popup shown!</div> : <div>popup hidden!</div> }
          </div>
        )
      }
    }
    
    ReactDOM.render(<Example />, document.querySelector("#app"))
    
    类示例扩展了React.Component{
    构造函数(){
    超级();
    此.state={
    isPopupShown:false
    }
    this.togglePopup=this.togglePopup.bind(this);
    }
    togglePopup(){
    this.setState(prevState=>({
    isPopupShown:!prevState.isPopupShown
    }));
    }
    render(){
    返回(
    切换弹出窗口
    {this.state.isPopupShown===true?显示弹出窗口!:弹出窗口隐藏!}
    )
    }
    }
    ReactDOM.render(,document.querySelector(“#app”))
    

    下面是一个JSFIDLE,展示了如何切换某些内容:

    使用确认而不是警报或sweetAlert:

    delete(Code) {
        if( confirm('Sure want to delete?')) {
            axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {
    
        this.setState({ clients: clients.records });
    )}
    
    else {
            // Do something..
        } }.bind(this));
    
        <button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>
    
    删除(代码){
    如果(确认('确定要删除?')){
    axios.delete('http://localhost:4000/app/deleteclient/“+代码)。然后(函数(客户端){
    this.setState({clients:clients.records});
    )}
    否则{
    //做点什么。。
    }}.bind(这个));
    this.delete(client.Code)}type=“button”>
    
    使用确认而不是警告或警告:

    delete(Code) {
        if( confirm('Sure want to delete?')) {
            axios.delete('http://localhost:4000/app/deleteclient/' + Code).then(function(clients) {
    
        this.setState({ clients: clients.records });
    )}
    
    else {
            // Do something..
        } }.bind(this));
    
        <button style={btn} onClick={(Code) => this.delete(client.Code)} type="button"><i class="fa fa-trash-o"></i></button>
    
    删除(代码){
    如果(确认('确定要删除?')){
    axios.delete('http://localhost:4000/app/deleteclient/“+代码)。然后(函数(客户端){
    this.setState({clients:clients.records});
    )}
    否则{
    //做点什么。。
    }}.bind(这个));
    this.delete(client.Code)}type=“button”>
    
    就在上周,我为创建了自己的React包装器组件。(NPM上已经有一些包装器类,但我不喜欢它们的工作方式,所以我自己制作了。)下面的链接是a)我的SweetAlert2包装器组件和B)基于用户输入启动警报的两种不同方式的完整功能示例


    链接的示例显示了如何以声明方式启动警报(例如,在
    render()
    函数中拼写JSX代码,然后在状态中切换
    show
    变量),或强制启动警报(例如,在
    render()中为警报保留占位符)
    函数,动态填充
    null
    或新生成警报的内容。

    就在上周,我为创建了自己的React包装器组件。(NPM上已经有一些包装器类,但我不喜欢它们的工作方式,所以我自己制作了。)下面的链接是a)我的SweetAlert2包装器组件和B)基于用户输入启动警报的两种不同方式的完整功能示例


    链接的示例显示了如何以声明方式启动警报(例如,在
    render()
    函数中拼写JSX代码,然后在状态中切换
    show
    变量),或强制启动警报(例如,在
    render()中为警报保留占位符)
    动态填充
    null
    或新生成的警报内容的函数)。

    谢谢@Ab我通过添加swwet警报编辑了我的帖子,我想添加,你能看一下吗?我给了你一个功能完整的StackBlitz。它向您展示了如何将其包含到组件中。它向您展示了如何通过单击某个项目(如按钮)来启动它。在某种程度上,你必须能够自己将代码放入你的应用程序中。谢谢@Ab我通过添加swwet警报来编辑我的帖子,我想添加,你能看一下吗?我给了你一个完全实用的StackBlitz。它向您展示了如何将其包含到组件中。它向您展示了如何通过单击某个项目(如按钮)来启动它。在某种程度上,你必须能够自己将代码放入你的应用程序中。“我尝试使用sweetAlert,但它不会显示任何弹出窗口。”在哪里?@KevinB你能看看帖子吗?我编辑了它,我是说。。。。它似乎仍然不是你删除方法的一部分。“我尝试使用sweetAlert,但它没有显示任何弹出窗口。”在哪里?@KevinB你能看看帖子吗?我编辑了它,我是说。。。。它似乎仍然不是删除方法的一部分。