【问题标题】:How can I pass data to Modal using react. Edit or Delete?如何使用 react 将数据传递给 Modal。编辑还是删除?
【发布时间】:2020-05-12 09:48:25
【问题描述】:

我想在单击按钮编辑时将数据传递给模式,例如 ID、电子邮件和密码。刚开始反应。我在这里使用 API。当我使用 laravel 或其他 pl 时,我怎么能做到这一点似乎非常不同。希望对如何做到这一点提供一些帮助和建议。

这是代码,this.state 包含我的 API 中的字段 切换 Modal 和使用 react-table 返回的 fetch API

import React from 'react';
import namor from "namor";
import ReactTable from 'react-table';
import { Link } from 'react-router-dom';
import { Panel, PanelHeader } from './../../components/panel/panel.jsx';
import 'react-table/react-table.css';
import axios from 'axios';
import SweetAlert from 'react-bootstrap-sweetalert';
import ReactNotification from 'react-notifications-component';
import 'react-notifications-component/dist/theme.css';
import { Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap';


class apitable extends React.Component {
    constructor(props) {
        super(props);


        this.state = {
            tableData: [{
                id: '',
                email: '',
                password: '',
                modalDialog: false,
                modalWithoutAnimation: false,

            }],
        };

        this.toggleModal = this.toggleModal.bind(this);


    }

toggleModal(name) {
        switch (name) {
            case 'modalDialog':
                this.setState({ modalDialog: !this.state.modalDialog });
                break;
            case 'modalWithoutAnimation':
                this.setState({ modalWithoutAnimation: !this.state.modalWithoutAnimation });
                break;
            case 'modalMessage':
                this.setState({ modalMessage: !this.state.modalMessage });
                break;
            case 'modalAlert':
                this.setState({ modalAlert: !this.state.modalAlert });
                break;
            default:
                break;
        }
    }


componentDidMount() {
  axios
    .get("https://lmsapi.riverside-tekteach.com/api/teachers", {
      responseType: "json"
    })
    .then(response => {
      this.setState({ tableData: response.data });
    });
}

render() {
    const { tableData } = this.state;

    return (


        <div class="panel panel-inverse">


            Edit Modal

            <Modal isOpen={this.state.modalWithoutAnimation} fade={false} toggle={() => this.toggleModal('modalWithoutAnimation')} >
                <ModalHeader toggle={() => this.toggleModal('modalWithoutAnimation')}>Modal Without Animation</ModalHeader>
                <ModalBody>
                    <p>

                                </p>
                </ModalBody>
                <ModalFooter>
                    <button className="btn btn-white" onClick={() => this.toggleModal('modalWithoutAnimation')}>Close</button>
                </ModalFooter>
            </Modal>



            <div class="panel-body  undefined">
        <ReactTable filterable data={tableData} columns={[
            {
                Header: 'Info',
                columns: [
                    {
                        Header: 'Id',
                        accessor: 'id',
                    },
                    {
                        Header: 'Email',
                        id: 'email',
                        accessor: d => d.email,
                    },
                    {
                        Header: 'Password',
                        id: 'password',
                        accessor: d => d.password,
                    },
                    {
                        id: 'edit',
                        accessor: '[row identifier to be passed to button]',
                        //Cell: ({ value }) => (<button className="btn btn-danger btn-sm">Edit</button>)
                        Cell: row => (
                            <div>
                                <button onClick={() => this.toggleModal('modalWithoutAnimation')} className="btn btn-info btn-sm">Edit {tableData.map(tableData => tableData.id(1))}</button>&nbsp;
                                <button className="btn btn-danger btn-sm" >Deletes </button>
                            </div>
                        )
                    },
                ],
            },

        ]} defaultPageSize={10} defaultSorted={this.defaultSorted} className="-highlight" />
            </div>
            </div>
)}}
 export default apitable

【问题讨论】:

  • 请提供Minimal, Complete, and Reproducible代码示例。 Modal 是您在其中获取数据的同一组件的一部分吗?您尝试将道具传递给您的模态时尝试过什么?
  • 这既不是完整的,也不是可重现的反应代码。您能否分享整个组件代码,和/或展示ReactTable 是什么?你用的是什么版本的react-table
  • @DrewReese 这就是我所有的代码缺少什么??

标签: reactjs modal-dialog bootstrap-modal react-table


【解决方案1】:

您可以定义状态变量并在模态中使用它。

 this.state = {
          selectedData:{},
            tableData: [{
                id: '',
                email: '',
                password: '',
                modalDialog: false,
                modalWithoutAnimation: false,

            }],
        };

并像下面这样传递选定的行数据

 Cell: row => (
                            <div>
                                <button onClick={() => {
                                  this.setState({selectedData:row.original})
                                  console.log(row.original)
                                  this.toggleModal('modalWithoutAnimation')}} className="btn btn-info btn-sm">Edit </button>&nbsp;
                                <button className="btn btn-danger btn-sm" >Deletes </button>
                            </div>
                        )


内部模态

         <ModalBody>
            <p>
            {this.state.selectedData.id}<br/>
            {this.state.selectedData.email}<br/>
            {this.state.selectedData.password}
            </p>
        </ModalBody>

【讨论】:

  • 很好的解决方案,我试着理解一下,非常感谢
【解决方案2】:

只是在这里尝试一下。通常在反应中使用数组并且您想要对条目进行操作,然后您将要使用的元素的索引传递给回调。

创建一个新的 onClick 处理程序,用于设置您要编辑的索引并切换模式打开

editByIndex(editIndex) {
  this.setState({ editIndex });
  this.toggleModal("modalWithoutAnimation");
}

更新onClick 回调

Cell: row => (
  <div>
    <button
      onClick={() => editByIndex(row.index)} // based on row props
      className="btn btn-info btn-sm"
    >
      Edit
    </button>
    &nbsp;
    <button className="btn btn-danger btn-sm">Delete</button>
  </div>
)

row props

然后将this.state.tableData[this.state.editIndex] 传递给模态

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2017-07-30
    • 1970-01-01
    相关资源
    最近更新 更多