getSize returning the wrong value? #3947
Replies: 5 comments 4 replies
-
In the code export const defaultColumnSizing = {
size: 150,
minSize: 20,
maxSize: Number.MAX_SAFE_INTEGER,
} The default size is fixed to 150px. If the content is wider than 150, the cell is enlarged, but the size of the column stays the same (150px) in behind. I don't know how to change this behavior. It effects the column re-sizing feature as well causing the glitch and delays while resizing. |
Beta Was this translation helpful? Give feedback.
-
I would like to share my work-around how I set the Firstly, I created a state hook for the columnSizing; and passed it into the table instance. const [columnSizing, setColumnSizing] = useState<ColumnSizingState>({});
// I omitted the other table features for simplicity
const instance = useTableInstance(table, {
columns,
data,
columnResizeMode: "onChange" as ColumnResizeMode,
state: {
columnSizing,
},
onColumnSizingChange: setColumnSizing,
}); Then, in the table component, I created a useEffect in order to get the actual useEffect(() => {
const headcells: NodeListOf<HTMLElement> = document.querySelectorAll(
".my-custom-table thead th"
);
let newColumnSizing = {};
headcells.forEach((headcell): void => {
headcell.style.width = `${headcell.clientWidth}px`;
newColumnSizing = {
...newColumnSizing,
[headcell.dataset.columnName as string]: headcell.clientWidth,
};
});
instance.setColumnSizing(newColumnSizing); // here I updated the table columnSizing state
}, [instance.options.data]); // it is table data, or, you can use the data hook passed into table instance as well In order the above hook to work, I set the table implementation like below. (I showed the necessary part only. I omitted the rest) <Table
className="my-custom-table"
style={{
width: `${
instance.getTotalSize() +
(instance.getAllLeafColumns().length + 1) * 3 // I got better result when I added this which is related with the tr css borders !
}px`,
}}
> <th
key={header.id}
data-column-name={header.id} // useEffect depends on that !
colSpan={header.colSpan}
style={{ width: header.getSize() }}
> And finally, I placed the html resizer element into the end of the {header.column.getCanResize() ? (
<div
{...{
onMouseDown: header.getResizeHandler(),
onTouchStart: header.getResizeHandler(),
className: `resizer ${
header.column.getIsResizing() ? "isResizing" : ""
}`,
}}
/>
) : null} And, don't forget the set the table.createDisplayColumn({
id: "field",
size: , // optional, but, if the column has fixed width set it !
minSize: , // optional, it is so important, otherwise the mouse left-move sets the width up to zero in behind
enableResizing: , // true, or false, optional, default is true
// omitted the other renderers (cell, header etc.)
}), I hope my above entry is useful for someone who suffers from weird resizing issue. |
Beta Was this translation helpful? Give feedback.
-
The problem still exists. getSize() is returning wrong values if table width is 100%. |
Beta Was this translation helpful? Give feedback.
-
I found the answer here. When I set the width value for The idea comes from |
Beta Was this translation helpful? Give feedback.
-
Today I met the problem when implementing sticky columns. // Table, TableHead etc are just simple wrappers around native html elements, nothing fancy there.
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<TableHead
key={header.id}
colSpan={header.colSpan}
// callback ref which does the heavy lifting
ref={(thElem) => columnSizingHandler(thElem, table, header.column)}
>
{header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
// nothing interesting here
</Table const columnSizingHandler = (thElem: HTMLTableCellElement | null, table: Table<any>, column: Column<any>) => {
if (!thElem) return;
// If you don't do that, there will be an infinite loop. We update the value in state only if the value has actually changed.
if (table.getState().columnSizing[column.id] === thElem.getBoundingClientRect().width) return;
table.setColumnSizing((prevSizes) => ({
...prevSizes,
// 100% accurate float-point width, even if table content is loaded async
[column.id]: thElem.getBoundingClientRect().width,
}));
}; |
Beta Was this translation helpful? Give feedback.
-
Hey, @tannerlinsley! Awesome working, I'm migrating a table from v7 to v8 and I'm finding it way better both in terms of API and performance!
I'm trying to make a table where users can make a horizontal scroll and have their pinned columns still visible. But to achieve this I need to get the size of the column to the left so I can pass it to the second column: left-[sizeOfTheFirstColumnPx].
But when I try to get the size, it always returns 150 for all the columns even though in practice it's bigger.
I'm using it wrong? This returned value should be the correspondent column size in pixels?
Beta Was this translation helpful? Give feedback.
All reactions