Display skeleton rows during initial load #2386
-
Hi! I want to display an arbitrary number of rows whilst fetching the first page. Here's what I got: Visually it is what I want. But the code doesn't seem any good. {loading &&
Array.from({ length: skeletonRows }).map(() => {
return headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<td {...column.getHeaderProps()}>
<Skeleton />
</td>
))}
</tr>
))
})} Does anyone have a better approach to this? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 16 replies
-
Hi, Recently I had added Skeleton loading to my table. First I attempted what you're doing but I quickly went ahead with other approach. The idea is to change the data & columns array passed to useTable if the content is loading. The following code create 30 rows (you can change according to your requirement) with empty data, and replace each cell with Skeleton component. const Table = (columns, data) => {
...
const tableData = React.useMemo(
() => (loading ? Array(30).fill({}) : data),
[loading, data]
);
const tableColumns = React.useMemo(
() =>
loading
? columns.map((column) => ({
...column,
Cell: <Skeleton />,
}))
: columns,
[loading, columns]
);
useTable({ columns: tableColumns, data: tableData });
...
} |
Beta Was this translation helpful? Give feedback.
-
Unfortunately this give me issues with Here is how I'm using with ShadCN
|
Beta Was this translation helpful? Give feedback.
-
This is my solution for working with grouped columns |
Beta Was this translation helpful? Give feedback.
-
I did this in the table body. Keep Table header as it is and add condition for loading only into the body : isLoading
? (
<TableBody>
{renderSkeletonRows()}
</TableBody>)
: table.getRowModel().rows.length === 0
? null
: (
<LYTableBody
rowVirtualizer={rowVirtualizer}
table={table}
tableState={tableState}
tableRef={tableRef}
onSelect={onSelect}
onMouseDown={onMouseDown}
onTouchStart={onTouchStart}
onTouchEnd={onTouchEnd}
onDoubleClick={onDoubleClick}
/>
) and renderSkeletonRows is this one : const renderSkeletonRows = () => (
<Fragment>
{/* Render 10 skeleton rows as a placeholder while loading */}
{Array.from(new Array(10)).map((_, index) => (
<TableRow key={index}>
{/* Render skeleton cells for each visible column */}
{table.getVisibleLeafColumns().map((column) => (
<TableCell key={column.id}>
<Skeleton variant="rounded" width="100%" height={8} />
</TableCell>
))}
</TableRow>
))}
</Fragment>
); Table and Skeleton are custom components in my framework because I have completely remove any MUI dependencies You can find full code for this here : /~https://github.com/fblettner/liberty/blob/main/frontend/src/components/forms/FormsTable/views/TableGrid.tsx |
Beta Was this translation helpful? Give feedback.
Hi,
Recently I had added Skeleton loading to my table. First I attempted what you're doing but I quickly went ahead with other approach.
The idea is to change the data & columns array passed to useTable if the content is loading. The following code create 30 rows (you can change according to your requirement) with empty data, and replace each cell with Skeleton component.
The good thing is your column headers/footers are intact.