javascript React:无法编辑的文本框以及如何将数据作为数组提交

vbopmzt1  于 2022-10-30  发布在  Java
关注(0)|答案(1)|浏览(159)

我正在显示一个列表。请找到沙箱:https://codesandbox.io/s/solitary-butterfly-4tg2w0
无法编辑文本框。description-id是主键。在Post API调用中,如何将更改的文本框值与相应的description-id一起保存为集合。StoredProcedure将description-id与display-sequence作为参数,以将更改的数据保存到数据库中。
请帮助表单提交如何做到这一点?谢谢

import "./styles.css";
import React from "react";
import XMLParser from "react-xml-parser";

const data = `<?xml version="1.0"?>
<Category>
<description description-id="11" display-sequence="2">testing</description>
<description description-id="15" display-sequence="5">Guide</description>
<description description-id="20" display-sequence="7">test</description>
<description description-id="25" display-sequence="10">Guide</description>
<description description-id="30" display-sequence="12">test</description>
</Category>
</xml>`;

const REQUEST_URL = "";

const axios = {
  get: () =>
    new Promise((resolve) => {
      setTimeout(resolve, 1000, { data });
    })
};

class Sort_Descr extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      proddescriptions: [],
      proddescription_id: "",
      display_sequence: ""
    };
    this.handleChange = this.handleChange.bind(this);
  }
  handleChange(event) {}
  componentDidMount() {
    this.getlistofdescriptions();
  }

  getlistofdescriptions() {
    axios
      .get(REQUEST_URL, { "Content-Type": "application/xml; charset=utf-8" })
      .then((response) => {
        const jsonDataFromXml = new XMLParser().parseFromString(data);
        const descriptions = jsonDataFromXml.getElementsByTagName(
          "description"
        );
        console.log(descriptions);
        this.setState({
          proddescriptions: jsonDataFromXml.getElementsByTagName("description")
        });
      });
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <div>
            <ul style={{ listStyle: "none" }}>
              {this.state.proddescriptions.map((item, index) => {
                return (
                  <li key={item.attributes["description-id"]}>
                    {item.attributes["description-id"]}
                    <input
                      type="text"
                      size="5"
                      maxlength="3"
                      value={item.attributes["display-sequence"]}
                      onChange={this.handleChange}
                    />
                    &nbsp; {item.value}
                  </li>
                );
              })}
            </ul>
          </div>

          <input type="submit" name="submit" value="Submit" id="btnsubmit" />
        </form>
      </div>
    );
  }
}

export default function App() {
  return (
    <div className="App">
      <h4>Sort list by updating the number in the textbox</h4>

      <Sort_Descr />
    </div>
  );
}
l7wslrjt

l7wslrjt1#

handleChange中,你需要根据改变的值更新状态。同样在handleSubmit中,你需要添加e.preventDefault(),这样它就不会刷新页面。

import "./styles.css";
import React from "react";
import XMLParser from "react-xml-parser";

const data = `<?xml version="1.0"?>
<Category>
<description description-id="11" display-sequence="2">testing</description>
<description description-id="15" display-sequence="5">Guide</description>
<description description-id="20" display-sequence="7">test</description>
<description description-id="25" display-sequence="10">Guide</description>
<description description-id="30" display-sequence="12">test</description>
</Category>
</xml>`;

const REQUEST_URL = "";

const axios = {
  get: () =>
    new Promise((resolve) => {
      setTimeout(resolve, 1000, { data });
    })
};

class Sort_Descr extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      proddescriptions: [],
      proddescription_id: "",
      display_sequence: ""
    };
    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }
  handleChange(id, value) {
    const proddescriptions = [...this.state.proddescriptions];
    proddescriptions.find(
      (desc) => desc.attributes["description-id"] === id
    ).attributes["display-sequence"] = value;
    this.setState({
      proddescriptions
    });
  }
  componentDidMount() {
    this.getlistofdescriptions();
  }

  getlistofdescriptions() {
    axios
      .get(REQUEST_URL, { "Content-Type": "application/xml; charset=utf-8" })
      .then((response) => {
        const jsonDataFromXml = new XMLParser().parseFromString(data);
        const descriptions = jsonDataFromXml.getElementsByTagName(
          "description"
        );
        this.setState({
          proddescriptions: descriptions
        });
      });
  }

  handleSubmit(e) {
    e.preventDefault();
    const jsonDataFromXml = new XMLParser().parseFromString(data);
    jsonDataFromXml.children = this.state.proddescriptions;
    console.log(new XMLParser().toString(jsonDataFromXml));
    // submit data on success
    // reload object by calling this.getlistofdescriptions();
  }

  render() {
    return (
      <div>
        <form onSubmit={this.handleSubmit}>
          <div>
            <ul style={{ listStyle: "none" }}>
              {this.state.proddescriptions.map((item, index) => {
                return (
                  <li key={item.attributes["description-id"]}>
                    {item.attributes["description-id"]}
                    <input
                      type="text"
                      size="5"
                      maxLength="3"
                      value={item.attributes["display-sequence"]}
                      onChange={(e) =>
                        this.handleChange(
                          item.attributes["description-id"],
                          e.target.value
                        )
                      }
                    />
                    &nbsp; {item.value}
                  </li>
                );
              })}
            </ul>
          </div>

          <input type="submit" name="submit" value="Submit" id="btnsubmit" />
        </form>
      </div>
    );
  }
}

export default function App() {
  return (
    <div className="App">
      <h4>Sort list by updating the number in the textbox</h4>

      <Sort_Descr />
    </div>
  );
}

工作样品https://codesandbox.io/s/nice-wave-5md793

相关问题