简体   繁体   English

从数组中的值分配表单值

[英]Assigning form value from values within an array

I have a table that has an edit button next to each row. 我有一个表格,每行旁边都有一个编辑按钮。 I send the persons id to the form component and get their data from the DB using the id. 我将人员ID发送到表单组件,并使用ID从数据库中获取其数据。 I assign it to patient. 我将其分配给患者。 I then want to use the data to autofill a form. 然后,我想使用数据自动填写表格。

Here is the top of the file. 这是文件的顶部。

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

    //console.log(this.props.location.search);
    const query = queryString.parse(this.props.location.search);
    console.log(query.patientId);

    this.state = {
      ptId: query.patientId || null,
      patient: null
    };

Here is the fetch that I confirm works when I log it. 这是我在登录时确认有效的提取。

  getPatient() {
    fetch("https://localhost:5001/api/PtEditPg/" + this.state.ptId)
      .then(response => response.json())
      .then(data => this.setState({ patient: data }));
  }

Here is the one of the forms showing what I have attempted 这是显示我尝试过的形式之一

              <label htmlFor="firstName">First Name</label>
              <input
                type="Name"
                className="form-control"
                id="firstName"
                name="firstName"
                placeholder="First Name"
                value={this.state.patient.firstName}
                onChange={this.handleChange}
              />

I finally figured it out. 我终于弄明白了。 The object was an array I think? 我认为对象是数组吗?

This is what worked 这是有效的

this.state.patient[0].firstName

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

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