[flowUI] Calculation (SUM) in treeDataGrid

I need to sum values of treeDataGrid, tried the following like generic UI but looks like it is nnot recognized in flow UI.

image

Interestingly, when I load the children rows (lowest level) from controller, parents rows populates 0, not the sum.
Is this not yet implemented or in FlowUI it is implemented differently?

Hi,

This functionality isn’t implemented yet. Here the issue.

Regards,
Gleb

Thank you Gleb for creating an issue. This is a basic requirement for any dataGrid, especially treeDataGrid. As we are migrating our application from the legacy platform version (CUBA) to FlowUI, we are facing the absence of many such things. Thanks for prioritizing…

Hi Gleb
Do you have any suggestions or can share code snippets on how do I perform “sum” for a column in dataGrid?

Hi,

We haven’t investigated a workaround for aggregation yet.

Regards,
Gleb

I have made progress in calculating the amount field to respective parent levels in treeDataGrid. But as you see, the original child data became 0 though all parent levels calculation is working well.

image

Here is my code, any suggestions to fix this problem to get it working?

 private void calcTreeGrid(){

// Create a map to store the child items for each parent item
        Map<BalanceSheet, List<BalanceSheet>> childItemsMap = new HashMap<>();
// Create a map to store the sum for each parent item
        Map<BalanceSheet, Double> sumMap = new HashMap<>();

// Iterate over each parent row
        balanceSheetDc.getItems().forEach(parentItem -> {
            double sum = 0.0;

            // Retrieve child rows for the current parent row
            List<BalanceSheet> childItems = balanceSheetDc.getMutableItems().stream()
                    .filter(childItem -> Objects.equals(childItem.getParentHead(), parentItem))
                    .collect(Collectors.toList());

            // Calculate the sum using the desired column(s) values
            sum = childItems.stream().mapToDouble(BalanceSheet::getAmountCy).sum();

            // Set the sum value on the parent row
            parentItem.setAmountCy(sum);

            // Update the sum in the map
            sumMap.put(parentItem, sum);
        });

        // Restore the original child item values
        childItemsMap.forEach((parentItem, childItems) -> {
            balanceSheetDc.getMutableItems().removeAll(childItems);
            balanceSheetDc.getMutableItems().addAll(childItems);
        });

    }

The alternative approach as follows but this second approach is also missing the value of original children value like the first approach:

private void calcTotalInTreeGrid(){
        // Iterate over each parent row
        balanceSheetDc.getItems().forEach(parentItem -> {
            final MutableDouble sum = new MutableDouble(0.0);

            // Retrieve child rows for the current parent row
            balanceSheetDc.getMutableItems().stream()
                    .filter(childItem -> Objects.equals(childItem.getParentHead(), parentItem))
                    .forEach(childItem -> {
                        // Calculate the sum using the desired column(s) values
                        double value = childItem.getAmountCy();
                        sum.add(value);
                    });

            // Set the sum value on the parent row
            parentItem.setAmountCy(sum.doubleValue());
        });

    }

    public class MutableDouble {
        private double value;

        public MutableDouble(double value) {
            this.value = value;
        }

        public void add(double value) {
            this.value += value;
        }

        public double doubleValue() {
            return value;
        }
    }

Hi,

In order to find a bug reason, I need to debug your code. Could you please attach a small demo project that reproduces the problem?

Gleb

Hi Gleb
I have created a demo project as attached. You will see the lowest level records has value when you go to the editor screen but it is 0 in the list view screen.
treeDataGridSum.zip (447.5 KB)

Thank you for the demo project. The problem that you calculate sum event if an item has no children. As a result, the sum becomes 0. You just need to skip calculation in this case, e.g. (based on code in the demo project):

// Calculate the sum using the desired column(s) values
double sum = childItems.isEmpty()
        ? (parentItem.getAmountCy() != null ? parentItem.getAmountCy() : 0.0)
        : childItems.stream().filter(f-> f.getAmountCy()!=null).mapToDouble(BalanceSheet::getAmountCy).sum();

Regards,
Gleb

Hi Gleb
Thank you and sorry for replying late. It is working very good except as follows:
As you see in the screen-shot, TOTAL EQUITY AND LIABLITIES is calculated from the children items perfectly. However, TOTAL ASSETS is not calculated from other items. Is there any clue what could be the reason?

image