The table instance to inspect
The ids of hideable columns that have no data in any row
// A result set where no product has a CAS number or SDS link:
getEmptyHideableColumnIds(table); // ["cas", "sds"]
export function getEmptyHideableColumnIds<TData>(table: Table<TData>): string[] {
const rows = table.getCoreRowModel().flatRows;
const emptyColumnIds: string[] = [];
for (const column of table.getAllColumns()) {
if (!column.getCanHide()) continue;
const columnDef = column.columnDef;
// Opt-out for accessor columns whose value is an optional, separately-loaded
// derivation (e.g. price-trend); auto-hiding would drop a column the user
// opted into whenever no row currently has a value.
if (columnDef.meta?.skipEmptyHide) continue;
const isAccessorColumn = 'accessorKey' in columnDef || 'accessorFn' in columnDef;
const dataKeys = columnDef.meta?.dataKeys ?? [];
// A display column with no accessor and no declared dataKeys can't be judged.
if (!isAccessorColumn && dataKeys.length === 0) continue;
const hasData = rows.some((row) => {
if (isAccessorColumn) return hasValue(row.getValue(column.id));
const original = row.original as Record<string, unknown>;
return dataKeys.some((key) => hasValue(original[key]));
});
if (!hasData) emptyColumnIds.push(column.id);
}
return emptyColumnIds;
}
Returns the ids of hideable columns that contain no data in ANY row — across variant sub-rows and rows filtered out of the current view, not just the visible page — so callers can auto-hide columns irrelevant to a result set. Accessor columns (with
accessorKeyoraccessorFn) are read via each row's value; display columns without an accessor are read from the product fields named in theirmeta.dataKeys. Columns that can't be hidden, and display columns withoutmeta.dataKeys(whose emptiness can't be determined), are never reported.