Replies: 1 comment 1 reply
-
This issue #23 pointed me to something that helped me to create something that actually works (although I had to read it multiple times to understand ;) ) It turns out the measurements taken by import {measureElement} from "@tanstack/react-virtual";
// in component
// init an array which will hold the maximum width for each column
const colWidths = React.useRef(Array.from({ length: columns }, () => 0));
const columnVirtualizer = useVirtualizer({
horizontal: true,
count: columns,
getScrollElement: () => parentRef.current,
estimateSize: () => 50,
overscan: 5,
measureElement: (element, entry, virtualizer) => {
const size = measureElement(element, entry, virtualizer); // call the default one
const index = Number.parseInt(element.getAttribute("data-index") as any);
if (colWidths.current[index] < size) {
colWidths.current[index] = size;
}
return colWidths.current[index];
},
}); This code makes the table act exactly as intended. I'm aware it could still change the width of a column if wider content appears, but this is acceptable in my use case. CodeSandbox: https://codesandbox.io/p/sandbox/tanstack-virtual-grid-dynamic-width-fpcqv9 I just wonder if there are some other better ways. |
Beta Was this translation helpful? Give feedback.
-
Hi!
My first thought was to create an issue, but I decided to ask here for help if I'm not doing something wrong.
Version
3.0.0-beta.68
What I'm trying to achieve
A grid with virtualised rows and columns where width is dynamic and height can be fixed.
What's the problem
The animation below shows how Column 0 recalculates width based on the widest cell (header). When I use also random lorem ipsum it gets worse with horizontal scroll but fixes over recalculations.
What I've tried already:
Mostly all of the examples. The closest one is React Example: Dynamic but it focuses on dynamic height.
I tried to change the example to a such state where I could have a single row and dynamic widths of the columns. The closest behaviour I could achieve is a state, where on the first render some columns have the default value from
estimateSize
function. After scrolling/being rendered again they recalculate properly.Demo of the problem: Codesandbox
(I recommend to horizontally scroll to column 3 where worse problem occurs, but gets fixed while scrolling vertically)
Perhaps someone will know what's the problem here or point out whether it is a bug. I will be glad for any help.
Beta Was this translation helpful? Give feedback.
All reactions