How to show and hide some columns on React Table?
Asked Answered
S

4

42

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.

Image of my table

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.

Sf answered 5/12, 2018 at 17:54 Comment(2)
is the styling css to visually hide wouldn't work?Worlock
No I think it will not work only by css. If you can, you can show a demo.Sf
P
45

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 showis deprecated. Use initialstate.hiddenColumns instead.

Check: https://github.com/tannerlinsley/react-table/issues/1804

Perigynous answered 5/12, 2018 at 18:2 Comment(7)
Can you please show me a demo on Code Sandbox. I am a beginner in React.Sf
Can you please somehow show a demo how can I disable a column if the checkbox is unchecked?Sf
Thanks 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?Sf
Lol. @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.!Perigynous
thanks! saved me some searching - the former storybook docs seem to have disappearedPolivy
Thanks. Appreciate the detail also regarding hiddenColumnsHulda
There's an example on the react table docs covering exactly this case react-table.tanstack.com/docs/examples/column-hidingKeratogenous
L
21

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
)

docs https://react-table-v7.tanstack.com/docs/api/useTable#table-options

Lucrative answered 7/10, 2021 at 8:19 Comment(0)
F
1

I have been using react-table and after looking at the internal code of useTable hook, got this solution;

initialState:{
 hiddenColumns:["id"] //array of column names to be hidden
}
Footboard answered 10/11, 2023 at 10:32 Comment(0)
G
1

For React Table V8, you can use the columnVisibility key.

ex.

initialState: {
  columnVisibility: {
    name: false;
    address: false;
  }`
}
Glorianna answered 23/11, 2023 at 2:56 Comment(1)
this one works. don't forget to give id's to your columns. like name, address, etc.Marris

© 2022 - 2024 — McMap. All rights reserved.