-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DataGrid] How to get the visible columns? #1507
Comments
@marcellemcm Perfect, this is a continuation of #1435. @dtassone This time, an export flag on the column definition won't cut it. We have a dynamic use case |
Well I believe @marcellemcm is expecting a handler to get the new visible columns when one gets hidden? FYI, we have an event |
@dtassone Do you mean, have a callback to know what are the visible rows (to store them in a React state) so they can then be provided as an argument to the export feature? |
Yes to respect our pattern, we should have those props so users can hook on to those events easily and do something... FYI The visible columns are already in the grid state. And the export feature already uses the visible columns. |
@dtassone The more we move forward, the more I wonder if the one callback per state pattern is scaling. It feels like there is an endless need for adding new props. But maybe I'm wrong, and it's only because we don't yet have a high coverage. In any case, I think that we should explore all our options. For instance, we could imagine having a state callback selector, allowing developers to cherrypick, sub parts of the state and replicate them from the data grid (being the source of truth in an uncontrolled mode) to a parent state. Isn't what we had onStateChange for? The alpha stage is perfect for this type of exploration, it's now that we can freeze what pattern works and which one doesn't. |
The idea is to make the component easy to use and approachable to users so they get where they want in a rapid manner.
We already use this approach internally, as we use selectors and useEffect on the gridState. |
@dtassone a couple of thoughts:
export default function DataGridDemo() {
const [visibleColumns, setVisibleColumns] = React.useState();
console.log("render"); // Only render when the visible columns changes.
return (
<div style={{ height: 400, width: "100%" }}>
<DataGrid
onStateChange={({ state }) => {
const newVisibleColumns = visibleGridColumnsSelector(state);
setVisibleColumns(newVisibleColumns);
}}
rows={rows}
columns={columns}
pageSize={5}
checkboxSelection
/>
</div>
);
} It also seems what you wanted to do initially #190 (comment) :p.
|
Well ATM you need to give a new ref to override the change done by grid.
I don't see any issue in having a reasonably long list of props as long as it is well documented. export default function DataGridDemo() {
const apiRef = useApiRef();
const visibleColumns = useGridSelector(apiRef, visibleGridColumnsSelector);
useEffect(()=> {
//Do something when visible columns changes.
}, [visibleColumns]);
return (
<div style={{ height: 400, width: "100%" }}>
<DataGrid
rows={rows}
columns={columns}
pageSize={5}
checkboxSelection
/>
</div>
);
} |
Agree, this is the problematic behavior I wanted to go after, it's not controlled in the definition of the term, but it's close (IMHO, not close enough).
Yeah, why not, I don't see a strong reason for not doing it either. Initially, I was defending the idea of having one prop per controllable state. I have only moved my argumentation to the other side so we could keep a healthy discussion between the pros and cons. |
@dtassone Loved this approach #1507 (comment), but apiRef is being created as |
I'm closing for ##1412 (comment). This should solve the pain. |
Summary 💡
I am using Data Grid and I have a requirement to export to csv, xls and txt, only the data that is visible in the table.
When clicking on the rendered Column menu component by clicking on the 3-point "kebab" icon in the column headers and then show columns/Hide.
In the documentation I didn't find any props to show the column parameters and get the columns visible when this event was triggered .
I know that there is an ApiRef.Column on the X-Grid. But how to know this information in the DataGrid?
The text was updated successfully, but these errors were encountered: