1

I am trying to create a table to list all of my users in one go but I am having trouble getting it to display properly.

If I have two arrays to mix in (one of column names and one for where the main array user list should these go inside the return statement? I am stuck at the minute were it is either outputting nothing on a blank screen or throwing this error.

enter image description here

const columns = [
        { field: 'id', headerName: 'ID', width: 70 },
        { field: 'firstname', headerName: 'First name', width: 130 },
        { field: 'lastName', headerName: 'Last name', width: 130 },
        { field: 'userName', headerName: 'Username', width: 130},
        { field: 'userEmail', headerName: 'Email', width: 180 },
        { field: 'userRole', headerName: 'Role', width: 80}
    ];
    const rows = [
        { id: userLists.userID, firstname: userLists.userFirstName, lastName: userLists.userSurname,  userName: userLists.username, userEmail: userLists.userEmail, userRole: userLists.userRoleID },
    ];


    return (

        <div style={{ height: 400, width: '100%' }}>
        {userLists.map(( {}) => {
         return   <DataGrid rows={rows} columns={columns} pageSize={10} checkboxSelection />
     })}
        </div>
    );
}
1
  • How are you accessing the property userID on userLists and the same time mapping over it? Commented Apr 21, 2021 at 13:15

1 Answer 1

1

you should map your data this way :

const columns = [
        { field: 'id', headerName: 'ID', width: 70 },
        { field: 'firstname', headerName: 'First name', width: 130 },
        { field: 'lastName', headerName: 'Last name', width: 130 },
        { field: 'userName', headerName: 'Username', width: 130},
        { field: 'userEmail', headerName: 'Email', width: 180 },
        { field: 'userRole', headerName: 'Role', width: 80}
    ];
    const rows =userLists.map(user=>({ id: user.userID, firstname: user.userFirstName, lastName: user.userSurname,  userName: user.username, userEmail: user.userEmail, userRole: user.userRoleID }));


    return (

        <div style={{ height: 400, width: '100%' }}>
        <DataGrid rows={rows} columns={columns} pageSize={10} checkboxSelection />
        </div>
    );
}
Sign up to request clarification or add additional context in comments.

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.