MUI DataGrid 显示 Airtable 数据
MUI DataGrid display Airtable data
我是用Airtable作为基础数据的临时数据库,用Material-UIDataGrid来展示。我能够从 Airtable 检索数据并将其显示在 console.log 中,但无法将其发送到 DataGrid 进行显示。
这是 MUI DataGrid - https://mui.com/components/data-grid/#mit-version
这是 Airtable npm 包 https://www.npmjs.com/package/airtable
This is the console.log displaying the data retrieved.
This is where the error is on the site, it only displays the column header, not the data.
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/styles";
import { Container, Grid } from "@material-ui/core";
import { DataGrid } from "@mui/x-data-grid";
import Airtable from "airtable";
const base = new Airtable({ apiKey: "*****" }).base(
"*****"
);
const useStyles = makeStyles(
(theme) => ({
container: {
marginTop: "10vh",
},
}),
{}
);
const col = [
{ field: "id", headerName: "ID", width: 90 },
{ field: "name", headerName: "Institution Name", width: 150 },
{ field: "town", headerName: "Location Town", width: 150 },
{ field: "state", headerName: "State", width: 150 },
];
export default function CE3() {
const classes = useStyles();
const [rows, setRows] = useState([]);
useEffect(() => {
base("Table 1")
.select({
maxRecords: 3,
view: "Grid view",
})
.eachPage((records, fetchNextPage) => {
setRows(records);
console.log(records);
fetchNextPage();
});
}, []);
return (
<div>
<Container fixed={true} className={classes.container}>
Select at least <strong>1</strong> college
<br></br>
{rows && (
<DataGrid
rows={rows}
columns={col}
pageSize={5}
rowsPerPageOptions={[5]}
checkboxSelection
disableSelectionOnClick
/>
)}
</Container>
</div>
);
}
您必须为 DataGrid 的父级设置高度,以便 DataGrid 可以匹配此高度(参见 https://mui.com/components/data-grid/layout/#predefined-dimensions)
或将 autoHeight
属性设置为 true(参见 https://mui.com/components/data-grid/layout/#auto-height)
我是用Airtable作为基础数据的临时数据库,用Material-UIDataGrid来展示。我能够从 Airtable 检索数据并将其显示在 console.log 中,但无法将其发送到 DataGrid 进行显示。
这是 MUI DataGrid - https://mui.com/components/data-grid/#mit-version
这是 Airtable npm 包 https://www.npmjs.com/package/airtable
This is the console.log displaying the data retrieved.
This is where the error is on the site, it only displays the column header, not the data.
import React, { useEffect, useState } from "react";
import { makeStyles } from "@material-ui/styles";
import { Container, Grid } from "@material-ui/core";
import { DataGrid } from "@mui/x-data-grid";
import Airtable from "airtable";
const base = new Airtable({ apiKey: "*****" }).base(
"*****"
);
const useStyles = makeStyles(
(theme) => ({
container: {
marginTop: "10vh",
},
}),
{}
);
const col = [
{ field: "id", headerName: "ID", width: 90 },
{ field: "name", headerName: "Institution Name", width: 150 },
{ field: "town", headerName: "Location Town", width: 150 },
{ field: "state", headerName: "State", width: 150 },
];
export default function CE3() {
const classes = useStyles();
const [rows, setRows] = useState([]);
useEffect(() => {
base("Table 1")
.select({
maxRecords: 3,
view: "Grid view",
})
.eachPage((records, fetchNextPage) => {
setRows(records);
console.log(records);
fetchNextPage();
});
}, []);
return (
<div>
<Container fixed={true} className={classes.container}>
Select at least <strong>1</strong> college
<br></br>
{rows && (
<DataGrid
rows={rows}
columns={col}
pageSize={5}
rowsPerPageOptions={[5]}
checkboxSelection
disableSelectionOnClick
/>
)}
</Container>
</div>
);
}
您必须为 DataGrid 的父级设置高度,以便 DataGrid 可以匹配此高度(参见 https://mui.com/components/data-grid/layout/#predefined-dimensions)
或将 autoHeight
属性设置为 true(参见 https://mui.com/components/data-grid/layout/#auto-height)