0

I am new to the React world and am struggling to get my JSON data to display in a table.

class GetUnassignedUsers extends React.Component {
    constructor () {
        super();
        this.state = {
            data:[]
        };
    }
    componentDidMount () {
        fetch("http://localhost/dashboard/?action=unassignedUsers.getUnassingedUsers", {
            credentials: 'same-origin'
        })
        .then(response => response.json())
        .then(json => this.setState({data: json}));
    }
    render () {
        console.log("teams", this.state.data.teams);
        console.log("unassignedUsers", this.state.data.unassignedUsers);
        var teams = this.state.data.teams;
        var unassignedUsers = this.state.data.unassignedUsers;
        return (
            <tr>
                <td>
                    {unassignedUsers.ID}
                </td>
                <td>
                    23/08/2015
                </td>
                <td>
                    Tradeprint
                </td>
                <td>
                    [email protected]
                </td>
                <td>
                    John Bloggs
                </td>
                <td>
                    Aberfeldy
                </td>
                <td>
                    AD9 8QR
                </td>
                <td>
                    Unassigned
                </td>
            </tr>
        );
    }
};

export default GetUnassignedClients;

I am accessing the JSON and returning it in the render method in the 2 console logs (console.log("teams", this.state.data.teams); and console.log("unassignedUsers", this.state.data.unassignedUsers);).

My issue is below that where I tidy up the dot notation by declaring the 2 variables for teams and unassignedUsers. Then in my return I want to produce a row for each JSON record with the unassignedUser ID in the first <td>.

2
  • 1
    Possible duplicate of Iterate over json atributes to form table in React? Commented Jan 30, 2017 at 15:18
  • I had a look at this question but it seemed to be quite different to mine despite the title being similar. The way their code is written is quite different. This question is using ES6 so there is quite a lot of differences in that alone. Also they are accessing JSON stored in a variable and not saved to state after being fetched from an external source. Commented Feb 1, 2017 at 10:12

3 Answers 3

1
var unassignedUsers = this.state.data.unassignedUsers; // Probably it returns array of users

So all you have to do:

var rows = unassignedUsers.map(function(user) {
    return (<tr>
        <td>{user.ID}</td>
        <td>{user.somethingElse}</td>
      </tr>)
});
return (
   <table>
   <tbody>
       {rows}
   </tbody>
   </table>
)
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah it returns an array of users. I have implemented this and now I get the error: Uncaught TypeError: Cannot read property 'map' of undefined I thought this was due to componentDidMount running after it tries (and fails) to render the first time - but then it re-renders the second time so surely that should then return the data?
Because you defined initial state like this: this.state = { data:[] }; But you are not using data as array you are using data as object and inside this object you have to give default value for unassignedUsers so this.state = { data: { unassignedUsers: [] } }; If you define initial state like this in first render unassignedUsers will be mapped as an empty array so there will not be any unexpected errors.
1

To create the table row dynamically, u need to iterate the json and create the ui items, like this:

return (
   <table>
     <tbody>
      {unassignedUsers.map((item,index)=>{
        return <tr key={index}>
            <td>
                {item.ID}
            </td>
            <td>
                23/08/2015
            </td>
            <td>
                Tradeprint
            </td>
            <td>
                [email protected]
            </td>
            <td>
                John Bloggs
            </td>
            <td>
                Aberfeldy
            </td>
            <td>
                AD9 8QR
            </td>
            <td>
                Unassigned
            </td>
        </tr>
      })  
     </tbody> 
   </table> 
);

Comments

0

In your case, I would use the existing components, like Reactable Table as a great jump start.

import React from "react";
import ReactDOM from "react-dom";;
import Reactable from "reactable";
var Table = Reactable.Table;
ReactDOM.render(
    <Table className="table" data={[
        { Name: 'Griffin Smith', Age: 18 , Position: 'Schoolar'},
        { Age: 23,  Name: 'Lee Salminen', Position: 'Student'},
        { Age: 28, Name: 'Mike Kofs', Position: 'Developer' },
    ]} />,
    document.getElementById('app')
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.