Different values in a generated column

Hi,

How can I insert different values in a generated column?

transactionsTable.addGeneratedColumn("id", e -> {
    Label label = componentsFactory.createComponent(Label.class);
    //                for (int i=0; i< transactionsDs.size(); i++) 
    label.setValue(i);
    return label;
});

Hi,

Could you clarify your question?

Gleb

I want a new column in a table that can record the id for instance: 1,2,3…

Simply create a string that contains the desired value and set to the label component.

Alternatively, If you want to display just dynamic text, use special class Table.PlainTextCell instead of the Label component. It will simplify rendering and make the table faster.

Regards,
Gleb

I need a dynamic text (integer).
I used a Transient (Integer) property lineNum but I have the next error: ‘void’ type not allowed here

  int i=0;
        transactionsTable.addGeneratedColumn("id", e -> {
        for (Transaction c : transactionsDs.getItems()) {
                Table.PlainTextCell(Integer.toString(c.setLineNum(i++)));
            }
            return new Table.PlainTextCell("");
        });
  1. You pass c.setLineNum(i++) to the toString method, i.e. you pass a method that has no return type to the function that accepts Integer.
  2. You call Table.PlainTextCell every iteration without new and you don’t save it to a variable.
  3. Creating Table.PlainTextCell every iteration won’t produce an aggregated object with all the values.
  4. And finally, you return Table.PlainTextCell with empty value

How can I use Table.PlainTextCell in order to produce an agregated object?

Simply create a string that contains the desired value and return a PlainTextCell with this string:

StringBuilder sb = new StringBuilder();
int i = 0;
for (Transaction c : transactionsDs.getItems()) {
    c.setLineNum(i++);
    sb.append(i).append(",");
}
return new Table.PlainTextCell(sb.toString());

Thank you, Gleb!

Unfortunately it put the whole string into one cell.
My need is to put a number for each (Transaction) item (vertically).

image

It appears that you need numbering for items and as I see you have the lineNum attribute for that purpose. So, I would recommend taking a look at this post. Please note that in case of the persistent attribute you don’t need (but still can) use generated column, instead add it to the XML descriptor (i.e. add a column that refers to this attribute).

Thank you very much, Gleb!
However I don’t understand why doesn’t work with just simple addGeneratedColumn (or similar) method and needs to make a Transient or a persistent new column.

You don’t need, but without detailed description from your side I’m trying to guess your data model

and your actual requirements

Going back to your problem, you can use generated columns, e.g.:

@Subscribe
private void onInit(InitEvent event) {
    final int[] i = {1};
    customersTable.addGeneratedColumn("number", entity ->
            new Table.PlainTextCell(String.valueOf(i[0]++)));
}

Please note that in this case, the number column can’t be sortable and every time your table is repainted (e.g. if you sort another column, remove element etc.) row numbers will be incremented. That’s why in the link above it’s recommended to use the persistent property. Of course, everything depends on your requirements.

1 Like

Yes, you are right. Now I understood the reason.
I saw that you prefer to use onInit (Cuba 7) instead of init method. Is more accurate?

  • onInit(InitEvent event) - CUBA 7 new screens
  • init(Map) - CUBA 6 and CUBA 7 legacy screens
1 Like