【问题标题】:How can i display API response data in a table如何在表格中显示 API 响应数据
【发布时间】:2019-12-06 02:35:30
【问题描述】:

我有以下代码在 reactJs 中使用 axios 从 API 获取数据

import React, { Component } from 'react';
import axios from 'axios';

export default class App extends Component {
  constructor(props) {
      super(props);
      this.state = {
          person: {}
      }
   }      

  componentDidMount() {
      axios.get('URL', {
        method: 'GET',
        headers: {
        'api-key': 'api-key goes here'
        }
    })
      .then((response) => {
          this.successShow(response);
      })
      .catch((error) => {
          this.successShow(error);
      });
  }
  successShow(response) {
      this.setState({
          person: response.data
      });
  }

  render() {
      return (
          <div>
            <h2>Welcome to React</h2>
            <p>{JSON.stringify(this.state.person.data)}</p>
            <div>{this.member}</div>
          </div>
         );
        }
   }

结果如下图所示

{"Table":[{"id":1001,"first_name":"Adam","last_name":"Quda",.....}]}

我想将数据显示为

 First Name   Last Name
    Adam          Quda
     .             .
     .             .

我试过this.state.person.data.first_name,{this.state.person.data.Table[].first_name},但有时我会出错

未定义函数名first_name 或Table..

【问题讨论】:

    标签: reactjs api axios


    【解决方案1】:

    使用地图循环遍历您的数据,为每个对象创建一行。更改显示成功以将数组保存为状态,而不是对象。

    successShow = response => {
      this.setState({
        // response format {"Table":[{"id":1001,"first_name":"Adam","last_name":"Quda",.....}]}
        person: response.data && response.data.Table
      });
    };
    

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.21.1/babel.min.js"></script>
    <style>
    table {
      font-family: arial, sans-serif;
      border-collapse: collapse;
      width: 100%;
    }
    
    td, th {
      border: 1px solid #dddddd;
      text-align: left;
      padding: 8px;
    }
    
    tr:nth-child(even) {
      background-color: #dddddd;
    }
    </style>
    <div id="root"></div>
    
    <script type="text/babel">
    
    class App extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          person: [{ id: 1001, first_name: "Adam", last_name: "Quda" }]
        };
      }
    
      render() {
        return (
          <div>
            <table className="table">
              <tr>
                <th>Id</th>
                <th>First name</th>
                <th>Last name</th>
              </tr>
              {this.state.person.map(person => (
                <tr>
                  <td>{person.id}</td>
                  <td>{person.first_name}</td>
                  <td>{person.last_name}</td>
                </tr>
              ))}
            </table>
          </div>
        );
      }
    }
    
    ReactDOM.render(
        <App />,
        document.getElementById('root')
    );
    </script>

    【讨论】:

    • 谢谢 使用API​​的axios数据怎么样?替换此行 `person: response.data && response.data.Table` 并以{person.first_name}? 访问它
    • response.data.Table 是一个数组来访问第一个对象做response.data.Table[0]
    • 你的意思是response.data.Table.first_name?我想我试过了,但我得到了未定义的函数名表,你能用 api/axios get 函数添加吗?
    • @Kuku response.data.Table 是一个数组来获取第一个对象使用response.data.Table[0] 来获取名称response.data.Table[0].first_name
    • 我得到未定义的函数 Tablr
    猜你喜欢
    • 2022-09-25
    • 2020-07-30
    • 2019-01-01
    • 2018-03-07
    • 2020-12-10
    • 2019-12-29
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多