How to show and hide some columns on React Table?
Solution 1
In column there is a property show
.
show: true, // can be used to hide a column
.
Make it false to hide the particular column. Keep user's check-box values in the state.
https://react-table.js.org/#/story/readme
Note: The column property show
is deprecated. Use initialstate.hiddenColumns
instead.
Check: https://github.com/tannerlinsley/react-table/issues/1804
Solution 2
Code example:
const columns = useMemo(
() => [
{
Header: 'DeviceId',
accessor: 'DeviceId',
},
{
Header: 'Serial_Number',
accessor: 'Serial_Number',
},
{
Header: 'Type',
accessor: 'Type',
},
],[]
);
const initialState = { hiddenColumns: ['DeviceId'] };
useTable({
columns,
data,
initialState
},
useSortBy
)
Related videos on Youtube
Tom
Updated on March 31, 2022Comments
-
Tom about 2 years
I have created a React Table. I want to hide or show some columns in my table by user action. By default all columns should be visible but we will have some check boxes to hide or show some columns.
Suppose I want to show 4 of the 8 columns. Please suggest me an easy technique to achieve this.
My Column Header Array is
const columns = [ { Header: 'Column 1', columns: [ { Header: 'S Column 1', accessor: 'firstName' } ] }, { Header: 'Column 2', columns: [ { Header: 'S Column 2', accessor: 'firstName' } ] }, { Header: 'Column 3', columns: [ { Header: 'S Column 3', accessor: 'firstName' } ] }, { Header: 'Column 4', columns: [ { Header: 'S column 4', accessor: 'firstName' } ] }, { Header: 'Column 5', columns: [ { Header: 'S column 5', accessor: 'firstName' } ] }, { Header: 'Column 6', columns: [ { Header: 'S column 6a', accessor: 'firstName' }, { Header: 'S column 6b', accessor: 'firstName' }, { Header: 'S column 6c', accessor: 'firstName' }, { Header: 'S column 6d', accessor: 'firstName' } ] }, { Header: 'Column 7', columns: [ { Header: 'S column 7', accessor: 'firstName' } ] }, { Header: 'Column 8', columns: [ { Header: 'S Column 8a', accessor: 'firstName' }, { Header: 'S Column 8b', accessor: 'firstName' }, { Header: 'S Column 8c', accessor: 'firstName' }, { Header: 'S Column 8d', accessor: 'firstName' } ] }, ];
Please help me to achieve this feature by easiest technique.
If you can, you can show a demo on codesandbox.
-
unknown_boundaries over 5 yearsis the styling css to visually hide wouldn't work?
-
Admin over 5 yearsNo I think it will not work only by css. If you can, you can show a demo.
-
-
Admin over 5 yearsCan you please show me a demo on Code Sandbox. I am a beginner in React.
-
Admin over 5 yearsCan you please somehow show a demo how can I disable a column if the checkbox is unchecked?
-
Admin over 5 yearsThanks a lot. One further question. You have used the checkbox for First Name. But I want to do for the entire name. If I disable name both first and last name will be disabled. Can you kindly make this last change in your demo?
-
Janaka Chathuranga over 5 yearsLol. @Scooby, You should really study a little bit about react and it's concept ;) I did the change in the sandbox. codesandbox.io/s/03x3r0vx1l . Check it out.!
-
Kris Lamote over 5 yearsthanks! saved me some searching - the former storybook docs seem to have disappeared
-
Andre DiCioccio about 4 yearsThanks. Appreciate the detail also regarding hiddenColumns
-
Stuart Hallows about 3 yearsThere's an example on the react table docs covering exactly this case react-table.tanstack.com/docs/examples/column-hiding