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;
// `dataKeys`, when present, take precedence over the accessor: they read the
// raw product fields directly, so an accessor's constant fallback (e.g. the
// purity column's "Ungraded") can't mask an otherwise-empty column.
const hasData = rows.some((row) => {
if (dataKeys.length > 0) {
const original = row.original as Record<string, unknown>;
return dataKeys.some((key) => hasValue(original[key]));
}
return hasValue(row.getValue(column.id));
});
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. A column with
meta.dataKeysis judged from those raw product fields (this takes precedence, so an accessor's constant fallback can't mask an empty column); otherwise accessor columns are read via each row's value. Columns that can't be hidden, and non-accessor columns withoutmeta.dataKeys(whose emptiness can't be determined), are never reported.