2026-10-28 18:14:38 +04:00

33 lines
1.4 KiB
JavaScript

export const getOrderedVisibleColumns = (table) => [
...table.getLeftVisibleLeafColumns(),
...table.getCenterVisibleLeafColumns(),
...table.getRightVisibleLeafColumns(),
];
export const getColumnVirtualizerOptions = ({ table }) => {
const columns = getOrderedVisibleColumns(table);
const leftColumns = table.getLeftVisibleLeafColumns();
const rightColumns = table.getRightVisibleLeafColumns();
const getColumnSize = (index) => columns[index]?.getSize() ?? 150;
return {
overscan: 3,
scrollPaddingStart: leftColumns.reduce((width, column) => width + column.getSize(), 0),
scrollPaddingEnd: rightColumns.reduce((width, column) => width + column.getSize(), 0),
estimateSize: getColumnSize,
getItemKey: (index) => columns[index].id,
measureElement: (element) => element ? getColumnSize(Number(element.getAttribute('data-index'))) : 150,
// Pinned indices must use visible positions: hidden pinned columns leave
// gaps in column.getPinnedIndex(), which MRT uses by default.
rangeExtractor: ({ startIndex, endIndex, overscan, count }) => {
const indexes = new Set();
for (let index = 0; index < leftColumns.length; index++) indexes.add(index);
for (let index = Math.max(0, startIndex - overscan); index <= Math.min(count - 1, endIndex + overscan); index++) {
indexes.add(index);
}
for (let index = count - rightColumns.length; index < count; index++) indexes.add(index);
return [...indexes].sort((a, b) => a - b);
},
};
};