Replies: 1 comment
-
| Yes, you can do this by initializing // Build initial visibility state with all columns hidden const initialVisibility = Object.fromEntries( columns.map((col) => [col.id ?? col.accessorKey, false]) ) const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({ ...initialVisibility, // Only these columns are visible: name: true, email: true, }) const table = useReactTable({ data, columns, state: { columnVisibility }, onColumnVisibilityChange: setColumnVisibility, getCoreRowModel: getCoreRowModel(), })If you want this to be truly dynamic (e.g., columns are defined elsewhere and you always want hidden-by-default), you can create a helper: function hiddenByDefault( columns: ColumnDef<any>[], visible: string[] ): VisibilityState { const state: VisibilityState = {} for (const col of columns) { const id = (col as any).id ?? (col as any).accessorKey if (id) { state[id] = visible.includes(id) } } return state } // Usage: const [columnVisibility, setColumnVisibility] = useState( hiddenByDefault(columns, ['name', 'email']) )You don't need to override |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This is the api that defines whether column is visible.
I want to make all the columns hidden by default, meaning it will be visible only there is an attribute with value 'true' in provided columnVisibility object. Is there a way to override this api?
Beta Was this translation helpful? Give feedback.
All reactions