Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vaadin Table row inline editing

Tags:

java

vaadin

I created a table in vaadin and I have an edit button for each row of the table.I tried

table.setEditable(true) 

but that makes the whole table editable. When I click the button edit I want only the selected row to be editable and if it is possible some cells to be customized example checkbox, dropdown etc. Any suggestions?

like image 394
Antonio Papa Avatar asked Dec 27 '22 05:12

Antonio Papa


1 Answers

Here is a very simplistic example:

final Table table = new Table();
table.setEditable(true);
table.setTableFieldFactory(new TableFieldFactory() {
    private static final long serialVersionUID = 1L;

    @Override
    public Field<?> createField(Container container, Object itemId, Object propertyId, Component uiContext) {
        if (itemId == table.getData()) {
            return DefaultFieldFactory.get().createField(container, itemId, propertyId, uiContext);
        }
        return null;
    }
});
table.addGeneratedColumn("", new ColumnGenerator() {
    private static final long serialVersionUID = 1L;

    @Override
    public Object generateCell(Table source, final Object itemId, Object columnId) {
        Button button = new Button(itemId == table.getData() ? "Save" : "Edit");
        button.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                if (table.getData() == null) {
                    // edit
                    table.setData(itemId);
                    table.refreshRowCache();
                } else if (itemId == table.getData()) {
                    // save
                    table.setData(null);
                    // ...
                    table.refreshRowCache();
                }
            }
        });
        return button;
    }
});
// ...
like image 76
Krayo Avatar answered Dec 31 '22 15:12

Krayo