Replies: 1 comment
-
Might be unrelated to your specific needs, but I hide columns with empty cells with a small loop over the rows: header: ({ column, table }) => {
const { rows } = table.getRowModel()
if (rows.every(row => !row.original.cellData)) {
return null
}
return (
<DataTableColumnHeader
title='Data'
column={column}
/>
)
}, where |
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
-
We have a setup where the user should select between different datasets. Per data set some columns are empty because they are irrelevant.
Since table state resets on data change, the obvious solution for me was to calculate the initial state
Now, I wanted to try and write a plugin hook that adds the same functionality in a more general way.
The idea is to add a column property hideWhenEmpty and if it's true and all values (taken from the accessors) are falsy, the column should be discarded.
I was looking into into the hooks useControlledState, visibleColumns and visibleColumnsDeps:
visibleColumns lets us filter the visible columns, so it should be the obvious hook, but I cant memoize the visible columns using useMemo, since react complains that it's run inside of another hook.
visibleColumnsDeps allows me to useMemo, but it receives an empty array and I can't find documentation about the intended use.okay, it's the dependencies, when visibleColumns should update. Makes sense.The third option would be to useControlledState and update hiddenColumns, but the instance received here, does not yet contain allColumns and rows. This means, i need to duplicate the logic id and accessor logic to get the id of the column to hide and the value of the columns to check for falsy values.
So, what would be the proper point to hook into the column processing and throw away empty columns?
I'm currently thinking about two hooks in a shared closure. One, that hooks into allColumnDeps and calculates the columns to be dropped and a second one on visibleColumns that uses the calculation and drops the columns, but that feels messy.
Is there a better hook or did I miss something?
Beta Was this translation helpful? Give feedback.
All reactions