Using ShowPivotTableAction
ShowPivotTableAction
is a special action that allows you to export data from components that extend ListDataComponent
, such as DataGrid and TreeDataGrid, to a Pivot Table.
The simplest way to use this action is to define it within your dataGrid
and then associate it with a button.
<dataGrid id="tipInfoesDataGrid"
dataContainer="tipInfoesDc"
minWidth="100px"
width="100%">
<actions>
<action id="showPivotTableAction" type="pvttbl_showPivotTable"/>
</actions>
</dataGrid>
You can also create the action programmatically within your view controller and use it with a button.
@Subscribe
public void onInit(final InitEvent event) {
ShowPivotTableAction<TipInfo> showPivotTableAction =
actions.create(ShowPivotTableAction.ID);
showPivotTableAction.setTarget(tipInfoesDataGrid);
showManualSetPivotTableActionButton.setAction(showPivotTableAction);
showManualSetPivotTableActionButton.setText(
messages.getMessage(getClass(), "manualSetShowActionButton.text"));
}
ShowPivotTableAction
has two modes for exporting data:
-
All rows: Exports all rows in the
DataGrid
orTreeDataGrid
. -
Selected rows: Exports only the selected rows in the
DataGrid
orTreeDataGrid
.
If no rows are selected, all rows will be exported by default without confirmation.
A pivot table in UI Mode will be displayed in a new tab. By default, all attributes included in the component’s data container view will be shown, except for:
-
Attributes of type
Collection
. -
Attributes of type
byte[]
(byte array). -
UUID
attributes. -
Attributes annotated with @SystemLevel.
To exclude certain attributes or include only a specific subset, use the PivotTableViewBuilder
Spring bean. This bean provides a fluent API for configuring the pivot table component in the opened view.
To implement custom configuration, handle the ActionPerformedEvent
.
@Subscribe("tipInfoesDataGrid.customShowPivotTableAction")
public void onCustomShowPivotTableAction(final ActionPerformedEvent event) {
PivotTableViewBuilder builder = getApplicationContext().getBean(
PivotTableViewBuilder.class, tipInfoesDataGrid);
Renderers renderers = new Renderers();
renderers.setSelectedRenderer(Renderer.TABLE);
renderers.setRenderers(List.of(Renderer.TABLE, Renderer.TABLE_BAR_CHART, Renderer.HEATMAP,
Renderer.ROW_HEATMAP, Renderer.COL_HEATMAP));
builder.withIncludedProperties(Arrays.asList("sex", "smoker", "day", "time")) (1)
.withRows(Arrays.asList("sex", "smoker"))
.withColumns(Arrays.asList("day", "time"))
.withRenderers(renderers)
.withItems(tipInfoesDataGrid.getItems().getItems()) (2)
.show();
}
1 | The withIncludedProperties() method only considers the properties you explicitly include, ignoring all others. To exclude specific properties, use the withExcludedProperties() method. |
2 | All other methods starting with "with" can be used to configure Pivot Table options. These include methods like withHiddenFromAggregations() , withHiddenFromDragDrop() , withAggregationProperties() , and more. |
You can export the displayed Pivot Table data to Excel (if the current renderer is supported). A corresponding button will be displayed by default in the opened tab for this purpose.