Customer Columns Table

Is it possible to manage dynamic columns in a table? I want to give my clients the opportunity to add or remove columns in a table. Generally they are calculated columns that can make reference to other columns of the same row, for example if I have a table with 2 (A and B) columns that have numerical values, the user could add a third column that performs arithmetic operations on columns A and B. It could also be very interesting to create columns, where it is possible to write scripts of java languages ​​that return a value, with reference to the row datasource. The column could be created to show a value, but also to create a style for that single cell, for example to condition the background color of a cell if the sum of the columns A + B> 100.

Is it available to similar functionality with the current grid table component?

The only relevant functionality available out-of-the-box is Dynamic Attributes, but they are stored in the database and not calculated.

I think that calculated attributes shown in tables can be implemented in the project. You need some descriptors defining these attributes and calculation rules, and you can dynamically add appropriate columns to the tables using addGeneratedColumn method.

1 Like

Thanks Konstantin,
and what do you think about using a some script language java ? Suppose a table with 2 columns, price and quantity, then user want add a new column “Amount” and write some language script he can use, for example he can write a string like this “e.getQuanitity()*e.getPrice()”.

ordinesTable.addGeneratedColumn("Amount", new Table.ColumnGenerator<Ordine>() {
            @Override
            public Component generateCell(Order e) {
                Label field = (Label) componentsFactory.createComponent(Label.NAME);
                String script = getScript(...)
                // now script variable has "order.getQuanitity()*order.getPrice()"
                // call code by string
                string value = .....
                field.setValue(value);
                return field;
            }
        });

is possible call code by string ? I am thinking a something similar, give to user a tool to write script code, and able to return a value at the same time into the the script should be able to access object current entity.

Any idea ?

You can use Scripting interface to evaluate Groovy scripts. Groovy is very similar to Java, so I think this is what you need.

I’ve difficults understand how use a groovy script into generateCell method. How should be written the script, if I want use Order DataSource e into generateCell method to do a multipler operation math like this:
order.getQuanitity()*order.getPrice() ?

Looking your sample, I gues it would be something like:

@Inject
protected Scripting scripting;
...
ordinesTable.addGeneratedColumn("Amount", new Table.ColumnGenerator<Ordine>() {
            @Override
            public Component generateCell(Order e) {
                Label field = (Label) componentsFactory.createComponent(Label.NAME);
                Binding binding = new Binding();
                binding.setVariable("order", getItem());
                BigDecimal value = scripting.evaluateGroovy("return order.getQuanitity()*order.getPrice()", binding);                
                field.setValue(value);
                return field;
            }
        });
...

… but didnt test it.

2 Likes

thanks work, now I understood.

You can store your script in a file and use:

...
BigDecimal value = scripting.runGroovyScript("com/abc/sales/CalculatePrice.groovy", binding);
...

for more complexes scripts.

1 Like

You can also make the script a bit more groovy:

BigDecimal value = scripting.evaluateGroovy("order.quanitity * order.price", binding);

I just published my dynamic-columns component, and I’ll submit it to the marketplace later today.

You can find it on GH at the following url:

Bye
Paolo

2 Likes