Ady96
Ady96

Reputation: 716

Change column name - Vaadin 7.8.4

I use IndexedContainer in my grid.

Grid grid = new Grid();
IndexedContainer container = new IndexedContainer();
grid.setContainerDataSource(container);
container.addContainerProperty("Something", String.class, "");

I need to change the name of the container property. E.g property "Something" to "New property" after the click on the button. Any ideas ? Thank you very much !

Upvotes: 2

Views: 1173

Answers (1)

Morfic
Morfic

Reputation: 15518

NOTE 1: Where did you get vaadin 7.8.4 from? The latest 7.x release I can see is 7.7.10. For this exercise I'll assume it's a typo and use 7.7.4...


NOTE 2: Not sure whether you want to change just the column caption, or the entire property id... If it's just the caption you can use:

grid.getColumn("Something").setHeaderCaption("Something else");

AFAIK it's not possible to change a property. However, you can work around this by removing it and adding a new one:

public class MyGridWithChangeableColumnHeader extends VerticalLayout {
    public MyGridWithChangeableColumnHeader() {
        // basic grid setup
        Grid grid = new Grid();
        IndexedContainer container = new IndexedContainer();
        grid.setContainerDataSource(container);
        container.addContainerProperty("P1", String.class, "");
        container.addContainerProperty("Something", String.class, "");
        container.addContainerProperty("P3", String.class, "");

        // button to toggle properties
        Button button = new Button("Toggle properties", event -> {
            String oldProperty, newProperty;
            if (container.getContainerPropertyIds().contains("Something")) {
                oldProperty = "Something";
                newProperty = "Something else";
            } else {
                oldProperty = "Something else";
                newProperty = "Something";
            }

            container.removeContainerProperty(oldProperty);
            container.addContainerProperty(newProperty, String.class, "");
            grid.setColumnOrder("P1", newProperty, "P3");
        });

        addComponents(grid, button);
    }
}

Result:

container-toggle-property

Upvotes: 4

Related Questions