简体   繁体   English

我如何从其余的 api 呈现表格

[英]How can i render the table from the rest api

{
  "firma": {
    "serialNo": 12345,
    "companyName": "facebook",
    "email": "admin@admin",
    "bankNo": 987,
    "adress": "london",
    "faturas": [
      {
        "fid": 123,
        "updateDate": [2021, 12, 14],
        "sellDate": 1241231,
        "price": 1500
      }
    ]
  }
}

This is the json that I get from my API.这是我从我的 API 获得的 json。 And I am trying to make a frontend for the given API using React:我正在尝试使用 React 为给定的 API 制作一个前端:

class UserComponents extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      users: []
    };
  }

  componentDidMount() {
    UserService.getFatura().then((response) => {
      this.setState({ users: response.data });
    });
  }

  render() {
    return (
      <div>
        <h2 className="text-center">Faturalar</h2>
        <div className="row">
          <table className="table table-striped table-bordered">
            <thread>
              <tr>
                <th>User serialNo</th>
                <th>User companyName</th>
                <th>User email</th>
                <th>User bankNo</th>
                <th>User adress</th>
              </tr>
            </thread>
            <tbody>
              {this.state.users.map((users) => (
                <tr key={users.serialNo}>
                  <td> {users.serialNo}</td>
                  <td> {users.companyName}</td>
                  <td> {users.email}</td>
                  <td> {users.bankNo}</td>
                  <td> {users.adress}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
    );
  }
}

export default UserComponents;

The problem is I cant get the faturas in my json to my table in react.问题是我无法将 json 中的 faturas 放到我的桌子上以进行反应。 I try some code but didnt work i am very new and need help in react.我尝试了一些代码但没有用我很新,需要帮助做出反应。 Please make it simple as you can.请尽可能简单。 note: firma and faturas have one to many relationship serialNo is the foreign key in faturas.注意:firma 和 faturas 是一对多的关系 serialNo 是 faturas 中的外键。

Your data have sub-field firma .您的数据具有子字段firma If you want print data first you must get this field.如果你想先打印数据,你必须得到这个字段。

Like this;像这样;

{this.state.users.map(({firma}) => ( // javascript can get property like this
  <tr key={firma.serialNo}>
  <td> {firma.serialNo}</td>
  <td> {firma.companyName}</td>
  <td> {firma.email}</td>
  <td> {firma.bankNo}</td>
  <td> {firma.adress}</td>
 </tr>
))}

or this;或这个;

{this.state.users.map((user) => (
  <tr key={user.firma.serialNo}>
  <td> {user.firma.serialNo}</td>
  <td> {user.firma.companyName}</td>
  <td> {user.firma.email}</td>
  <td> {user.firma.bankNo}</td>
  <td> {user.firma.adress}</td>
 </tr>
))}

If you want printing faturas, you can use this;如果你想打印faturas,你可以使用它;

{this.state.users.map((user) => (
  <tr key={user.firma.serialNo}>
  <td> {user.firma.serialNo}</td>
  <td> {user.firma.companyName}</td>
  <td> {user.firma.email}</td>
  <td> {user.firma.bankNo}</td>
  <td> {user.firma.adress}</td>
  <td> {user.firma.faturas.map((fatura) => (<div>
       <p>Fatura ID : {fatura.fid}</p>
       <p>Sell Date: {fatura.sellDate}</p>
       <p>Price: {fatura.price}</p>
     </div>)
  )}</td>
 </tr>
))}

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

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