Conditionally hide column
I'm currently re-using a table with 6 columns for an admin and a seller user. Basically what I want to achieve is that the admin can see all 6 columns while the seller can only see 4. Is there a way I can conditionally hide a column based on a data(bool) like the example below? Thank you.
const columns = [{
name: "Status",
selector: "status",
sortable: true,
right: false,
hideColumn: () => auth.hasRole(['admin']) ? true : false,
cell: row => <StatusLabel status={row.status} />
}]
One method is that you can turn columns into a function and then just pass params to include/exclude certain array elements. Just make sure you memorize it for performance reasons.
const columns = (admin) => [
admin && {
name: "Status",
selector: "status",
sortable: true,
right: false,
cell: row => <StatusLabel status={row.status} />
}
...
];
....
<DataTable
...
columns={columns(auth.hasRole(['admin']))}
// optionally memioze this for performance or use react useMemo
columns={memoize(() => columns(auth.hasRole(['admin'])))}
/>
The only issue I see here is that the data is still in data and this data is passed into RDT so just be aware that if you expose data using any of RDT's other features like selectableRows or expandableRows
There are ways other ways to hide columns using like using conditionalCellStyles or hide but you are limited to display: none which while it is removed from the flow the data could easily be viewed using the dev tools inspector.
https://github.com/jbetancur/react-data-table-component#161-columns
I am going to tag this as a feature because I think RDT could use a hideWhen to make things more convenient.