Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an editable generated column in Vaadin?

Tags:

vaadin

I have a table in Vaadin that has 3 generated columns. However, I want one of them to be editable. Hence, the table has the following columns:

table.addGeneratedColumn("name", new NameGeneratedColumn());
table.addGeneratedColumn("classification", new ClassificationGeneratedColumn());
table.addGeneratedColumn("variation", new VariationGeneratedColumn());

I'd like to make the classification column editable when I click on an edit button. Inside the buttonClick method that receives the ClickEvent I tried to implement

table.setTableFieldFactory(new TableFieldFactory() {

        @Override
        public Field createField(Container container, Object itemId, Object propertyId, Component uiContext) 
            TextField tx = new TextField();
            tx.focus();
            tx.setWidth("90%");
            return tx;
        }
    });

And added the table.setEditable(true) which didn't affect anything because there are only generated columns on the table. It doesn't even enter the createField method.

like image 891
Rita Avatar asked Jan 25 '26 00:01

Rita


2 Answers

As far as I know, generated columns are not passed to the field factory. You maybe could add a normal String column with the "classification" id and then then add the generated column with the same id. Maybe You even need to remove the generated column when setting the table editable.

Something like this should work:

    final Table t = new Table();
    t.addContainerProperty("classification", String.class, null);
    final ColumnGenerator generator = new ColumnGenerator() {
        @Override
        public Object generateCell(Table source, Object itemId,
                Object columnId) {
            return "1";
        }
    };
    t.addGeneratedColumn("classification", generator);
    t.addItem();
    t.addItem();
    layout.addComponent(t);
    Button button = new Button("editable", new ClickListener() {

        @Override
        public void buttonClick(ClickEvent event) {
            t.setEditable(!t.isEditable());
            if (t.isEditable())
                t.removeGeneratedColumn("classification");
            else
                t.addGeneratedColumn("classification", generator);
        }
    });
    layout.addComponent(button);
like image 72
raffael Avatar answered Jan 27 '26 00:01

raffael


Create the editable component, that you need, right in the ColumnGenerator's generateCell method. This method gets both itemId and propertyId in its arguments, so you can check if the given cell is in editable state. You need to track this state yourself, of course, just keep an Object editedItemId somewhere.

You need to call the refreshRowCache method of your table for this to work. From its Javadoc:

A typical case when this is needed is if you update a generator (e.g. CellStyleGenerator) and want to ensure that the rows are redrawn with new styles.

Note that calling this method is not cheap so avoid calling it unnecessarily.

like image 42
Vsevolod Golovanov Avatar answered Jan 26 '26 23:01

Vsevolod Golovanov