Manually add aggregation to the DataGrid it doesn't work

I created an AggregationInfo:

AggregationInfo aggregationInfo = new AggregationInfo();
aggregationInfo.setType(AggregationInfo.Type.SUM);

Then I created a column and added an aggregation to the dataGrid:

Grid.Column<EmployeeDTO> column = dataGrid.addColumn(new NumberRenderer<>(keyValue -> {
      Map<EmployeeDTO, Long> map = itemsMap.get(new EntityDto(kanbanStage));
      return map.get(keyValue);
 }, NumberFormat.getNumberInstance()))
         .setKey(STAGE_KEY + kanbanStage.getId())
         .setHeader(instanceNameProvider.getInstanceName(kanbanStage));

 dataGrid.addAggregation(column, aggregationInfo);

But it is not working and not throwing any error.

Hi!

As far as I can see, you don’t set dataGrid.setAggregatable(true); in your code.

Also, I want to inform you that common aggregation types (SUM, AVG etc.) are supported only if the meta property path is specified in the aggregation info. Otherwise, you need to implement a custom aggregation strategy.

An example can be found in the UI Samples online demo application: UI Samples :: DataGrid with aggregation

Best regards,
Dmitriy

Hello Dmitry

I did all:
dataGrid.setAggregable(true);
Tried using AggregationStrategy as well.

But since I programmatically added the column to the DataGrid, when I get the MetaPropertyPath from it, I get null.

for (KanbanStage kanbanStage : kanbanStages) {
            DataGrid.Column<EmployeeDTO> column =  dataGrid.addColumn(new NumberRenderer<>(keyValue -> {
                        Map<EmployeeDTO, Long> map = itemsMap.get(new EntityDto(kanbanStage));
                        return map.get(keyValue);
                    }, NumberFormat.getNumberInstance()))
                    .setKey(STAGE_KEY + kanbanStage.getId())
                    .setHeader(instanceNameProvider.getInstanceName(kanbanStage));

            MetaPropertyPath propertyPath = dataGrid.getColumnMetaPropertyPath(column);
            aggregationInfo.setPropertyPath(propertyPath); // propertyPath is null
        }

This is expected behavior because the metaPropertyPath in a column only appears if it is specified when the column is created. See methods overloading:

  • io.jmix.flowui.component.grid.DataGrid#addColumn(io.jmix.core.metamodel.model.MetaPropertyPath)
  • io.jmix.flowui.component.grid.DataGrid#addColumn(java.lang.String, io.jmix.core.metamodel.model.MetaPropertyPath)

Meta property path is calculated automatically when creating the column declaratively using XML markup, you can find an loader code here: io.jmix.flowui.xml.layout.loader.component.AbstractGridLoader#loadColumn

As I wrote earlier, if you don’t have a meta property path for a column, you can try using a custom aggregation strategy.

You can also provide a test project so that we can understand your issue in more detail.

Regards,
Dmitriy