我知道我必须使用getRowID,但是我如何使用statId
而不是id
呢?是独一无二的。或者为它们中的每一个生成一个id
属性?
错误:MUI:数据网格组件要求所有行都具有唯一的id
属性。或者,您可以使用getRowId
属性为每行指定一个自定义ID。在以下各行中提供了一个没有ID的行:{“statID”:813183,“Team ID”:3,“Season”:2021年,“Name”:“亚特兰大老鹰”,“Team”:“ATL”,“Wins”:41,“Losks”:31,“FieldGoalsMade”:3492.1,“FieldGoalsAttemted”:7468.1,“FieldGoalsPercentage”:55.6%,“TwoPointersMade”:2427.9,“TwoPointersAttemted”:4612.1,“TwoPointersPercent”:62.5,“Three PointersMade”:1064.2,“三分球命中率”:2856,“三分球命中率”:44.3,“三分球命中率”:1684.8,“三分球命中率”:2074.8,“三分球命中率”:96.5,“进攻篮板”:903.6,“防守篮板”:3002.2,“篮板”:3905.9,“助攻”:2065.3,“抢断”:598.1,“盖帽”:405.4,“失误”:1086.7,“个人犯规”:1655.1,“得分”:9733.2,“双打”:120.1,“三双”:1.4}
import { useState, useEffect } from 'react';
import type { NextPage } from 'next';
import Container from '@mui/material/Container';
import Box from '@mui/material/Box';
import { DataGrid, GridColDef, GridRowIdGetter } from '@mui/x-data-grid';
import { Grid, Paper, Typography } from '@mui/material';
import Skeleton from '@mui/material/Skeleton';
import { blue } from '@mui/material/colors';
import FormOne from './../src/FormOne';
import { TeamSeason } from './../src/lib/interfaces/TeamSeason';
const LoadingSkeleton = () => (
<Box
sx={{
height: 'max-content',
}}
>
{[...Array(10)].map((_, index) => (
<Skeleton variant="rectangular" sx={{ my: 4, mx: 1 }} key={index} />
))}
</Box>
);
const columns: GridColDef[] = [
{ field: 'statId', headerName: 'Stat ID' },
{ field: 'name', headerName: 'Name', width: 300 },
{ field: 'season', headerName: 'Season', width: 600 },
];
const Home: NextPage = () => {
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
setInterval(
() =>
fetch('https://localhost:7000/TeamSeason/2021')
.then((response) => response.json())
.then((data) => {
setData(data);
setLoading(false);
}),
3000
);
}, []);
return (
<Container
maxWidth={false}
sx={{
height: '100vh',
overflow: 'auto',
background: `linear-gradient(to right, ${blue[200]}, ${blue[500]})`,
}}
>
<Container maxWidth="lg" sx={{ mt: 3, mb: 3 }}>
<Grid container spacing={{ xs: 2, md: 3 }}>
<Grid item xs={12} md={6}>
<Paper sx={{ padding: 3 }}>
<FormOne data={data} />
</Paper>
</Grid>
<Grid item xs={12} md={6}>
<Paper sx={{ padding: 3 }}></Paper>
</Grid>
<Grid item xs={12}>
<Paper sx={{ padding: 3 }}>
<DataGrid
sx={{ height: '650px' }} // either autoHeight or this
rows={data}
columns={columns}
pageSize={10}
// autoHeight
rowsPerPageOptions={[10]}
disableSelectionOnClick
disableColumnMenu
disableColumnSelector
components={{
LoadingOverlay: LoadingSkeleton,
}}
loading={loading}
/>
</Paper>
</Grid>
</Grid>
</Container>
</Container>
);
};
export default Home;
export interface TeamSeason {
statId: number;
teamId: number;
season: number;
name: string;
team: string;
wins: number;
losses: number;
fieldGoalsMade: number;
fieldGoalsAttempted: number;
fieldGoalsPercentage: number;
twoPointersMade: number;
twoPointersAttempted: number;
twoPointersPercentage: number;
threePointersMade: number;
threePointersAttempted: number;
threePointersPercentage: number;
freeThrowsMade: number;
freeThrowsAttempted: number;
freeThrowsPercentage: number;
offensiveRebounds: number;
defensiveRebounds: number;
rebounds: number;
assists: number;
steals: number;
blockedShots: number;
turnovers: number;
personalFouls: number;
points: number;
doubleDoubles: number;
tripleDoubles: number;
}
5条答案
按热度按时间c2e8gylq1#
您可以在数据网格道具中使用以下内容:
GetRowID={(Row)=>row.statID}
j2cgzkjk2#
如果您使用的是MongoDB,则应该包括此行
getRowId={(row) => row._id}
,因为MongoDB的ID名为_id,这将查找_id选项并选择它作为唯一IDkknvjkwl3#
数据网格需要一个id,因此您需要名为“id”的列,因此
nlejzf6q4#
这意味着您的响应对象列表没有id属性,从您的数据中找到任何Uniq键并将其用作id。示例:
9nvpjoqh5#
对于这个错误,请记住迭代在
keys
上运行,以充当每一行的唯一标识符。getRowId
属性希望从该行推断出唯一的id。以下是一个解决方案示例:
更多信息可以在MUI site here上找到。