Answer the question
In order to leave comments, you need to log in
How to specify row id property for DataGrid MaterialUI component?
I'm trying to use the DataGrid component from MaterialUI in my ReactJS project. When I try to display data in a table, I get an error:
Unhandled Rejection (Error): Material-UI: The data grid component requires all rows to have a unique id property.
A row was provided without id in the rows prop:
[{"id":1,"title":"test","comment":"test problem","status":"OPEN"},{"id":2,"title":"another","comment":"another problem","status":"OPEN"}
{
"tickets": [
{
"id": 1,
"title": "test",
"comment": "test problem",
"status": "OPEN"
},
{
"id": 2,
"title": "another",
"comment": "another problem",
"status": "OPEN"
},
{
"id": 3,
"title": "another ticket about problem",
"comment": "problem description",
"status": "open"
},
{
"id": 4,
"title": "just another ticket about problem",
"comment": "problem description",
"status": "open"
}
]
}
render () {
const {error, isLoaded, items} = this.state;
const columns = [
{ field: 'id', headerName: 'ID', width: 70 },
{ field: 'title', headerName: 'Title', width: 130 },
{ field: 'user.username', headerName: 'Username', width: 130 },
{ field: 'status', headerName: 'Status', width: 90 }
];
const rows = [items.map(item => item)];
if (error) {
return <Typography variant="h6" color="error">{error.message}</Typography>;
} else if (!isLoaded) {
return <Typography variant="h6" color="secondary">Loading...</Typography>;
} else {
/*return (
<ul>
{items.map(item => (
<li>{item.id} {item.title}</li>
)
)}
</ul>
);*/
console.log(rows);
return <DataGrid rows={rows} columns={columns}></DataGrid>;
}
}
"tickets": [
{
"id": 1,
"title": "test",
"comment": "test problem",
"status": "OPEN",
"user": {
"id": 2,
"username": "notadmin",
"first_name": "",
"last_name": "",
"email": ""
}
},
{
"id": 2,
"title": "another",
"comment": "another problem",
"status": "OPEN",
"user": {
"id": 1,
"username": "admin",
"first_name": "",
"last_name": "",
"email": ""
}
}
]
Answer the question
In order to leave comments, you need to log in
const rows = [items.map(item => item)];
And what about in the case of such nesting, when I only need its username from the user object?
I do everything according to the model from the official documentation for Material
valueGetter: params => params.row.user.username
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question