๋ฐ์ํ
Intro
์๋ ํ์ธ์. ์ด๋ฒ ์๊ฐ์๋ TableVirtuoso ์ฌ์ฉ ์ค ๋ฐ์ดํฐ๊ฐ ๋น ๋ฐฐ์ด์ผ ๋ EmptyPlaceholder ์์ฑ์ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค.
Example
ํ์ฌ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค๊ณ ๊ฐ์ ํด๋ด ์๋ค. ์ด ์ํ์์ dataList๊ฐ null์ด๊ฑฐ๋ ๋น๋ฐฐ์ด์ผ ๋ ํน์ ๋ฐ์ดํฐ๊ฐ ๋น์ด์๋ค๋ ํน์ ํ ์คํธ๋ฅผ ํ์ํด๋ณด๊ฒ ์ต๋๋ค.
const VirtuosoTableComponents = (totalColSpan: number): TableComponents<any> => ({
Scroller: React.forwardRef<HTMLDivElement>((props, ref) => (
<TableContainer {...props} ref={ref} />
)),
Table: (props) => (
<Table {...props} sx={{ borderCollapse: 'separate', tableLayout: 'fixed' }} />
),
TableHead: React.forwardRef<HTMLTableSectionElement>((props, ref) => (
<TableHead {...props} ref={ref} />
)),
TableRow,
TableBody: React.forwardRef<HTMLTableSectionElement>((props, ref) => (
<TableBody {...props} ref={ref} />
))
});
const VirtualizedTable = <T extends TypeRES>({
headers,
rows,
}: Props<T>) => {
const theme = useTheme();
const [dataList, setDataList] = useState<T[]>(rows);
useEffect(() => {
setDataList(rows);
}, [rows]);
let totalColSpan = 1;
headers?.forEach(header => {
totalColSpan += header.subHeaders?.length ?? 1;
});
const memoizedVirtuosoTableComponents = useMemo(
() => VirtuosoTableComponents(totalColSpan),
[totalColSpan]
);
const fixedHeaderContent = () => {
const filteredHeaders = headers?.filter(header => !header.hide);
return (
<>
...
</>
);
};
const rowContent = (_index: number, row: any) => {
// Row content
};
return (
<Box style={{ height: 600, width: '100%', backgroundColor: 'transparent', overflowX: 'auto' }} >
<TableVirtuoso
data={dataList}
components={memoizedVirtuosoTableComponents}
fixedHeaderContent={fixedHeaderContent}
itemContent={rowContent}
/>
</Box>
);
};
export default VirtualizedTable;
How to solve the problem
VirtuosoTableComponents ์ปดํฌ๋ํธ ์ ์ธ ์ EmptyPlaceholder ๋ฅผ ์ถ๊ฐํ์ฌ ํ์ฉํด์ฃผ๋ฉด ๋ฉ๋๋ค. <TableBody> ํ๊ทธ๋ถํฐ ํ์ฉํ์ฌ ๋น๋ฐฐ์ด์ผ ๋ ํ์ํ๊ณ ์ ํ๋ ํ ์คํธ ํน์ ์์ด์ฝ์ ๋ฃ์ด์ฃผ์๋ฉด ๋ฉ๋๋ค. ์ ๋ colSpan ์์ฑ๋ ์ถ๊ฐ๋ก ์ ๋ฌํ์ฌ ํ ์ด๋ธ์ header์ ๋ง๊ฒ colSpan๋ ์ ์ฉ๋๋๋ก ํ์์ต๋๋ค.
const VirtuosoTableComponents = (totalColSpan: number): TableComponents<any> => ({
Scroller: React.forwardRef<HTMLDivElement>((props, ref) => (
<TableContainer {...props} ref={ref} />
)),
Table: (props) => (
<Table {...props} sx={{ borderCollapse: 'separate', tableLayout: 'fixed' }} />
),
TableHead: React.forwardRef<HTMLTableSectionElement>((props, ref) => (
<TableHead {...props} ref={ref} />
)),
TableRow,
TableBody: React.forwardRef<HTMLTableSectionElement>((props, ref) => (
<TableBody {...props} ref={ref} />
)),
EmptyPlaceholder: React.forwardRef(() => {
return (
<TableBody>
<TableRow>
<TableCell align="center" colSpan={totalColSpan -1 }>
<Typography>๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.</Typography>
</TableCell>
</TableRow>
</TableBody>
);
}),
});
๋ฐ์ํ