英文:
How can I get child object value from object in MUI Datagrid
问题
I am using this method:
useEffect(() => {
PetService.getAll("/pets")
.then(response => {
setData(response.content);
})
}, [setData]);
and get this response:
{
"timestamp": 1678181099411,
"message": "Success",
"data": {
"content": [
{
"id": 1,
"name": "Tom",
"type": {
"id": 3,
"name": "Cat"
},
...
However, although I can get the content values properly, I cannot get the name
of the type
value on MUI Datagrid. I use this config for columns:
export const petColumns = [
{
field: "id",
},
{
field: "name",
headerName: "Pet Name"
{
field: "type.name",
headerName: "Type"
}
];
So, how can I access the value of the child object? I also tried "type['name']"
.
英文:
I am using this method:
useEffect(() => {
PetService.getAll("/pets")
.then(response => {
setData(response.content);
})
}, [setData]);
and get this response:
{
"timestamp": 1678181099411,
"message": "Success",
"data": {
"content": [
{
"id": 1,
"name": "Tom",
"type": {
"id": 3,
"name": "Cat"
},
...
However, although I can get the content values properly, I cannot get name
of the type
value on MUI Datagrid. I use this config for columns:
export const petColumns = [
{
field: "id",
},
{
field: "name",
headerName: "Pet Name"
{
field: "type.name",
headerName: "Type"
}
];
So, how can I access the value of the child object? I also tried "type['name']"
.
答案1
得分: 2
你不能使用field
属性访问嵌套对象,但可以使用value getter:
export const petColumns = [
{
field: "id",
},
{
field: "name",
headerName: "Pet Name"
},
{
field: "type.name",
headerName: "Type",
valueGetter: ({ row }) => row.type.name
}
];
英文:
You can't access nested objects with the field
property, but can use a value getter:
export const petColumns = [
{
field: "id",
},
{
field: "name",
headerName: "Pet Name"
{
field: "type.name",
headerName: "Type",
valueGetter: ({ row }) => row.type.name
}
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论