【发布时间】:2020-05-15 14:20:02
【问题描述】:
我正在开发一个 React 应用程序,尝试使用 reactstrap 库在单击按钮时打开一个模式。此模式将有一个包含 2 个字段的表单,名字和姓氏。提交表单后,主页上的表格中的 2 个单元格应使用 First Name 和 Last Name 值进行更新。
我想要一些关于我当前代码中的 2 个问题的帮助:
- 单击按钮时无法弹出模式。
- 我不知道如何使用模态表单中的数据填充表格单元格。
这是我当前的 ModalLeads 文件代码,其中包含模态:
import React, { Component } from "react";
import {
Modal,
ModalFooter,
ModalBody,
ModalHeader,
Button,
Col,
Form,
FormGroup,
Input,
Label
} from "reactstrap";
class ModalLeads extends Component {
constructor(props) {
super(props);
this.state = {
info: false,
firstName: null,
lastName: null,
};
this.handleChange = this.handleChange.bind(this);
}
//******************************************************************** */
handleChange = e => {
e.preventDefault();
this.setState({ [e.target.name]: e.target.value });
};
render() {
return (
<Modal
isOpen={this.state.info}
toggle={this.toggleInfo}
backdrop="static"
keyboard={false}
className={"modal-lg " + this.props.className}
>
<Form
action=""
method="POST"
onChange={this.handleChange}
onSubmit={this.onCreate}
encType="multipart/form-data"
>
<ModalHeader toggle={this.toggleInfo} className="blue-header">
<i className="cui-people"></i>Leads
</ModalHeader>
<ModalBody>
<FormGroup row className="my-0">
<Col xs="8">
<FormGroup>
<Label htmlFor="firstName">First Name</Label>
<Input
onChange={this.handleChange}
name="firstName"
type="text"
id="firstName"
value={this.state.firstName || ""}
placeholder="Enter First Name"
/>
</FormGroup>
</Col>
<Col xs="4">
<FormGroup>
<Label htmlFor="lastName">Last Name</Label>
<Input
onChange={this.handleChange}
name="lastName"
type="text"
id="lastName"
value={this.state.lastName || ""}
placeholder="Last Name"
/>
</FormGroup>
</Col>
</FormGroup>
</ModalBody>
<ModalFooter>
<Button color="primary" type="submit" onClick={this.toggleInfo}>
<i className="cis-check-double-alt"></i> Save
</Button>
<Button
color="secondary"
onClick={() => {
this.toggleInfo();
this.resetForm();
}}
>
Cancel
</Button>
</ModalFooter>
</Form>
</Modal>
);
}
}
export default ModalLeads;
这是我当前的潜在客户文件代码,其中包括一个 ModalLeads 元素:
import React, { Component, Suspense } from "react";
import axios from "axios";
import { BootstrapTable, TableHeaderColumn } from "react-bootstrap-table";
import "react-bootstrap-table/dist//react-bootstrap-table-all.min.css";
import LeadsModal from "../Modal/ModalLeads";
import {
Button,
Card,
CardBody,
CardHeader,
Row,
ModalFooter
} from "reactstrap";
// state value for info is initially set to false but should be updated to true when we want to trigger the modal to show:
class Leads extends Component {
constructor(props) {
super(props);
this.state = {
modalClient: false,
leadId: null,
info: false,
firstName: null,
lastName: null,
}
this.toggleInfo = this.toggleInfo.bind(this);
}
toggle() {
this.setState({
modal: !this.state.modal
});
}
// function to change the state value for info, should run when we want to open or close the modal:
toggleInfo(leadId) {
this.setState({
info: !this.state.info
});
}
buttonFormat(cell, row) {
//const id = api + `clients/${row.id}`;
const id = row.id;
return (
<Row>
<Button className="btn btn-primary" onClick={() => this.toggleInfo(id)}>
<i className="cis-comment-bubble-edit"></i>
</Button>
</Row>
);
}
render() {
return (
{/************************ Windows Modal */}
<LeadsModal
isOpen={this.state.info}
toggle={this.toggleInfo}
backdrop="static"
keyboard={false}
className={"modal-lg " + this.props.className}
/>
// I use react-bootstrap-table for rendering the grid:
<div>
<Card>
<CardBody>
<CardBody>
<BootstrapTable
data={this.state.table}
version="4"
striped
hover
pagination
options={this.options}
>
<TableHeaderColumn dataField="firstName" dataSort>
First Name
</TableHeaderColumn>
<TableHeaderColumn dataField="lastName" dataSort>
Last Name
</TableHeaderColumn>
<TableHeaderColumn isKey dataField="email">
Email
</TableHeaderColumn>
<TableHeaderColumn dataField="phone" dataSort>
Phone
</TableHeaderColumn>
<TableHeaderColumn dataField="title" dataSort>
Title
</TableHeaderColumn>
<TableHeaderColumn
dataField="id"
dataFormat={this.buttonFormat.bind(this)}
>
Action
</TableHeaderColumn>
</BootstrapTable>
</CardBody>
</Card>
</div>
);
}
}
export default Leads;
【问题讨论】:
-
抱歉不干净,是的,我想从 onClick 打开模态,我需要发送状态值以填充输入
-
我的问题是如何用状态填充我的输入
-
我刚刚更新了您的问题和代码,使其更加清晰。将来,请在描述您的问题时尽可能清楚,尽可能详细地提供。另外,请不要将同一文件的代码分成多个 sn-ps,因为这会使代码在调试时更难以阅读和复制。
标签: reactjs bootstrap-modal react-bootstrap reactstrap react-bootstrap-table