I want to show quantity of rows in a group title of Grid.
To achieve this I need to pass custom TableGroupRow.contentComponent.
I have access to fields like row.value inside my custom implementation of this component, but collapsed rows array is keyed by Symbol which is NOT exported from the library, so I need to write ugly code like this:
const finder = s => s.toString().includes( "collapsed" );
let COLLAPSED_KEY = "";
const setCollapsedKey = col => {
const smb = Object.getOwnPropertySymbols( col );
const n = smb.find( finder );
if( !n && process.env.NODE_ENV !== "production" ){
throw new Error( "Collapsed key not found" );
}
COLLAPSED_KEY = n;
}
const GroupContentComponent = ({ column, row }) => {
if( !COLLAPSED_KEY ){
setCollapsedKey( row );
}
const collapsed = row[ COLLAPSED_KEY ];
return (
<TableGroupRow.Content column={column} row={row}>
{row.value} {Array.isArray( collapsed ) ? `(${collapsed.length})` : ""}
</TableGroupRow.Content>
);
}
Is it possible to export this symbol?
Hi,
Currently, the constant you are talking about is private. However, you can implement your scenario using our React Core API.
As you can see in documentation, the IntegratedGrouping plugin exports the getCollapsedRows getter that returns collapsed rows for a specific row. To use this getter create a custom plugin as follows:
const ItemCounter = () => (
<Plugin name="ItemCounter">
<Template
name="tableCell"
predicate={({ tableRow }) => isGroupTableRow(tableRow)}
>
{params => (
<TemplateConnector>
{({ getCollapsedRows }) => {
const updatedParams = {
...params,
tableRow: {
...params.tableRow,
row: {
...params.tableRow.row,
collapsedRows: getCollapsedRows(params.tableRow.row) || []
}
}
};
return <TemplatePlaceholder params={updatedParams} />;
}}
</TemplateConnector>
)}
</Template>
</Plugin>
);
Here we calculate collapsed rows and update parameters that are used for group cell rendering. This guide contains detailed information about it.
I've created a sample that shows this approach in action.
We'll discuss how to simplify your use-case implementation in the future.
Thank you for this approach, it works ok. Next question: is it possible to access summaries inside GroupRow somehow? I want to display summary not only under groups, but also in group titles. I understand, that I can use your approach with getCollapsedRows and do the work of IntegratedSummary manually. But is there a way to use already calculated sums of existing Summary plugin?
You are welcome!
As for information about a summary, yes, it will be available if you are using the 'IntegratedSummary' plugin before the 'TableGroupRow' one. As you can see in our documentation, the 'IntegratedSummary' plugin exports the 'groupSummaryValues' getter that contains the information about group summary values. This getter will be available inside the 'TemplateConnector' component.
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests.