How to display json data with ReactJs as table

后端 未结 2 931
别那么骄傲
别那么骄傲 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

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

    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 ( {headerComponents} {rowComponents}
    ); }, generateHeaders: function() { var cols = this.props.cols; // [{key, label}] // generate our header (th) cell components return cols.map(function(colData) { return {colData.label} ; }); }, 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 {item[colData.key]} ; }); return {cells} ; }); } }); React.render(, document.getElementById('example'));

    提交回复
    热议问题