我是react的新手,我用strapi创建了一个后端,我测试了graphQl查询,并检查了它是否返回查询中所有节点的值。
现在,我使用Material UI DataGrid组件,我可以根据查询填充大多数节点,除了那些嵌套节点的节点。但是在我的控制台日志中,我返回了所有节点的值,所以我猜我的问题一定是在数据表的代码中?
我没有看到lead.leadSourceName的值和联系人节点的所有值,包括联系人节点中的notes节点,当单击一行时,所有这些都将在一个模态中打开。该模态确实打开,但值为undefined。再次,在控制台日志中,所有节点的所有值都存在。
这是我的代码:
import React, { useState } from 'react';
import { useQuery } from '@apollo/client';
import { DataGrid } from '@material-ui/data-grid';
import { Modal, Typography, Box } from '@material-ui/core';
import ReactMarkdown from 'react-markdown';
import gql from 'graphql-tag';
const GET_COMPANIES_QUERY = gql`
query {
companies {
data {
id
attributes {
companyName
status
internship
futureMinEmp
futureMaxEmp
leads {
leadSourceName
}
contact {
firstName
lastName
jobtitle
lastContact
nextContact
notes {
date
note
}
}
}
}
}
}
`;
const columns = [
{ field: 'companyName', headerName: 'Company Name', flex: 1, headerAlign: 'center' },
{ field: 'status', headerName: 'Status', flex: 1, headerAlign: 'center' },
{ field: 'internship', headerName: 'Internship', flex: 1, headerAlign: 'center' },
{ field: 'futureMinEmp', headerName: 'Min Employees', flex: 1, headerAlign: 'center' },
{ field: 'futureMaxEmp', headerName: 'Max Employees', flex: 1, headerAlign: 'center' },
{
field: 'leads',
headerName: 'Leads',
flex: 1,
headerAlign: 'center',
valueGetter: (params) => {
const leads = params.row.attributes?.leads;
if (typeof leads === 'object' && leads !== null && leads.hasOwnProperty('leadSourceName')) {
console.log('LEADS OBJECT:', leads); // add this line to log the value of the leads object
return leads.leadSourceName;
} else {
return 'No leads';
}
},
},
];
const CompaniesPage = () => {
const [selectedContact, setSelectedContact] = useState(null);
const { loading, error, data } = useQuery(GET_COMPANIES_QUERY);
if (loading) return <p>Loading...</p>;
if (error) return <p>Error :(</p>;
const companies = data?.companies?.data?.map((company) => {
console.log('Company:', company);
const leads = Array.isArray(company.attributes?.leads) && company.attributes.leads.length > 0
? company.attributes.leads.map((lead) => lead.leadSourceName).join(', ')
: 'No leads';
return {
id: company.id,
...company.attributes,
leads,
};
});
const handleRowClick = (params) => {
console.log('Selected contact:', params.row.contact);
setSelectedContact(params.row.contact);
};
const handleCloseModal = (event) => {
event.preventDefault();
setSelectedContact(null);
};
return (
<>
<DataGrid
rows={companies}
columns={columns}
autoHeight
onRowClick={handleRowClick}
getRowClassName={(params) => {
// add class name to row based on condition
return params.row.contact ? 'clickable-row' : '';
}}
/>
<Modal open={!!selectedContact} onClose={handleCloseModal} style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<Box bgcolor="background.paper" boxShadow={2} p={2} style={{ outline: 'none' }}>
<Typography variant="h4">
{selectedContact ? `${selectedContact.firstName} ${selectedContact.lastName}` : ''}
</Typography>
<Typography variant="subtitle1">{selectedContact ? selectedContact.jobtitle : ''}</Typography>
<Typography variant="subtitle2">{selectedContact ? `Last Contact: ${selectedContact.lastContact}` : ''}</Typography>
<Typography variant="subtitle2">{selectedContact ? `Next Contact: ${selectedContact.nextContact}` : ''}</Typography>
<ReactMarkdown>{selectedContact?.notes?.[0]?.note}</ReactMarkdown>
</Box>
</Modal>
</>
);
};
export default CompaniesPage
1条答案
按热度按时间bnl4lu3b1#
我猜
DataGrid
是在contact
对象上应用stringify
,使其在单元格中可显示,因此,您无法访问它的内部。试试这个:
我还建议从你的行中删除对象,以防止这种误解。