How to display json data with ReactJs as table

后端 未结 2 918
别那么骄傲
别那么骄傲 2020-12-05 12:18

I would like to display my json data with reactJs in the table, but I can\'t.

[
  {
    \"userId\": 1,
    \"id\": 1,
    \"title\": \"sunt aut facere repell         


        
相关标签:
2条回答
  • 2020-12-05 13:02

    You can use map to iterate over your JSON data

    class App extends React.Component {
      constructor(){
        super() 
          this.state = {
            data: []
          }
        
      }
      componentDidMount() {
        $.ajax({
           url: "http://jsonplaceholder.typicode.com/posts",
           type: "GET",
           dataType: 'json',
           ContentType: 'application/json',
           success: function(data) {
             
             this.setState({data: data});
           }.bind(this),
           error: function(jqXHR) {
             console.log(jqXHR);
           }.bind(this)
        })
      }
      render() {
        
            
        return (
          <table>
          <tbody>{this.state.data.map(function(item, key) {
                 
                   return (
                      <tr key = {key}>
                          <td>{item.userId}</td>
                          <td>{item.id}</td>
                          <td>{item.title}</td>
                          <td>{item.body}</td>
                      </tr>
                    )
                 
                 })}</tbody>
           </table>
        )
      }
    }
    
    ReactDOM.render(<App/>, document.getElementById('app'))
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.8/react-dom.js"></script>
    <div id="app"></div>

    0 讨论(0)
  • 2020-12-05 13:02

    are you aiming for a component to display data , you can do something like this

    <div id="example"></div>
    
    var cols = [
        { key: 'id', label: 'Id' },
        { key: 'userId', label: 'User' },    
        { key: 'title', label: 'Title' },
        { key: 'body', label: 'Body' }
    ];
    
    var data = [
      {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
      },
      {
        "userId": 1,
        "id": 2,
        "title": "qui est esse",
        "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
      }
    ];
    
    var Table = React.createClass({
    
    render: function() {
        var headerComponents = this.generateHeaders(),
            rowComponents = this.generateRows();
    
        return (
            <table>
                <thead> {headerComponents} </thead>
                <tbody> {rowComponents} </tbody>
            </table>
        );
    },
    
    generateHeaders: function() {
        var cols = this.props.cols;  // [{key, label}]
    
        // generate our header (th) cell components
        return cols.map(function(colData) {
            return <th key={colData.key}> {colData.label} </th>;
        });
    },
    
    generateRows: function() {
        var cols = this.props.cols,  // [{key, label}]
            data = this.props.data;
    
        return data.map(function(item) {
            // handle the column data within each row
            var cells = cols.map(function(colData) {
    
                // colData.key might be "firstName"
                return <td> {item[colData.key]} </td>;
            });
            return <tr key={item.id}> {cells} </tr>;
        });
    }
    });
    
    React.render(<Table cols={cols} data={data}/>,  document.getElementById('example'));
    
    0 讨论(0)
提交回复
热议问题