将行设置为数据网格 React
Set rows to datagrid React
我有一个组件,我从后端获取数据并尝试设置为 DataGrid
这是代码
export const CompaniesHouseContainer: React.FC<Props> = () => {
const classes = useStyles();
const companyHouseDataAccess = useCompaniesHouseDataAccess();
const [loadingCH, setLoadingCH] = useState(false);
const [companiesHouseResults, setComponentHouseResults] = useState<CompaniesHouseTestDto[]>();
const companyName = useRef<any>();
const postalCode = useRef<any>();
let rows: any[] = [];
const columns = [
{ field: 'companyName', headerName: 'Company Name', width: 70 },
{ field: 'addressLine1', headerName: 'Address Line 1', width: 130 },
{ field: 'addressLine2', headerName: 'Address Line 1', width: 130 },
{
field: 'city',
headerName: 'City',
width: 90,
},
{
field: 'postalCode',
headerName: 'Postal Code',
width: 160,
},
];
const handleSubmitForm = async (e: any) => {
e.preventDefault();
setLoadingCH(true);
const data = await companyHouseDataAccess.getCompaniesHouseResult(companyName.current.value, postalCode.current.value);
console.log(rows);
setComponentHouseResults(data);
setLoadingCH(false);
rows = data;
console.log('Here is rows');
console.log(rows);
};
return (
<div className="flex-grow flex flex-col min-h-0">
<div className="flex-grow flex flex-col min-h-0">
<div className="pr-0 mt-80" style={{ display: 'flex', justifyContent: 'center' }}>
<MuiCardWithAnimation component={AnimateSimple} recipe="slideRightInLarge" style={{ width: '70%' }} square>
<CardContent className="flex flex-col items-center justify-center p-32 md:p-48 md:pt-128 ">
<Typography variant="h6" className="md:w-full mb-32">
Search Company
</Typography>
<form name="recoverForm" onSubmit={handleSubmitForm} noValidate className="flex flex-col justify-center w-full">
<TextField
variant="outlined"
label="Company Name"
className="mb-16"
type="text"
name="companyName"
inputRef={companyName}
style={{ marginTop: '50px' }}
required
/>
<TextField
variant="outlined"
label="Postal Code"
className="mb-16"
type="text"
name="postalCode"
style={{ marginTop: '50px' }}
inputRef={postalCode}
required
/>
<Button
variant="contained"
color="primary"
className="w-224 mx-auto mt-16"
aria-label="Reset"
style={{ marginTop: '30px' }}
type="submit"
>
SUBMIT
</Button>
</form>
</CardContent>
</MuiCardWithAnimation>
</div>
</div>
<div className="flex-grow flex flex-col min-h-0">
<div className="pr-0 mt-20" style={{ display: 'flex', justifyContent: 'center', overflowY: 'scroll' }}>
<div style={{ height: 400, width: '100%' }}>
<DataGridPro rows={rows} columns={columns} pageSize={5} rowsPerPageOptions={[5]} checkboxSelection />
</div>
</div>
</div>
</div>
);
};
这里我尝试用来自 BE rows = data;
的数据填充行
这是来自 BE
的数据
[
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 6,
"matchConfidenceStrategyName": "FuzzyCompanyNameStrategy"
},
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 4,
"matchConfidenceStrategyName": "FuzzyNameStrictAddressStrategy"
},
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 60,
"matchConfidenceStrategyName": "OrderMatchingStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 12,
"matchConfidenceStrategyName": "FuzzyCompanyNameStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 9,
"matchConfidenceStrategyName": "FuzzyNameStrictAddressStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 30,
"matchConfidenceStrategyName": "OrderMatchingStrategy"
}
]
我看到 table 已呈现,但没有行
哪里会出问题?
更新常量不会触发组件的任何重新渲染。所以你需要将行存储在本地状态:
而不是:
let rows: any[] = [];
你应该有:
const [rows,setRows] = useState<any[]>([])
我有一个组件,我从后端获取数据并尝试设置为 DataGrid
这是代码
export const CompaniesHouseContainer: React.FC<Props> = () => {
const classes = useStyles();
const companyHouseDataAccess = useCompaniesHouseDataAccess();
const [loadingCH, setLoadingCH] = useState(false);
const [companiesHouseResults, setComponentHouseResults] = useState<CompaniesHouseTestDto[]>();
const companyName = useRef<any>();
const postalCode = useRef<any>();
let rows: any[] = [];
const columns = [
{ field: 'companyName', headerName: 'Company Name', width: 70 },
{ field: 'addressLine1', headerName: 'Address Line 1', width: 130 },
{ field: 'addressLine2', headerName: 'Address Line 1', width: 130 },
{
field: 'city',
headerName: 'City',
width: 90,
},
{
field: 'postalCode',
headerName: 'Postal Code',
width: 160,
},
];
const handleSubmitForm = async (e: any) => {
e.preventDefault();
setLoadingCH(true);
const data = await companyHouseDataAccess.getCompaniesHouseResult(companyName.current.value, postalCode.current.value);
console.log(rows);
setComponentHouseResults(data);
setLoadingCH(false);
rows = data;
console.log('Here is rows');
console.log(rows);
};
return (
<div className="flex-grow flex flex-col min-h-0">
<div className="flex-grow flex flex-col min-h-0">
<div className="pr-0 mt-80" style={{ display: 'flex', justifyContent: 'center' }}>
<MuiCardWithAnimation component={AnimateSimple} recipe="slideRightInLarge" style={{ width: '70%' }} square>
<CardContent className="flex flex-col items-center justify-center p-32 md:p-48 md:pt-128 ">
<Typography variant="h6" className="md:w-full mb-32">
Search Company
</Typography>
<form name="recoverForm" onSubmit={handleSubmitForm} noValidate className="flex flex-col justify-center w-full">
<TextField
variant="outlined"
label="Company Name"
className="mb-16"
type="text"
name="companyName"
inputRef={companyName}
style={{ marginTop: '50px' }}
required
/>
<TextField
variant="outlined"
label="Postal Code"
className="mb-16"
type="text"
name="postalCode"
style={{ marginTop: '50px' }}
inputRef={postalCode}
required
/>
<Button
variant="contained"
color="primary"
className="w-224 mx-auto mt-16"
aria-label="Reset"
style={{ marginTop: '30px' }}
type="submit"
>
SUBMIT
</Button>
</form>
</CardContent>
</MuiCardWithAnimation>
</div>
</div>
<div className="flex-grow flex flex-col min-h-0">
<div className="pr-0 mt-20" style={{ display: 'flex', justifyContent: 'center', overflowY: 'scroll' }}>
<div style={{ height: 400, width: '100%' }}>
<DataGridPro rows={rows} columns={columns} pageSize={5} rowsPerPageOptions={[5]} checkboxSelection />
</div>
</div>
</div>
</div>
);
};
这里我尝试用来自 BE rows = data;
这是来自 BE
的数据[
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 6,
"matchConfidenceStrategyName": "FuzzyCompanyNameStrategy"
},
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 4,
"matchConfidenceStrategyName": "FuzzyNameStrictAddressStrategy"
},
{
"companyName": "N LEDNER CARPENTRY & JOINERY LTD",
"companyNumber": "10603985",
"companyStatus": "active",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10603985",
"addressLine1": "38 Derby Drive",
"addressLine2": "Leybourne",
"country": "United Kingdom",
"locality": "West Malling",
"postalCode": "ME19 5FJ",
"region": "Kent",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 60,
"matchConfidenceStrategyName": "OrderMatchingStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 12,
"matchConfidenceStrategyName": "FuzzyCompanyNameStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 9,
"matchConfidenceStrategyName": "FuzzyNameStrictAddressStrategy"
},
{
"companyName": "LEDNERAL LTD",
"companyNumber": "10471091",
"companyStatus": "dissolved",
"companyType": "ltd",
"kind": "searchresults#company",
"companyProfileLink": "https://find-and-update.company-information.service.gov.uk//company/10471091",
"addressLine1": "Abergele Road",
"addressLine2": null,
"country": null,
"locality": "Colwyn Bay",
"postalCode": "LL29 8BF",
"region": "Conwy",
"sicCodes": null,
"matchConfidenceBand": "UNMATCHED",
"matchConfidenceScore": 30,
"matchConfidenceStrategyName": "OrderMatchingStrategy"
}
]
我看到 table 已呈现,但没有行
哪里会出问题?
更新常量不会触发组件的任何重新渲染。所以你需要将行存储在本地状态:
而不是:
let rows: any[] = [];
你应该有:
const [rows,setRows] = useState<any[]>([])